60 public ScriptEngineManager() {
61 ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
62 init(ctxtLoader);
63 }
64
65 /**
66 * This constructor loads the implementations of
67 * <code>ScriptEngineFactory</code> visible to the given
68 * <code>ClassLoader</code> using the service provider mechanism.<br><br>
69 * If loader is <code>null</code>, the script engine factories that are
70 * bundled with the platform are loaded. <br>
71 *
72 * @param loader ClassLoader used to discover script engine factories.
73 */
74 public ScriptEngineManager(ClassLoader loader) {
75 init(loader);
76 }
77
78 private void init(final ClassLoader loader) {
79 globalScope = new SimpleBindings();
80 engineSpis = new HashSet<ScriptEngineFactory>();
81 nameAssociations = new HashMap<String, ScriptEngineFactory>();
82 extensionAssociations = new HashMap<String, ScriptEngineFactory>();
83 mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
84 initEngines(loader);
85 }
86
87 private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
88 if (loader != null) {
89 return ServiceLoader.load(ScriptEngineFactory.class, loader);
90 } else {
91 return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
92 }
93 }
94
95 private void initEngines(final ClassLoader loader) {
96 Iterator<ScriptEngineFactory> itr = null;
97 try {
98 ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
99 new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
100 @Override
383 public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
384 if (type == null || factory == null) throw new NullPointerException();
385 mimeTypeAssociations.put(type, factory);
386 }
387
388 /**
389 * Registers a <code>ScriptEngineFactory</code> to handle an extension.
390 * Overrides any such association found using the Discovery mechanism.
391 *
392 * @param extension The extension type to be associated with the
393 * <code>ScriptEngineFactory</code>.
394 * @param factory The class to associate with the given extension.
395 * @throws NullPointerException if any of the parameters is null.
396 */
397 public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
398 if (extension == null || factory == null) throw new NullPointerException();
399 extensionAssociations.put(extension, factory);
400 }
401
402 /** Set of script engine factories discovered. */
403 private HashSet<ScriptEngineFactory> engineSpis;
404
405 /** Map of engine name to script engine factory. */
406 private HashMap<String, ScriptEngineFactory> nameAssociations;
407
408 /** Map of script file extension to script engine factory. */
409 private HashMap<String, ScriptEngineFactory> extensionAssociations;
410
411 /** Map of script MIME type to script engine factory. */
412 private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
413
414 /** Global bindings associated with script engines created by this manager. */
415 private Bindings globalScope;
416 }
|
60 public ScriptEngineManager() {
61 ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
62 init(ctxtLoader);
63 }
64
65 /**
66 * This constructor loads the implementations of
67 * <code>ScriptEngineFactory</code> visible to the given
68 * <code>ClassLoader</code> using the service provider mechanism.<br><br>
69 * If loader is <code>null</code>, the script engine factories that are
70 * bundled with the platform are loaded. <br>
71 *
72 * @param loader ClassLoader used to discover script engine factories.
73 */
74 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(ScriptEngineFactory::getEngineName));
81 nameAssociations = new HashMap<String, ScriptEngineFactory>();
82 extensionAssociations = new HashMap<String, ScriptEngineFactory>();
83 mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
84 initEngines(loader);
85 }
86
87 private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
88 if (loader != null) {
89 return ServiceLoader.load(ScriptEngineFactory.class, loader);
90 } else {
91 return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
92 }
93 }
94
95 private void initEngines(final ClassLoader loader) {
96 Iterator<ScriptEngineFactory> itr = null;
97 try {
98 ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
99 new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
100 @Override
383 public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
384 if (type == null || factory == null) throw new NullPointerException();
385 mimeTypeAssociations.put(type, factory);
386 }
387
388 /**
389 * Registers a <code>ScriptEngineFactory</code> to handle an extension.
390 * Overrides any such association found using the Discovery mechanism.
391 *
392 * @param extension The extension type to be associated with the
393 * <code>ScriptEngineFactory</code>.
394 * @param factory The class to associate with the given extension.
395 * @throws NullPointerException if any of the parameters is null.
396 */
397 public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
398 if (extension == null || factory == null) throw new NullPointerException();
399 extensionAssociations.put(extension, factory);
400 }
401
402 /** Set of script engine factories discovered. */
403 private TreeSet<ScriptEngineFactory> engineSpis;
404
405 /** Map of engine name to script engine factory. */
406 private HashMap<String, ScriptEngineFactory> nameAssociations;
407
408 /** Map of script file extension to script engine factory. */
409 private HashMap<String, ScriptEngineFactory> extensionAssociations;
410
411 /** Map of script MIME type to script engine factory. */
412 private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
413
414 /** Global bindings associated with script engines created by this manager. */
415 private Bindings globalScope;
416 }
|