nashorn/src/jdk/nashorn/internal/runtime/Context.java
changeset 16226 0e4f37e6cc40
parent 16225 81d58c2b9fcf
child 16230 c38c724d82e7
equal deleted inserted replaced
16225:81d58c2b9fcf 16226:0e4f37e6cc40
    34 import java.io.File;
    34 import java.io.File;
    35 import java.io.IOException;
    35 import java.io.IOException;
    36 import java.io.PrintWriter;
    36 import java.io.PrintWriter;
    37 import java.lang.invoke.MethodHandle;
    37 import java.lang.invoke.MethodHandle;
    38 import java.lang.invoke.MethodHandles;
    38 import java.lang.invoke.MethodHandles;
       
    39 import java.lang.reflect.Constructor;
    39 import java.net.MalformedURLException;
    40 import java.net.MalformedURLException;
    40 import java.net.URL;
    41 import java.net.URL;
    41 import java.security.AccessController;
    42 import java.security.AccessController;
    42 import java.security.CodeSigner;
    43 import java.security.CodeSigner;
    43 import java.security.CodeSource;
    44 import java.security.CodeSource;
    82         if (sm != null) {
    83         if (sm != null) {
    83             // skip getCallerClass and getGlobal and get to the real caller
    84             // skip getCallerClass and getGlobal and get to the real caller
    84             Class<?> caller = Reflection.getCallerClass(2);
    85             Class<?> caller = Reflection.getCallerClass(2);
    85             ClassLoader callerLoader = caller.getClassLoader();
    86             ClassLoader callerLoader = caller.getClassLoader();
    86 
    87 
    87             // Allow this method only for nashorn's own classes, script
    88             // Allow this method only for nashorn's own classes, objects
    88             // generated classes and Java adapter classes. Rest should
    89             // package classes and Java adapter classes. Rest should
    89             // have the necessary security permission.
    90             // have the necessary security permission.
    90             if (callerLoader != myLoader &&
    91             if (callerLoader != myLoader &&
    91                 !(callerLoader instanceof NashornLoader) &&
    92                 !(callerLoader instanceof StructureLoader) &&
    92                 !(JavaAdapterFactory.isAdapterClass(caller))) {
    93                 !(JavaAdapterFactory.isAdapterClass(caller))) {
    93                 sm.checkPermission(new RuntimePermission("getNashornGlobal"));
    94                 sm.checkPermission(new RuntimePermission("getNashornGlobal"));
    94             }
    95             }
    95         }
    96         }
    96 
    97 
   330         this.appLoader = appLoader;
   331         this.appLoader = appLoader;
   331         this.scriptLoader = (ScriptLoader)AccessController.doPrivileged(
   332         this.scriptLoader = (ScriptLoader)AccessController.doPrivileged(
   332              new PrivilegedAction<ClassLoader>() {
   333              new PrivilegedAction<ClassLoader>() {
   333                 @Override
   334                 @Override
   334                 public ClassLoader run() {
   335                 public ClassLoader run() {
   335                     final ClassLoader structureLoader = new StructureLoader(sharedLoader, Context.this);
   336                     final StructureLoader structureLoader = new StructureLoader(sharedLoader, Context.this);
   336                     return new ScriptLoader(structureLoader, Context.this);
   337                     return new ScriptLoader(structureLoader, Context.this);
   337                 }
   338                 }
   338              });
   339              });
   339 
   340 
   340         this.namespace = new Namespace();
   341         this.namespace = new Namespace();
   957                 @Override
   958                 @Override
   958                 public ScriptLoader run() {
   959                 public ScriptLoader run() {
   959                     // Generated code won't refer to any class generated by context
   960                     // Generated code won't refer to any class generated by context
   960                     // script loader and so parent loader can be the structure
   961                     // script loader and so parent loader can be the structure
   961                     // loader -- which is parent of the context script loader.
   962                     // loader -- which is parent of the context script loader.
   962                     return new ScriptLoader(scriptLoader.getParent(), Context.this);
   963                     return new ScriptLoader((StructureLoader)scriptLoader.getParent(), Context.this);
   963                 }
   964                 }
   964              });
   965              });
   965     }
   966     }
   966 
   967 
   967     private ScriptObject newGlobalTrusted() {
   968     private ScriptObject newGlobalTrusted() {
   968         try {
   969         try {
   969             final Class<?> clazz = Class.forName("jdk.nashorn.internal.objects.Global", true, scriptLoader);
   970             final Class<?> clazz = Class.forName("jdk.nashorn.internal.objects.Global", true, scriptLoader);
   970             return (ScriptObject) clazz.newInstance();
   971             final Constructor cstr = clazz.getConstructor(Context.class);
   971         } catch (final ClassNotFoundException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException e) {
   972             return (ScriptObject) cstr.newInstance(this);
       
   973         } catch (final Exception e) {
   972             printStackTrace(e);
   974             printStackTrace(e);
   973             throw new RuntimeException(e);
   975             if (e instanceof RuntimeException) {
       
   976                 throw (RuntimeException)e;
       
   977             } else {
       
   978                 throw new RuntimeException(e);
       
   979             }
   974         }
   980         }
   975     }
   981     }
   976 }
   982 }