jdk/src/share/classes/javax/script/ScriptEngineManager.java
changeset 16906 44dfee24cb71
parent 14342 8435a30053c1
child 18233 1b37223926e5
equal deleted inserted replaced
16905:0419f45c7761 16906:44dfee24cb71
    26 package javax.script;
    26 package javax.script;
    27 import java.util.*;
    27 import java.util.*;
    28 import java.security.*;
    28 import java.security.*;
    29 import java.util.ServiceLoader;
    29 import java.util.ServiceLoader;
    30 import java.util.ServiceConfigurationError;
    30 import java.util.ServiceConfigurationError;
       
    31 import sun.reflect.CallerSensitive;
    31 import sun.reflect.Reflection;
    32 import sun.reflect.Reflection;
    32 import sun.security.util.SecurityConstants;
    33 import sun.security.util.SecurityConstants;
    33 
    34 
    34 /**
    35 /**
    35  * The <code>ScriptEngineManager</code> implements a discovery and instantiation
    36  * The <code>ScriptEngineManager</code> implements a discovery and instantiation
    58      * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
    59      * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
    59      * Otherwise, the effect is the same as calling <code>ScriptEngineManager(null)</code>.
    60      * Otherwise, the effect is the same as calling <code>ScriptEngineManager(null)</code>.
    60      *
    61      *
    61      * @see java.lang.Thread#getContextClassLoader
    62      * @see java.lang.Thread#getContextClassLoader
    62      */
    63      */
       
    64     @CallerSensitive
    63     public ScriptEngineManager() {
    65     public ScriptEngineManager() {
    64         ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
    66         ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
    65         if (canCallerAccessLoader(ctxtLoader)) {
    67         if (canCallerAccessLoader(ctxtLoader, Reflection.getCallerClass())) {
    66             if (DEBUG) System.out.println("using " + ctxtLoader);
    68             if (DEBUG) System.out.println("using " + ctxtLoader);
    67             init(ctxtLoader);
    69             init(ctxtLoader);
    68         } else {
    70         } else {
    69             if (DEBUG) System.out.println("using bootstrap loader");
    71             if (DEBUG) System.out.println("using bootstrap loader");
    70             init(null);
    72             init(null);
   417     private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
   419     private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
   418 
   420 
   419     /** Global bindings associated with script engines created by this manager. */
   421     /** Global bindings associated with script engines created by this manager. */
   420     private Bindings globalScope;
   422     private Bindings globalScope;
   421 
   423 
   422     private boolean canCallerAccessLoader(ClassLoader loader) {
   424     private boolean canCallerAccessLoader(ClassLoader loader, Class<?> caller) {
   423         SecurityManager sm = System.getSecurityManager();
   425         SecurityManager sm = System.getSecurityManager();
   424         if (sm != null) {
   426         if (sm != null) {
   425             ClassLoader callerLoader = getCallerClassLoader();
   427             ClassLoader callerLoader = getClassLoader(caller);
   426             if (!sun.misc.VM.isSystemDomainLoader(callerLoader)) {
   428             if (!sun.misc.VM.isSystemDomainLoader(callerLoader)) {
   427                 if (loader != callerLoader || !isAncestor(loader, callerLoader)) {
   429                 if (loader != callerLoader || !isAncestor(loader, callerLoader)) {
   428                     try {
   430                     try {
   429                         sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
   431                         sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
   430                     } catch (SecurityException exp) {
   432                     } catch (SecurityException exp) {
   436         } // else fallthru..
   438         } // else fallthru..
   437 
   439 
   438         return true;
   440         return true;
   439     }
   441     }
   440 
   442 
   441     // Note that this code is same as ClassLoader.getCallerClassLoader().
   443     // Note that this code is same as ClassLoader.getClassLoader().
   442     // But, that method is package private and hence we can't call here.
   444     // But, that method is package private and hence we can't call here.
   443     private ClassLoader getCallerClassLoader() {
   445     private ClassLoader getClassLoader(Class<?> caller) {
   444         Class<?> caller = Reflection.getCallerClass(3);
       
   445         if (caller == null) {
   446         if (caller == null) {
   446             return null;
   447             return null;
   447         }
   448         }
   448         return caller.getClassLoader();
   449         return caller.getClassLoader();
   449     }
   450     }