53
53
public final class AuthorizationFramework {
54
54
55
55
private static final Logger LOGGER = LoggerFactory .getLogger (AuthorizationFramework .class );
56
- private final File directory ;
56
+ private File pluginDirectory ;
57
57
private AuthorizationPluginClassLoader loader ;
58
58
59
59
private volatile static AuthorizationFramework instance = new AuthorizationFramework ();
@@ -70,6 +70,31 @@ public static AuthorizationFramework getInstance() {
70
70
return instance ;
71
71
}
72
72
73
+ /**
74
+ * Get the plugin directory.
75
+ */
76
+ public synchronized File getPluginDirectory () {
77
+ return pluginDirectory ;
78
+ }
79
+
80
+ /**
81
+ * Set the plugin directory.
82
+ *
83
+ * @param pluginDirectory the directory
84
+ */
85
+ public synchronized void setPluginDirectory (File pluginDirectory ) {
86
+ this .pluginDirectory = pluginDirectory ;
87
+ }
88
+
89
+ /**
90
+ * Set the plugin directory.
91
+ *
92
+ * @param directory the directory path
93
+ */
94
+ public void setPluginDirectory (String directory ) {
95
+ AuthorizationFramework .this .setPluginDirectory (directory != null ? new File (directory ) : null );
96
+ }
97
+
73
98
/**
74
99
* Checks if the request should have an access to project.
75
100
*
@@ -113,22 +138,19 @@ public boolean test(IAuthorizationPlugin plugin) {
113
138
private AuthorizationFramework () {
114
139
String path = RuntimeEnvironment .getInstance ()
115
140
.getPluginDirectory ();
116
- File directory = path == null ? null : new File (path );
117
- if (path == null || directory == null || !directory .isDirectory () || !directory .canRead ()) {
118
- LOGGER .log (Level .INFO , "plugin directory not found or not readable: {0}. "
119
- + "The AuthorizationFramework will just pass requests through." , path );
120
- }
121
141
plugins = new ArrayList <>();
122
- this . directory = directory ;
142
+ setPluginDirectory ( path ) ;
123
143
reload ();
124
144
}
125
145
126
146
/**
127
147
* Get available plugins.
128
148
*
129
149
* This and couple of following methods are declared as synchronized because
130
- * 1) plugins can be reloaded at anytime
131
- * 2) requests are pretty asynchronous
150
+ * <ol>
151
+ * <li>plugins can be reloaded at anytime</li>
152
+ * <li>requests are pretty asynchronous</li>
153
+ * </ol>
132
154
*
133
155
* So this tries to ensure that there will be no
134
156
* ConcurrentModificationException or other similar exceptions.
@@ -179,7 +201,7 @@ private void removeAll() {
179
201
* @return list of file with suffix
180
202
*/
181
203
private List <File > listFiles (String suffix ) {
182
- File [] files = directory .listFiles (new FilenameFilter () {
204
+ File [] files = pluginDirectory .listFiles (new FilenameFilter () {
183
205
@ Override
184
206
public boolean accept (File dir , String name ) {
185
207
return name .endsWith (suffix );
@@ -196,7 +218,7 @@ public boolean accept(File dir, String name) {
196
218
* @return recursively traversed list of files with given suffix
197
219
*/
198
220
private List <File > listFilesRec (String suffix ) {
199
- return listFilesClassesRec (directory , suffix );
221
+ return listFilesClassesRec (pluginDirectory , suffix );
200
222
}
201
223
202
224
private List <File > listFilesClassesRec (File start , String suffix ) {
@@ -236,7 +258,7 @@ private void loadClass(String classname) throws ClassNotFoundException,
236
258
SecurityException ,
237
259
InstantiationException ,
238
260
IllegalAccessException {
239
-
261
+
240
262
Class c = loader .loadClass (classname );
241
263
// check for implemented interfaces
242
264
Class [] intf = c .getInterfaces ();
@@ -256,7 +278,7 @@ private void loadClass(String classname) throws ClassNotFoundException,
256
278
}
257
279
258
280
private String getClassName (File f ) {
259
- String classname = f .getAbsolutePath ().substring (directory .getAbsolutePath ().length () + 1 , f .getAbsolutePath ().length ());
281
+ String classname = f .getAbsolutePath ().substring (pluginDirectory .getAbsolutePath ().length () + 1 , f .getAbsolutePath ().length ());
260
282
classname = classname .replace (File .separatorChar , '.' ); // convert to package name
261
283
classname = classname .substring (0 , classname .lastIndexOf ('.' )); // strip .class
262
284
return classname ;
@@ -275,24 +297,27 @@ private String getClassName(JarEntry f) {
275
297
* Old instances of plugins are removed and new list of plugins is
276
298
* constructed. Unload and load event is fired on each plugin.
277
299
*
278
- * @see IAuthorizationPlugin#load()
279
- * @see IAuthorizationPlugin#unload()
300
+ * @see IAuthorizationPlugin#load()
301
+ * @see IAuthorizationPlugin#unload()
280
302
*/
281
303
@ SuppressWarnings ("unchecked" )
282
304
public synchronized void reload () {
283
- if (directory == null || !directory .isDirectory () || !directory .canRead ()) {
305
+ if (pluginDirectory == null || !pluginDirectory .isDirectory () || !pluginDirectory .canRead ()) {
306
+ LOGGER .log (Level .WARNING , "plugin directory not found or not readable: {0}. "
307
+ + "All requests allowed." , pluginDirectory );
284
308
return ;
285
309
}
286
- LOGGER .log (Level .INFO , "Plugins are being reloaded from " + directory .getAbsolutePath ());
310
+ LOGGER .log (Level .INFO , "Plugins are being reloaded from {0}" , pluginDirectory .getAbsolutePath ());
287
311
removeAll ();
288
312
// trashing out the old instance of the loaded enables us
289
313
// to reaload the plugins at runtime
290
314
loader = (AuthorizationPluginClassLoader ) AccessController .doPrivileged (new PrivilegedAction () {
315
+ @ Override
291
316
public Object run () {
292
- return new AuthorizationPluginClassLoader (directory );
317
+ return new AuthorizationPluginClassLoader (pluginDirectory );
293
318
}
294
319
});
295
-
320
+
296
321
removeAll ();
297
322
plugins = new ArrayList <>();
298
323
@@ -322,7 +347,7 @@ public Object run() {
322
347
LOGGER .log (Level .INFO , "Could not manipulate with file because of: " , ex );
323
348
}
324
349
}
325
-
350
+
326
351
for (IAuthorizationPlugin plugin : getPlugins ()) {
327
352
try {
328
353
plugin .load ();
0 commit comments