28
28
import java .security .*;
29
29
import java .util .ServiceLoader ;
30
30
import java .util .ServiceConfigurationError ;
31
+ import java .util .function .Function ;
32
+ import java .util .stream .Stream ;
31
33
32
34
/**
33
35
* The <code>ScriptEngineManager</code> implements a discovery and instantiation
@@ -58,8 +60,7 @@ public class ScriptEngineManager {
58
60
* @see java.lang.Thread#getContextClassLoader
59
61
*/
60
62
public ScriptEngineManager () {
61
- ClassLoader ctxtLoader = Thread .currentThread ().getContextClassLoader ();
62
- init (ctxtLoader );
63
+ this (Thread .currentThread ().getContextClassLoader ());
63
64
}
64
65
65
66
/**
@@ -72,18 +73,6 @@ public ScriptEngineManager() {
72
73
* @param loader ClassLoader used to discover script engine factories.
73
74
*/
74
75
public ScriptEngineManager (ClassLoader loader ) {
75
- init (loader );
76
- }
77
-
78
- private void init (final ClassLoader loader ) {
79
- globalScope = new SimpleBindings ();
80
- engineSpis = new TreeSet <ScriptEngineFactory >(Comparator .comparing (
81
- ScriptEngineFactory ::getEngineName ,
82
- Comparator .nullsLast (Comparator .naturalOrder ()))
83
- );
84
- nameAssociations = new HashMap <String , ScriptEngineFactory >();
85
- extensionAssociations = new HashMap <String , ScriptEngineFactory >();
86
- mimeTypeAssociations = new HashMap <String , ScriptEngineFactory >();
87
76
initEngines (loader );
88
77
}
89
78
@@ -96,23 +85,13 @@ private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader lo
96
85
}
97
86
98
87
private void initEngines (final ClassLoader loader ) {
99
- Iterator <ScriptEngineFactory > itr = null ;
88
+ Iterator <ScriptEngineFactory > itr ;
100
89
try {
101
- ServiceLoader <ScriptEngineFactory > sl = AccessController .doPrivileged (
102
- new PrivilegedAction <ServiceLoader <ScriptEngineFactory >>() {
103
- @ Override
104
- public ServiceLoader <ScriptEngineFactory > run () {
105
- return getServiceLoader (loader );
106
- }
107
- });
108
-
90
+ var sl = AccessController .doPrivileged (
91
+ (PrivilegedAction <ServiceLoader <ScriptEngineFactory >>)() -> getServiceLoader (loader ));
109
92
itr = sl .iterator ();
110
93
} catch (ServiceConfigurationError err ) {
111
- System .err .println ("Can't find ScriptEngineFactory providers: " +
112
- err .getMessage ());
113
- if (DEBUG ) {
114
- err .printStackTrace ();
115
- }
94
+ reportException ("Can't find ScriptEngineFactory providers: " , err );
116
95
// do not throw any exception here. user may want to
117
96
// manage his/her own factories using this manager
118
97
// by explicit registratation (by registerXXX) methods.
@@ -125,25 +104,15 @@ public ServiceLoader<ScriptEngineFactory> run() {
125
104
ScriptEngineFactory fact = itr .next ();
126
105
engineSpis .add (fact );
127
106
} catch (ServiceConfigurationError err ) {
128
- System .err .println ("ScriptEngineManager providers.next(): "
129
- + err .getMessage ());
130
- if (DEBUG ) {
131
- err .printStackTrace ();
132
- }
107
+ reportException ("ScriptEngineManager providers.next(): " , err );
133
108
// one factory failed, but check other factories...
134
- continue ;
135
109
}
136
110
}
137
111
} catch (ServiceConfigurationError err ) {
138
- System .err .println ("ScriptEngineManager providers.hasNext(): "
139
- + err .getMessage ());
140
- if (DEBUG ) {
141
- err .printStackTrace ();
142
- }
112
+ reportException ("ScriptEngineManager providers.hasNext(): " , err );
143
113
// do not throw any exception here. user may want to
144
114
// manage his/her own factories using this manager
145
115
// by explicit registratation (by registerXXX) methods.
146
- return ;
147
116
}
148
117
}
149
118
@@ -212,44 +181,7 @@ public Object get(String key) {
212
181
* @throws NullPointerException if shortName is null.
213
182
*/
214
183
public ScriptEngine getEngineByName (String shortName ) {
215
- if (shortName == null ) throw new NullPointerException ();
216
- //look for registered name first
217
- Object obj ;
218
- if (null != (obj = nameAssociations .get (shortName ))) {
219
- ScriptEngineFactory spi = (ScriptEngineFactory )obj ;
220
- try {
221
- ScriptEngine engine = spi .getScriptEngine ();
222
- engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
223
- return engine ;
224
- } catch (Exception exp ) {
225
- if (DEBUG ) exp .printStackTrace ();
226
- }
227
- }
228
-
229
- for (ScriptEngineFactory spi : engineSpis ) {
230
- List <String > names = null ;
231
- try {
232
- names = spi .getNames ();
233
- } catch (Exception exp ) {
234
- if (DEBUG ) exp .printStackTrace ();
235
- }
236
-
237
- if (names != null ) {
238
- for (String name : names ) {
239
- if (shortName .equals (name )) {
240
- try {
241
- ScriptEngine engine = spi .getScriptEngine ();
242
- engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
243
- return engine ;
244
- } catch (Exception exp ) {
245
- if (DEBUG ) exp .printStackTrace ();
246
- }
247
- }
248
- }
249
- }
250
- }
251
-
252
- return null ;
184
+ return getEngineBy (shortName , nameAssociations , ScriptEngineFactory ::getNames );
253
185
}
254
186
255
187
/**
@@ -263,41 +195,7 @@ public ScriptEngine getEngineByName(String shortName) {
263
195
* @throws NullPointerException if extension is null.
264
196
*/
265
197
public ScriptEngine getEngineByExtension (String extension ) {
266
- if (extension == null ) throw new NullPointerException ();
267
- //look for registered extension first
268
- Object obj ;
269
- if (null != (obj = extensionAssociations .get (extension ))) {
270
- ScriptEngineFactory spi = (ScriptEngineFactory )obj ;
271
- try {
272
- ScriptEngine engine = spi .getScriptEngine ();
273
- engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
274
- return engine ;
275
- } catch (Exception exp ) {
276
- if (DEBUG ) exp .printStackTrace ();
277
- }
278
- }
279
-
280
- for (ScriptEngineFactory spi : engineSpis ) {
281
- List <String > exts = null ;
282
- try {
283
- exts = spi .getExtensions ();
284
- } catch (Exception exp ) {
285
- if (DEBUG ) exp .printStackTrace ();
286
- }
287
- if (exts == null ) continue ;
288
- for (String ext : exts ) {
289
- if (extension .equals (ext )) {
290
- try {
291
- ScriptEngine engine = spi .getScriptEngine ();
292
- engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
293
- return engine ;
294
- } catch (Exception exp ) {
295
- if (DEBUG ) exp .printStackTrace ();
296
- }
297
- }
298
- }
299
- }
300
- return null ;
198
+ return getEngineBy (extension , extensionAssociations , ScriptEngineFactory ::getExtensions );
301
199
}
302
200
303
201
/**
@@ -311,41 +209,52 @@ public ScriptEngine getEngineByExtension(String extension) {
311
209
* @throws NullPointerException if mimeType is null.
312
210
*/
313
211
public ScriptEngine getEngineByMimeType (String mimeType ) {
314
- if (mimeType == null ) throw new NullPointerException ();
315
- //look for registered types first
316
- Object obj ;
317
- if (null != (obj = mimeTypeAssociations .get (mimeType ))) {
318
- ScriptEngineFactory spi = (ScriptEngineFactory )obj ;
319
- try {
320
- ScriptEngine engine = spi .getScriptEngine ();
321
- engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
322
- return engine ;
323
- } catch (Exception exp ) {
324
- if (DEBUG ) exp .printStackTrace ();
325
- }
326
- }
212
+ return getEngineBy (mimeType , mimeTypeAssociations , ScriptEngineFactory ::getMimeTypes );
213
+ }
327
214
328
- for (ScriptEngineFactory spi : engineSpis ) {
329
- List <String > types = null ;
330
- try {
331
- types = spi .getMimeTypes ();
332
- } catch (Exception exp ) {
333
- if (DEBUG ) exp .printStackTrace ();
334
- }
335
- if (types == null ) continue ;
336
- for (String type : types ) {
337
- if (mimeType .equals (type )) {
338
- try {
339
- ScriptEngine engine = spi .getScriptEngine ();
340
- engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
341
- return engine ;
342
- } catch (Exception exp ) {
343
- if (DEBUG ) exp .printStackTrace ();
344
- }
215
+ private ScriptEngine getEngineBy (String selector , Map <String , ScriptEngineFactory > associations ,
216
+ Function <ScriptEngineFactory , List <String >> valuesFn )
217
+ {
218
+ Objects .requireNonNull (selector );
219
+ Stream <ScriptEngineFactory > spis = Stream .concat (
220
+ //look for registered types first
221
+ Stream .ofNullable (associations .get (selector )),
222
+
223
+ engineSpis .stream ().filter (spi -> {
224
+ try {
225
+ List <String > matches = valuesFn .apply (spi );
226
+ return matches != null && matches .contains (selector );
227
+ } catch (Exception exp ) {
228
+ debugPrint (exp );
229
+ return false ;
345
230
}
346
- }
231
+ })
232
+ );
233
+ return spis
234
+ .map (spi -> {
235
+ try {
236
+ ScriptEngine engine = spi .getScriptEngine ();
237
+ engine .setBindings (getBindings (), ScriptContext .GLOBAL_SCOPE );
238
+ return engine ;
239
+ } catch (Exception exp ) {
240
+ debugPrint (exp );
241
+ return null ;
242
+ }
243
+ })
244
+ .filter (Objects ::nonNull )
245
+ .findFirst ()
246
+ .orElse (null );
247
+ }
248
+
249
+ private static void reportException (String msg , Throwable exp ) {
250
+ System .err .println (msg + exp .getMessage ());
251
+ debugPrint (exp );
252
+ }
253
+
254
+ private static void debugPrint (Throwable exp ) {
255
+ if (DEBUG ) {
256
+ exp .printStackTrace ();
347
257
}
348
- return null ;
349
258
}
350
259
351
260
/**
@@ -354,11 +263,7 @@ public ScriptEngine getEngineByMimeType(String mimeType) {
354
263
* @return List of all discovered <code>ScriptEngineFactory</code>s.
355
264
*/
356
265
public List <ScriptEngineFactory > getEngineFactories () {
357
- List <ScriptEngineFactory > res = new ArrayList <ScriptEngineFactory >(engineSpis .size ());
358
- for (ScriptEngineFactory spi : engineSpis ) {
359
- res .add (spi );
360
- }
361
- return Collections .unmodifiableList (res );
266
+ return List .copyOf (engineSpis );
362
267
}
363
268
364
269
/**
@@ -369,8 +274,7 @@ public List<ScriptEngineFactory> getEngineFactories() {
369
274
* @throws NullPointerException if any of the parameters is null.
370
275
*/
371
276
public void registerEngineName (String name , ScriptEngineFactory factory ) {
372
- if (name == null || factory == null ) throw new NullPointerException ();
373
- nameAssociations .put (name , factory );
277
+ associateFactory (nameAssociations , name , factory );
374
278
}
375
279
376
280
/**
@@ -384,8 +288,7 @@ public void registerEngineName(String name, ScriptEngineFactory factory) {
384
288
* @throws NullPointerException if any of the parameters is null.
385
289
*/
386
290
public void registerEngineMimeType (String type , ScriptEngineFactory factory ) {
387
- if (type == null || factory == null ) throw new NullPointerException ();
388
- mimeTypeAssociations .put (type , factory );
291
+ associateFactory (mimeTypeAssociations , type , factory );
389
292
}
390
293
391
294
/**
@@ -398,22 +301,33 @@ public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
398
301
* @throws NullPointerException if any of the parameters is null.
399
302
*/
400
303
public void registerEngineExtension (String extension , ScriptEngineFactory factory ) {
401
- if (extension == null || factory == null ) throw new NullPointerException ();
402
- extensionAssociations .put (extension , factory );
304
+ associateFactory (extensionAssociations , extension , factory );
403
305
}
404
306
307
+ private static void associateFactory (Map <String , ScriptEngineFactory > associations , String association ,
308
+ ScriptEngineFactory factory )
309
+ {
310
+ if (association == null || factory == null ) throw new NullPointerException ();
311
+ associations .put (association , factory );
312
+ }
313
+
314
+ private static final Comparator <ScriptEngineFactory > COMPARATOR = Comparator .comparing (
315
+ ScriptEngineFactory ::getEngineName ,
316
+ Comparator .nullsLast (Comparator .naturalOrder ())
317
+ );
318
+
405
319
/** Set of script engine factories discovered. */
406
- private TreeSet <ScriptEngineFactory > engineSpis ;
320
+ private final TreeSet <ScriptEngineFactory > engineSpis = new TreeSet <>( COMPARATOR ) ;
407
321
408
322
/** Map of engine name to script engine factory. */
409
- private HashMap <String , ScriptEngineFactory > nameAssociations ;
323
+ private final HashMap <String , ScriptEngineFactory > nameAssociations = new HashMap <>() ;
410
324
411
325
/** Map of script file extension to script engine factory. */
412
- private HashMap <String , ScriptEngineFactory > extensionAssociations ;
326
+ private final HashMap <String , ScriptEngineFactory > extensionAssociations = new HashMap <>() ;
413
327
414
328
/** Map of script MIME type to script engine factory. */
415
- private HashMap <String , ScriptEngineFactory > mimeTypeAssociations ;
329
+ private final HashMap <String , ScriptEngineFactory > mimeTypeAssociations = new HashMap <>() ;
416
330
417
331
/** Global bindings associated with script engines created by this manager. */
418
- private Bindings globalScope ;
332
+ private Bindings globalScope = new SimpleBindings () ;
419
333
}
0 commit comments