@@ -74,6 +74,11 @@ public final class AuthorizationFramework {
74
74
* Stack of available plugins/stacks in the order of the execution.
75
75
*/
76
76
AuthorizationStack stack ;
77
+ /**
78
+ * New stack. This is set by {@code setStack()} and used for delayed
79
+ * reconfiguration in {@code reload()}.
80
+ */
81
+ AuthorizationStack newStack ;
77
82
78
83
/**
79
84
* Lock for safe reloads.
@@ -89,6 +94,20 @@ public final class AuthorizationFramework {
89
94
*/
90
95
private long pluginVersion = 0 ;
91
96
97
+ /**
98
+ * Whether to load plugins from class files and jar files.
99
+ */
100
+ private boolean loadClasses = true ;
101
+ private boolean loadJars = true ;
102
+
103
+ /**
104
+ * Create a new instance of authorization framework with no plugin
105
+ * directory and the default plugin stack.
106
+ */
107
+ public AuthorizationFramework () {
108
+ this (null );
109
+ }
110
+
92
111
/**
93
112
* Create a new instance of authorization framework with the plugin
94
113
* directory and the default plugin stack.
@@ -109,7 +128,6 @@ public AuthorizationFramework(String path) {
109
128
public AuthorizationFramework (String path , AuthorizationStack stack ) {
110
129
this .stack = stack ;
111
130
setPluginDirectory (path );
112
- reload ();
113
131
}
114
132
115
133
/**
@@ -138,6 +156,38 @@ public void setPluginDirectory(String directory) {
138
156
setPluginDirectory (directory != null ? new File (directory ) : null );
139
157
}
140
158
159
+ /**
160
+ * Make {@code reload()} search for plugins in class files.
161
+ * @param flag true or false
162
+ */
163
+ public void setLoadClasses (boolean flag ) {
164
+ loadClasses = flag ;
165
+ }
166
+
167
+ /**
168
+ * Whether to search for plugins in class files.
169
+ * @return true if enabled, false otherwise
170
+ */
171
+ public boolean isLoadClassesEnabled () {
172
+ return loadClasses ;
173
+ }
174
+
175
+ /**
176
+ * Make {@code reload()} search for plugins in jar files.
177
+ * @param flag true or false
178
+ */
179
+ public void setLoadJars (boolean flag ) {
180
+ loadJars = flag ;
181
+ }
182
+
183
+ /**
184
+ * Whether to search for plugins in class files.
185
+ * @return true if enabled, false otherwise
186
+ */
187
+ public boolean isLoadJarsEnabled () {
188
+ return loadJars ;
189
+ }
190
+
141
191
/**
142
192
* Checks if the request should have an access to project. See
143
193
* {@link #checkAll} for more information about invocation order.
@@ -248,17 +298,13 @@ public AuthorizationStack getStack() {
248
298
}
249
299
250
300
/**
251
- * Set the internal stack to this new value.
301
+ * Set new value of the authorization stack. This will come into effect
302
+ * only after {@code reload()} is called.
252
303
*
253
304
* @param s new stack to be used
254
305
*/
255
306
public void setStack (AuthorizationStack s ) {
256
- lock .writeLock ().lock ();
257
- try {
258
- this .stack = s ;
259
- } finally {
260
- lock .writeLock ().unlock ();
261
- }
307
+ this .newStack = s ;
262
308
}
263
309
264
310
/**
@@ -438,20 +484,19 @@ protected List<Class> getInterfaces(Class c) {
438
484
}
439
485
440
486
/**
441
- * Traverse list of files which possibly contain a java class and then
442
- * traverse a list of jar files to load all classes which are contained
443
- * within them into the given stack. Each class is loaded with
444
- * {@link #handleLoadClass(String)} which delegates the loading to the
445
- * custom class loader {@link #loadClass(String)}.
487
+ * Traverse list of files which possibly contain a java class
488
+ * to load all classes which are contained within them into the given stack.
489
+ * Each class is loaded with {@link #handleLoadClass(String)} which
490
+ * delegates the loading to the custom class loader
491
+ * {@link #loadClass(String)}.
446
492
*
447
493
* @param stack the stack where to add the loaded classes
448
494
* @param classfiles list of files which possibly contain a java class
449
- * @param jarfiles list of jar files containing java classes
450
495
*
451
496
* @see #handleLoadClass(String)
452
497
* @see #loadClass(String)
453
498
*/
454
- private void loadClasses (AuthorizationStack stack , List <File > classfiles , List < File > jarfiles ) {
499
+ private void loadClassFiles (AuthorizationStack stack , List <File > classfiles ) {
455
500
IAuthorizationPlugin pf ;
456
501
457
502
for (File file : classfiles ) {
@@ -467,6 +512,23 @@ private void loadClasses(AuthorizationStack stack, List<File> classfiles, List<F
467
512
}
468
513
}
469
514
}
515
+ }
516
+
517
+ /**
518
+ * Traverse list of jar files to load all classes which are contained within
519
+ * them into the given stack.
520
+ * Each class is loaded with {@link #handleLoadClass(String)} which
521
+ * delegates the loading to the custom class loader
522
+ * {@link #loadClass(String)}.
523
+ *
524
+ * @param stack the stack where to add the loaded classes
525
+ * @param jarfiles list of jar files containing java classes
526
+ *
527
+ * @see #handleLoadClass(String)
528
+ * @see #loadClass(String)
529
+ */
530
+ private void loadJarFiles (AuthorizationStack stack , List <File > jarfiles ) {
531
+ IAuthorizationPlugin pf ;
470
532
471
533
for (File file : jarfiles ) {
472
534
try (JarFile jar = new JarFile (file )) {
@@ -544,16 +606,24 @@ public Object run() {
544
606
}
545
607
});
546
608
547
- // clone a new stack not interfering with the current stack
548
- AuthorizationStack newStack = getStack ().clone ();
549
-
550
- // load all other possible plugin classes
551
- loadClasses (newStack ,
552
- IOUtils .listFilesRec (pluginDirectory , ".class" ),
553
- IOUtils .listFiles (pluginDirectory , ".jar" ));
609
+ AuthorizationStack newLocalStack ;
610
+ if (this .newStack == null ) {
611
+ // Clone a new stack not interfering with the current stack.
612
+ newLocalStack = getStack ().clone ();
613
+ } else {
614
+ newLocalStack = this .newStack .clone ();
615
+ }
616
+
617
+ // Load all other possible plugin classes.
618
+ if (isLoadClassesEnabled ()) {
619
+ loadClassFiles (newLocalStack , IOUtils .listFilesRec (pluginDirectory , ".class" ));
620
+ }
621
+ if (isLoadJarsEnabled ()) {
622
+ loadJarFiles (newLocalStack , IOUtils .listFiles (pluginDirectory , ".jar" ));
623
+ }
554
624
555
625
// fire load events
556
- loadAllPlugins (newStack );
626
+ loadAllPlugins (newLocalStack );
557
627
558
628
AuthorizationStack oldStack ;
559
629
/**
@@ -566,7 +636,7 @@ public Object run() {
566
636
lock .writeLock ().lock ();
567
637
try {
568
638
oldStack = stack ;
569
- stack = newStack ;
639
+ stack = newLocalStack ;
570
640
571
641
// increase the current plugin version tracked by the framework
572
642
increasePluginVersion ();
0 commit comments