nashorn/src/jdk/nashorn/internal/runtime/Context.java
changeset 16251 4a5d15b2f168
parent 16245 6a1c6c8bc113
child 16254 2ed824fc93be
equal deleted inserted replaced
16250:96a7a6218ad3 16251:4a5d15b2f168
    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.lang.reflect.Constructor;
    40 import java.net.MalformedURLException;
       
    41 import java.net.URL;
    40 import java.net.URL;
    42 import java.security.AccessController;
    41 import java.security.AccessController;
    43 import java.security.CodeSigner;
    42 import java.security.CodeSigner;
    44 import java.security.CodeSource;
    43 import java.security.CodeSource;
    45 import java.security.PrivilegedAction;
    44 import java.security.PrivilegedAction;
   618         Source source = null;
   617         Source source = null;
   619 
   618 
   620         // load accepts a String (which could be a URL or a file name), a File, a URL
   619         // load accepts a String (which could be a URL or a file name), a File, a URL
   621         // or a ScriptObject that has "name" and "source" (string valued) properties.
   620         // or a ScriptObject that has "name" and "source" (string valued) properties.
   622         if (src instanceof String) {
   621         if (src instanceof String) {
   623             String srcStr = (String)src;
   622             final String srcStr = (String)src;
   624             final File file = new File((String)src);
   623             final File   file   = new File(srcStr);
   625             if (srcStr.indexOf(':') != -1) {
   624             if (srcStr.indexOf(':') != -1) {
   626                 try {
   625                 if (srcStr.startsWith("nashorn:")) {
   627                     final URL url = new URL((String)src);
   626                     final String resource = "resources/" + srcStr.substring("nashorn:".length());
       
   627                     // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
       
   628                     // These scripts are always available and are loaded from nashorn.jar's resources.
       
   629                     source = AccessController.doPrivileged(
       
   630                             new PrivilegedAction<Source>() {
       
   631                                 @Override
       
   632                                 public Source run() {
       
   633                                     try {
       
   634                                         final URL resURL = Context.class.getResource(resource);
       
   635                                         return (resURL != null)? new Source(srcStr, resURL) : null;
       
   636                                     } catch (final IOException exp) {
       
   637                                         return null;
       
   638                                     }
       
   639                                 }
       
   640                             });
       
   641                 } else {
       
   642                     final URL url = file.toURI().toURL();
   628                     source = new Source(url.toString(), url);
   643                     source = new Source(url.toString(), url);
   629                 } catch (final MalformedURLException e) {
       
   630                     // fallback URL - nashorn:foo.js - check under jdk/nashorn/internal/runtime/resources
       
   631                     final String str = (String)src;
       
   632                     if (str.startsWith("nashorn:")) {
       
   633                         final String resource = "resources/" + str.substring("nashorn:".length());
       
   634                         // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
       
   635                         // These scripts are always available and are loaded from nashorn.jar's resources.
       
   636                         source = AccessController.doPrivileged(
       
   637                                 new PrivilegedAction<Source>() {
       
   638                                     @Override
       
   639                                     public Source run() {
       
   640                                         try {
       
   641                                             final URL resURL = Context.class.getResource(resource);
       
   642                                             return (resURL != null)? new Source(str, resURL) : null;
       
   643                                         } catch (final IOException exp) {
       
   644                                             return null;
       
   645                                         }
       
   646                                     }
       
   647                                 });
       
   648                     } else {
       
   649                         throw e;
       
   650                     }
       
   651                 }
   644                 }
   652             } else if (file.isFile()) {
   645             } else if (file.isFile()) {
   653                 source = new Source(srcStr, file);
   646                 source = new Source(srcStr, file);
   654             }
   647             }
   655         } else if (src instanceof File && ((File)src).isFile()) {
   648         } else if (src instanceof File && ((File)src).isFile()) {