505 |
505 |
506 /** is this context in strict mode? Cached from env. as this is used heavily. */ |
506 /** is this context in strict mode? Cached from env. as this is used heavily. */ |
507 final boolean _strict; |
507 final boolean _strict; |
508 |
508 |
509 /** class loader to resolve classes from script. */ |
509 /** class loader to resolve classes from script. */ |
510 private final ClassLoader appLoader; |
510 private final ClassLoader appLoader; |
511 |
|
512 /** Class loader to load classes from -classpath option, if set. */ |
|
513 private final ClassLoader classPathLoader; |
|
514 |
511 |
515 /** Class loader to load classes compiled from scripts. */ |
512 /** Class loader to load classes compiled from scripts. */ |
516 private final ScriptLoader scriptLoader; |
513 private final ScriptLoader scriptLoader; |
517 |
514 |
518 /** Current error manager. */ |
515 /** Current error manager. */ |
624 } |
621 } |
625 |
622 |
626 this.classFilter = classFilter; |
623 this.classFilter = classFilter; |
627 this.env = new ScriptEnvironment(options, out, err); |
624 this.env = new ScriptEnvironment(options, out, err); |
628 this._strict = env._strict; |
625 this._strict = env._strict; |
629 this.appLoader = appLoader; |
|
630 if (env._loader_per_compile) { |
626 if (env._loader_per_compile) { |
631 this.scriptLoader = null; |
627 this.scriptLoader = null; |
632 this.uniqueScriptId = null; |
628 this.uniqueScriptId = null; |
633 } else { |
629 } else { |
634 this.scriptLoader = createNewLoader(); |
630 this.scriptLoader = createNewLoader(); |
635 this.uniqueScriptId = new AtomicLong(); |
631 this.uniqueScriptId = new AtomicLong(); |
636 } |
632 } |
637 this.errors = errors; |
633 this.errors = errors; |
638 |
634 |
639 // if user passed -classpath option, make a class loader with that and set it as |
635 // if user passed -classpath option, make a URLClassLoader with that and |
640 // thread context class loader so that script can access classes from that path. |
636 // the app loader as the parent. |
641 final String classPath = options.getString("classpath"); |
637 final String classPath = options.getString("classpath"); |
642 if (!env._compile_only && classPath != null && !classPath.isEmpty()) { |
638 if (!env._compile_only && classPath != null && !classPath.isEmpty()) { |
643 // make sure that caller can create a class loader. |
639 // make sure that caller can create a class loader. |
644 if (sm != null) { |
640 if (sm != null) { |
645 sm.checkPermission(new RuntimePermission("createClassLoader")); |
641 sm.checkCreateClassLoader(); |
646 } |
642 } |
647 this.classPathLoader = NashornLoader.createClassLoader(classPath); |
643 this.appLoader = NashornLoader.createClassLoader(classPath, appLoader); |
648 } else { |
644 } else { |
649 this.classPathLoader = null; |
645 this.appLoader = appLoader; |
650 } |
646 } |
651 |
647 |
652 final int cacheSize = env._class_cache_size; |
648 final int cacheSize = env._class_cache_size; |
653 if (cacheSize > 0) { |
649 if (cacheSize > 0) { |
654 classCache = new ClassCache(this, cacheSize); |
650 classCache = new ClassCache(this, cacheSize); |
1179 final SecurityManager sm = System.getSecurityManager(); |
1175 final SecurityManager sm = System.getSecurityManager(); |
1180 if (sm != null) { |
1176 if (sm != null) { |
1181 checkPackageAccess(sm, fullName); |
1177 checkPackageAccess(sm, fullName); |
1182 } |
1178 } |
1183 |
1179 |
1184 // try the script -classpath loader, if that is set |
|
1185 if (classPathLoader != null) { |
|
1186 try { |
|
1187 return Class.forName(fullName, true, classPathLoader); |
|
1188 } catch (final ClassNotFoundException ignored) { |
|
1189 // ignore, continue search |
|
1190 } |
|
1191 } |
|
1192 |
|
1193 // Try finding using the "app" loader. |
1180 // Try finding using the "app" loader. |
1194 return Class.forName(fullName, true, appLoader); |
1181 return Class.forName(fullName, true, appLoader); |
1195 } |
1182 } |
1196 |
1183 |
1197 /** |
1184 /** |
1336 |
1323 |
1337 return Context.getContextTrusted(); |
1324 return Context.getContextTrusted(); |
1338 } |
1325 } |
1339 |
1326 |
1340 private URL getResourceURL(final String resName) { |
1327 private URL getResourceURL(final String resName) { |
1341 // try the classPathLoader if we have and then |
1328 if (appLoader != null) { |
1342 // try the appLoader if non-null. |
|
1343 if (classPathLoader != null) { |
|
1344 return classPathLoader.getResource(resName); |
|
1345 } else if (appLoader != null) { |
|
1346 return appLoader.getResource(resName); |
1329 return appLoader.getResource(resName); |
1347 } |
1330 } |
1348 |
1331 return ClassLoader.getSystemResource(resName); |
1349 return null; |
|
1350 } |
1332 } |
1351 |
1333 |
1352 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) { |
1334 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) { |
1353 ScriptFunction script = null; |
1335 ScriptFunction script = null; |
1354 |
1336 |