22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.script;
27 import java.util.*;
28 import java.net.URL;
29 import java.io.*;
30 import java.security.*;
31 import sun.misc.Service;
32 import sun.misc.ServiceConfigurationError;
33 import sun.reflect.Reflection;
34 import sun.security.util.SecurityConstants;
35
36 /**
37 * The <code>ScriptEngineManager</code> implements a discovery and instantiation
38 * mechanism for <code>ScriptEngine</code> classes and also maintains a
39 * collection of key/value pairs storing state shared by all engines created
40 * by the Manager. This class uses the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism to enumerate all the
41 * implementations of <code>ScriptEngineFactory</code>. <br><br>
42 * The <code>ScriptEngineManager</code> provides a method to return an array of all these factories
43 * as well as utility methods which look up factories on the basis of language name, file extension
44 * and mime type.
45 * <p>
46 * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope" maintained
47 * by the manager is available to all instances of <code>ScriptEngine</code> created
48 * by the <code>ScriptEngineManager</code>. The values in the <code>Bindings</code> are
49 * generally exposed in all scripts.
50 *
51 * @author Mike Grogan
52 * @author A. Sundararajan
53 * @since 1.6
54 */
55 public class ScriptEngineManager {
56 private static final boolean DEBUG = false;
57 /**
58 * If the thread context ClassLoader can be accessed by the caller,
59 * then the effect of calling this constructor is the same as calling
60 * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
61 * Otherwise, the effect is the same as calling <code>ScriptEngineManager(null)</code>.
62 *
185 * @throws IllegalArgumentException if key is empty string.
186 */
187 public void put(String key, Object value) {
188 globalScope.put(key, value);
189 }
190
191 /**
192 * Gets the value for the specified key in the Global Scope
193 * @param key The key whose value is to be returned.
194 * @return The value for the specified key.
195 */
196 public Object get(String key) {
197 return globalScope.get(key);
198 }
199
200 /**
201 * Looks up and creates a <code>ScriptEngine</code> for a given name.
202 * The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
203 * registered as a handler for the specified name using the <code>registerEngineName</code>
204 * method.
205 * <br><br> If one is not found, it searches the array of <code>ScriptEngineFactory</code> instances
206 * stored by the constructor for one with the specified name. If a <code>ScriptEngineFactory</code>
207 * is found by either method, it is used to create instance of <code>ScriptEngine</code>.
208 * @param shortName The short name of the <code>ScriptEngine</code> implementation.
209 * returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
210 * @return A <code>ScriptEngine</code> created by the factory located in the search. Returns null
211 * if no such factory was found. The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
212 * <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
213 * created <code>ScriptEngine</code>.
214 * @throws NullPointerException if shortName is null.
215 */
216 public ScriptEngine getEngineByName(String shortName) {
217 if (shortName == null) throw new NullPointerException();
218 //look for registered name first
219 Object obj;
220 if (null != (obj = nameAssociations.get(shortName))) {
221 ScriptEngineFactory spi = (ScriptEngineFactory)obj;
222 try {
223 ScriptEngine engine = spi.getScriptEngine();
224 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
225 return engine;
334 } catch (Exception exp) {
335 if (DEBUG) exp.printStackTrace();
336 }
337 if (types == null) continue;
338 for (String type : types) {
339 if (mimeType.equals(type)) {
340 try {
341 ScriptEngine engine = spi.getScriptEngine();
342 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
343 return engine;
344 } catch (Exception exp) {
345 if (DEBUG) exp.printStackTrace();
346 }
347 }
348 }
349 }
350 return null;
351 }
352
353 /**
354 * Returns an array whose elements are instances of all the <code>ScriptEngineFactory</code> classes
355 * found by the discovery mechanism.
356 * @return List of all discovered <code>ScriptEngineFactory</code>s.
357 */
358 public List<ScriptEngineFactory> getEngineFactories() {
359 List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
360 for (ScriptEngineFactory spi : engineSpis) {
361 res.add(spi);
362 }
363 return Collections.unmodifiableList(res);
364 }
365
366 /**
367 * Registers a <code>ScriptEngineFactory</code> to handle a language
368 * name. Overrides any such association found using the Discovery mechanism.
369 * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
370 * @param factory The class to associate with the given name.
371 * @throws NullPointerException if any of the parameters is null.
372 */
373 public void registerEngineName(String name, ScriptEngineFactory factory) {
374 if (name == null || factory == null) throw new NullPointerException();
|
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.script;
27 import java.util.*;
28 import java.net.URL;
29 import java.io.*;
30 import java.security.*;
31 import sun.misc.Service;
32 import sun.misc.ServiceConfigurationError;
33 import sun.reflect.Reflection;
34 import sun.security.util.SecurityConstants;
35
36 /**
37 * The <code>ScriptEngineManager</code> implements a discovery and instantiation
38 * mechanism for <code>ScriptEngine</code> classes and also maintains a
39 * collection of key/value pairs storing state shared by all engines created
40 * by the Manager. This class uses the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism to enumerate all the
41 * implementations of <code>ScriptEngineFactory</code>. <br><br>
42 * The <code>ScriptEngineManager</code> provides a method to return a list of all these factories
43 * as well as utility methods which look up factories on the basis of language name, file extension
44 * and mime type.
45 * <p>
46 * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope" maintained
47 * by the manager is available to all instances of <code>ScriptEngine</code> created
48 * by the <code>ScriptEngineManager</code>. The values in the <code>Bindings</code> are
49 * generally exposed in all scripts.
50 *
51 * @author Mike Grogan
52 * @author A. Sundararajan
53 * @since 1.6
54 */
55 public class ScriptEngineManager {
56 private static final boolean DEBUG = false;
57 /**
58 * If the thread context ClassLoader can be accessed by the caller,
59 * then the effect of calling this constructor is the same as calling
60 * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
61 * Otherwise, the effect is the same as calling <code>ScriptEngineManager(null)</code>.
62 *
185 * @throws IllegalArgumentException if key is empty string.
186 */
187 public void put(String key, Object value) {
188 globalScope.put(key, value);
189 }
190
191 /**
192 * Gets the value for the specified key in the Global Scope
193 * @param key The key whose value is to be returned.
194 * @return The value for the specified key.
195 */
196 public Object get(String key) {
197 return globalScope.get(key);
198 }
199
200 /**
201 * Looks up and creates a <code>ScriptEngine</code> for a given name.
202 * The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
203 * registered as a handler for the specified name using the <code>registerEngineName</code>
204 * method.
205 * <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
206 * stored by the constructor for one with the specified name. If a <code>ScriptEngineFactory</code>
207 * is found by either method, it is used to create instance of <code>ScriptEngine</code>.
208 * @param shortName The short name of the <code>ScriptEngine</code> implementation.
209 * returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
210 * @return A <code>ScriptEngine</code> created by the factory located in the search. Returns null
211 * if no such factory was found. The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
212 * <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
213 * created <code>ScriptEngine</code>.
214 * @throws NullPointerException if shortName is null.
215 */
216 public ScriptEngine getEngineByName(String shortName) {
217 if (shortName == null) throw new NullPointerException();
218 //look for registered name first
219 Object obj;
220 if (null != (obj = nameAssociations.get(shortName))) {
221 ScriptEngineFactory spi = (ScriptEngineFactory)obj;
222 try {
223 ScriptEngine engine = spi.getScriptEngine();
224 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
225 return engine;
334 } catch (Exception exp) {
335 if (DEBUG) exp.printStackTrace();
336 }
337 if (types == null) continue;
338 for (String type : types) {
339 if (mimeType.equals(type)) {
340 try {
341 ScriptEngine engine = spi.getScriptEngine();
342 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
343 return engine;
344 } catch (Exception exp) {
345 if (DEBUG) exp.printStackTrace();
346 }
347 }
348 }
349 }
350 return null;
351 }
352
353 /**
354 * Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
355 * found by the discovery mechanism.
356 * @return List of all discovered <code>ScriptEngineFactory</code>s.
357 */
358 public List<ScriptEngineFactory> getEngineFactories() {
359 List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
360 for (ScriptEngineFactory spi : engineSpis) {
361 res.add(spi);
362 }
363 return Collections.unmodifiableList(res);
364 }
365
366 /**
367 * Registers a <code>ScriptEngineFactory</code> to handle a language
368 * name. Overrides any such association found using the Discovery mechanism.
369 * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
370 * @param factory The class to associate with the given name.
371 * @throws NullPointerException if any of the parameters is null.
372 */
373 public void registerEngineName(String name, ScriptEngineFactory factory) {
374 if (name == null || factory == null) throw new NullPointerException();
|