1 7 8 package javax.script; 9 import java.util.*; 10 import java.net.URL ; 11 import java.io.*; 12 import java.security.*; 13 import sun.misc.Service; 14 import sun.misc.ServiceConfigurationError; 15 import sun.reflect.Reflection; 16 import sun.security.util.SecurityConstants; 17 18 37 public class ScriptEngineManager { 38 private static final boolean DEBUG = false; 39 47 public ScriptEngineManager() { 48 ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader(); 49 if (canCallerAccessLoader(ctxtLoader)) { 50 if (DEBUG) System.out.println("using " + ctxtLoader); 51 init(ctxtLoader); 52 } else { 53 if (DEBUG) System.out.println("using bootstrap loader"); 54 init(null); 55 } 56 } 57 58 68 public ScriptEngineManager(ClassLoader loader) { 69 init(loader); 70 } 71 72 private void init(final ClassLoader loader) { 73 globalScope = new SimpleBindings(); 74 engineSpis = new HashSet<ScriptEngineFactory>(); 75 nameAssociations = new HashMap<String , ScriptEngineFactory>(); 76 extensionAssociations = new HashMap<String , ScriptEngineFactory>(); 77 mimeTypeAssociations = new HashMap<String , ScriptEngineFactory>(); 78 AccessController.doPrivileged(new PrivilegedAction() { 79 public Object run() { 80 initEngines(loader); 81 return null; 82 } 83 }); 84 } 85 86 private void initEngines(final ClassLoader loader) { 87 Iterator itr = null; 88 try { 89 if (loader != null) { 90 itr = Service.providers(ScriptEngineFactory.class, loader); 91 } else { 92 itr = Service.installedProviders(ScriptEngineFactory.class); 93 } 94 } catch (ServiceConfigurationError err) { 95 System.err.println("Can't find ScriptEngineFactory providers: " + 96 err.getMessage()); 97 if (DEBUG) { 98 err.printStackTrace(); 99 } 100 return; 104 } 105 106 try { 107 while (itr.hasNext()) { 108 try { 109 ScriptEngineFactory fact = (ScriptEngineFactory) itr.next(); 110 engineSpis.add(fact); 111 } catch (ServiceConfigurationError err) { 112 System.err.println("ScriptEngineManager providers.next(): " 113 + err.getMessage()); 114 if (DEBUG) { 115 err.printStackTrace(); 116 } 117 continue; 119 } 120 } 121 } catch (ServiceConfigurationError err) { 122 System.err.println("ScriptEngineManager providers.hasNext(): " 123 + err.getMessage()); 124 if (DEBUG) { 125 err.printStackTrace(); 126 } 127 return; 131 } 132 } 133 134 143 public void setBindings(Bindings bindings) { 144 if (bindings == null) { 145 throw new IllegalArgumentException ("Global scope cannot be null."); 146 } 147 148 globalScope = bindings; 149 } 150 151 158 public Bindings getBindings() { 159 return globalScope; 160 } 161 162 169 public void put(String key, Object value) { 170 globalScope.put(key, value); 171 } 172 173 178 public Object get(String key) { 179 return globalScope.get(key); 180 } 181 182 198 public ScriptEngine getEngineByName(String shortName) { 199 if (shortName == null) throw new NullPointerException (); 200 Object obj; 202 if (null != (obj = nameAssociations.get(shortName))) { 203 ScriptEngineFactory spi = (ScriptEngineFactory)obj; 204 try { 205 ScriptEngine engine = spi.getScriptEngine(); 206 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); 207 return engine; 208 } catch (Exception exp) { 209 if (DEBUG) exp.printStackTrace(); 210 } 211 } 212 213 for (ScriptEngineFactory spi : engineSpis) { 214 List<String > names = null; 215 try { 216 names = spi.getNames(); 217 } catch (Exception exp) { 218 if (DEBUG) exp.printStackTrace(); 219 } 220 221 if (names != null) { 222 for (String name : names) { 223 if (shortName.equals(name)) { 224 try { 225 ScriptEngine engine = spi.getScriptEngine(); 226 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); 227 return engine; 228 } catch (Exception exp) { 229 if (DEBUG) exp.printStackTrace(); 230 } 231 } 232 } 233 } 234 } 235 236 return null; 237 } 238 239 249 public ScriptEngine getEngineByExtension(String extension) { 250 if (extension == null) throw new NullPointerException (); 251 Object obj; 253 if (null != (obj = extensionAssociations.get(extension))) { 254 ScriptEngineFactory spi = (ScriptEngineFactory)obj; 255 try { 256 ScriptEngine engine = spi.getScriptEngine(); 257 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); 258 return engine; 259 } catch (Exception exp) { 260 if (DEBUG) exp.printStackTrace(); 261 } 262 } 263 264 for (ScriptEngineFactory spi : engineSpis) { 265 List<String > exts = null; 266 try { 267 exts = spi.getExtensions(); 268 } catch (Exception exp) { 269 if (DEBUG) exp.printStackTrace(); 270 } 271 if (exts == null) continue; 272 for (String ext : exts) { 273 if (extension.equals(ext)) { 274 try { 275 ScriptEngine engine = spi.getScriptEngine(); 276 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); 277 return engine; 278 } catch (Exception exp) { 279 if (DEBUG) exp.printStackTrace(); 280 } 281 } 282 } 283 } 284 return null; 285 } 286 287 297 public ScriptEngine getEngineByMimeType(String mimeType) { 298 if (mimeType == null) throw new NullPointerException (); 299 Object obj; 301 if (null != (obj = mimeTypeAssociations.get(mimeType))) { 302 ScriptEngineFactory spi = (ScriptEngineFactory)obj; 303 try { 304 ScriptEngine engine = spi.getScriptEngine(); 305 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); 306 return engine; 307 } catch (Exception exp) { 308 if (DEBUG) exp.printStackTrace(); 309 } 310 } 311 312 for (ScriptEngineFactory spi : engineSpis) { 313 List<String > types = null; 314 try { 315 types = spi.getMimeTypes(); 316 } catch (Exception exp) { 317 if (DEBUG) exp.printStackTrace(); 318 } 319 if (types == null) continue; 320 for (String type : types) { 321 if (mimeType.equals(type)) { 322 try { 323 ScriptEngine engine = spi.getScriptEngine(); 324 engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); 325 return engine; 326 } catch (Exception exp) { 327 if (DEBUG) exp.printStackTrace(); 328 } 329 } 330 } 331 } 332 return null; 333 } 334 335 340 public List<ScriptEngineFactory> getEngineFactories() { 341 List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size()); 342 for (ScriptEngineFactory spi : engineSpis) { 343 res.add(spi); 344 } 345 return Collections.unmodifiableList(res); 346 } 347 348 355 public void registerEngineName(String name, ScriptEngineFactory factory) { 356 if (name == null || factory == null) throw new NullPointerException (); 357 nameAssociations.put(name, factory); 358 } 359 360 370 public void registerEngineMimeType(String type, ScriptEngineFactory factory) { 371 if (type == null || factory == null) throw new NullPointerException (); 372 mimeTypeAssociations.put(type, factory); 373 } 374 375 384 public void registerEngineExtension(String extension, ScriptEngineFactory factory) { 385 if (extension == null || factory == null) throw new NullPointerException (); 386 extensionAssociations.put(extension, factory); 387 } 388 389 390 private HashSet<ScriptEngineFactory> engineSpis; 391 392 393 private HashMap<String , ScriptEngineFactory> nameAssociations; 394 395 396 private HashMap<String , ScriptEngineFactory> extensionAssociations; 397 398 399 private HashMap<String , ScriptEngineFactory> mimeTypeAssociations; 400 401 402 private Bindings globalScope; 403 404 private boolean canCallerAccessLoader(ClassLoader loader) { 405 SecurityManager sm = System.getSecurityManager(); 406 if (sm != null) { 407 ClassLoader callerLoader = getCallerClassLoader(); 408 if (callerLoader != null) { 409 if (loader != callerLoader || !isAncestor(loader, callerLoader)) { 410 try { 411 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); 412 } catch (SecurityException exp) { 413 if (DEBUG) exp.printStackTrace(); 414 return false; 415 } 416 } } } 420 return true; 421 } 422 423 private ClassLoader getCallerClassLoader() { 426 Class caller = Reflection.getCallerClass(3); 427 if (caller == null) { 428 return null; 429 } 430 return caller.getClassLoader(); 431 } 432 433 private boolean isAncestor(ClassLoader cl1, ClassLoader cl2) { 435 do { 436 cl2 = cl2.getParent(); 437 if (cl1 == cl2) return true; 438 } while (cl2 != null); 439 return false; 440 } 441 } 442 | Popular Tags |