src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.api.scripting;
  27 
  28 import java.util.Arrays;
  29 import java.util.Collections;
  30 import java.util.List;

  31 import javax.script.ScriptEngine;
  32 import javax.script.ScriptEngineFactory;
  33 import jdk.nashorn.internal.runtime.Context;
  34 import jdk.nashorn.internal.runtime.Version;
  35 
  36 /**
  37  * JSR-223 compliant script engine factory for Nashorn. The engine answers for:
  38  * <ul>
  39  * <li>names {@code "nashorn"}, {@code "Nashorn"}, {@code "js"}, {@code "JS"}, {@code "JavaScript"},
  40  * {@code "javascript"}, {@code "ECMAScript"}, and {@code "ecmascript"};</li>
  41  * <li>MIME types {@code "application/javascript"}, {@code "application/ecmascript"}, {@code "text/javascript"}, and
  42  * {@code "text/ecmascript"};</li>
  43  * <li>as well as for the extension {@code "js"}.</li>
  44  * </ul>
  45  * Programs executing in engines created using {@link #getScriptEngine(String[])} will have the passed arguments
  46  * accessible as a global variable named {@code "arguments"}.
  47  *
  48  * @since 1.8u40
  49  */
  50 @jdk.Exported


 160      * @return newly created script engine.
 161      * @throws SecurityException
 162      *         if the security manager's {@code checkPermission}
 163      *         denies {@code RuntimePermission("nashorn.setConfig")}
 164      */
 165     public ScriptEngine getScriptEngine(final ClassLoader appLoader) {
 166         return newEngine(DEFAULT_OPTIONS, appLoader, null);
 167     }
 168 
 169     /**
 170      * Create a new Script engine initialized by given class filter.
 171      *
 172      * @param classFilter class filter to use.
 173      * @return newly created script engine.
 174      * @throws NullPointerException if {@code classFilter} is {@code null}
 175      * @throws SecurityException
 176      *         if the security manager's {@code checkPermission}
 177      *         denies {@code RuntimePermission("nashorn.setConfig")}
 178      */
 179     public ScriptEngine getScriptEngine(final ClassFilter classFilter) {
 180         classFilter.getClass(); // null check
 181         return newEngine(DEFAULT_OPTIONS, getAppClassLoader(), classFilter);
 182     }
 183 
 184     /**
 185      * Create a new Script engine initialized by given arguments.
 186      *
 187      * @param args arguments array passed to script engine.
 188      * @return newly created script engine.
 189      * @throws NullPointerException if {@code args} is {@code null}
 190      * @throws SecurityException
 191      *         if the security manager's {@code checkPermission}
 192      *         denies {@code RuntimePermission("nashorn.setConfig")}
 193      */
 194     public ScriptEngine getScriptEngine(final String... args) {
 195         args.getClass(); // null check
 196         return newEngine(args, getAppClassLoader(), null);
 197     }
 198 
 199     /**
 200      * Create a new Script engine initialized by given arguments.
 201      *
 202      * @param args arguments array passed to script engine.
 203      * @param appLoader class loader to be used as script "app" class loader.
 204      * @return newly created script engine.
 205      * @throws NullPointerException if {@code args} is {@code null}
 206      * @throws SecurityException
 207      *         if the security manager's {@code checkPermission}
 208      *         denies {@code RuntimePermission("nashorn.setConfig")}
 209      */
 210     public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader) {
 211         args.getClass(); // null check
 212         return newEngine(args, appLoader, null);
 213     }
 214 
 215     /**
 216      * Create a new Script engine initialized by given arguments.
 217      *
 218      * @param args arguments array passed to script engine.
 219      * @param appLoader class loader to be used as script "app" class loader.
 220      * @param classFilter class filter to use.
 221      * @return newly created script engine.
 222      * @throws NullPointerException if {@code args} or {@code classFilter} is {@code null}
 223      * @throws SecurityException
 224      *         if the security manager's {@code checkPermission}
 225      *         denies {@code RuntimePermission("nashorn.setConfig")}
 226      */
 227     public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader, final ClassFilter classFilter) {
 228         args.getClass(); // null check
 229         classFilter.getClass(); // null check
 230         return newEngine(args, appLoader, classFilter);
 231     }
 232 
 233     private ScriptEngine newEngine(final String[] args, final ClassLoader appLoader, final ClassFilter classFilter) {
 234         checkConfigPermission();
 235         try {
 236             return new NashornScriptEngine(this, args, appLoader, classFilter);
 237         } catch (final RuntimeException e) {
 238             if (Context.DEBUG) {
 239                 e.printStackTrace();
 240             }
 241             throw e;
 242         }
 243     }
 244 
 245     // -- Internals only below this point
 246 
 247     private static void checkConfigPermission() {
 248         final SecurityManager sm = System.getSecurityManager();
 249         if (sm != null) {




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.api.scripting;
  27 
  28 import java.util.Arrays;
  29 import java.util.Collections;
  30 import java.util.List;
  31 import java.util.Objects;
  32 import javax.script.ScriptEngine;
  33 import javax.script.ScriptEngineFactory;
  34 import jdk.nashorn.internal.runtime.Context;
  35 import jdk.nashorn.internal.runtime.Version;
  36 
  37 /**
  38  * JSR-223 compliant script engine factory for Nashorn. The engine answers for:
  39  * <ul>
  40  * <li>names {@code "nashorn"}, {@code "Nashorn"}, {@code "js"}, {@code "JS"}, {@code "JavaScript"},
  41  * {@code "javascript"}, {@code "ECMAScript"}, and {@code "ecmascript"};</li>
  42  * <li>MIME types {@code "application/javascript"}, {@code "application/ecmascript"}, {@code "text/javascript"}, and
  43  * {@code "text/ecmascript"};</li>
  44  * <li>as well as for the extension {@code "js"}.</li>
  45  * </ul>
  46  * Programs executing in engines created using {@link #getScriptEngine(String[])} will have the passed arguments
  47  * accessible as a global variable named {@code "arguments"}.
  48  *
  49  * @since 1.8u40
  50  */
  51 @jdk.Exported


 161      * @return newly created script engine.
 162      * @throws SecurityException
 163      *         if the security manager's {@code checkPermission}
 164      *         denies {@code RuntimePermission("nashorn.setConfig")}
 165      */
 166     public ScriptEngine getScriptEngine(final ClassLoader appLoader) {
 167         return newEngine(DEFAULT_OPTIONS, appLoader, null);
 168     }
 169 
 170     /**
 171      * Create a new Script engine initialized by given class filter.
 172      *
 173      * @param classFilter class filter to use.
 174      * @return newly created script engine.
 175      * @throws NullPointerException if {@code classFilter} is {@code null}
 176      * @throws SecurityException
 177      *         if the security manager's {@code checkPermission}
 178      *         denies {@code RuntimePermission("nashorn.setConfig")}
 179      */
 180     public ScriptEngine getScriptEngine(final ClassFilter classFilter) {
 181         Objects.requireNonNull(classFilter);
 182         return newEngine(DEFAULT_OPTIONS, getAppClassLoader(), classFilter);
 183     }
 184 
 185     /**
 186      * Create a new Script engine initialized by given arguments.
 187      *
 188      * @param args arguments array passed to script engine.
 189      * @return newly created script engine.
 190      * @throws NullPointerException if {@code args} is {@code null}
 191      * @throws SecurityException
 192      *         if the security manager's {@code checkPermission}
 193      *         denies {@code RuntimePermission("nashorn.setConfig")}
 194      */
 195     public ScriptEngine getScriptEngine(final String... args) {
 196         Objects.requireNonNull(args);
 197         return newEngine(args, getAppClassLoader(), null);
 198     }
 199 
 200     /**
 201      * Create a new Script engine initialized by given arguments.
 202      *
 203      * @param args arguments array passed to script engine.
 204      * @param appLoader class loader to be used as script "app" class loader.
 205      * @return newly created script engine.
 206      * @throws NullPointerException if {@code args} is {@code null}
 207      * @throws SecurityException
 208      *         if the security manager's {@code checkPermission}
 209      *         denies {@code RuntimePermission("nashorn.setConfig")}
 210      */
 211     public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader) {
 212         Objects.requireNonNull(args);
 213         return newEngine(args, appLoader, null);
 214     }
 215 
 216     /**
 217      * Create a new Script engine initialized by given arguments.
 218      *
 219      * @param args arguments array passed to script engine.
 220      * @param appLoader class loader to be used as script "app" class loader.
 221      * @param classFilter class filter to use.
 222      * @return newly created script engine.
 223      * @throws NullPointerException if {@code args} or {@code classFilter} is {@code null}
 224      * @throws SecurityException
 225      *         if the security manager's {@code checkPermission}
 226      *         denies {@code RuntimePermission("nashorn.setConfig")}
 227      */
 228     public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader, final ClassFilter classFilter) {
 229         Objects.requireNonNull(args);
 230         Objects.requireNonNull(classFilter);
 231         return newEngine(args, appLoader, classFilter);
 232     }
 233 
 234     private ScriptEngine newEngine(final String[] args, final ClassLoader appLoader, final ClassFilter classFilter) {
 235         checkConfigPermission();
 236         try {
 237             return new NashornScriptEngine(this, args, appLoader, classFilter);
 238         } catch (final RuntimeException e) {
 239             if (Context.DEBUG) {
 240                 e.printStackTrace();
 241             }
 242             throw e;
 243         }
 244     }
 245 
 246     // -- Internals only below this point
 247 
 248     private static void checkConfigPermission() {
 249         final SecurityManager sm = System.getSecurityManager();
 250         if (sm != null) {