8025629: load function should support a way to load scripts from classpath

Mon, 30 Sep 2013 21:33:38 +0530

author
sundar
date
Mon, 30 Sep 2013 21:33:38 +0530
changeset 587
7272ec90f2c6
parent 586
787e36fdf69a
child 588
f5aefbe76cec

8025629: load function should support a way to load scripts from classpath
Reviewed-by: lagergren, hannesw, attila

make/build.xml file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
test/script/trusted/JDK-8025629.js file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/internal/runtime/resources/load_test.js file | annotate | diff | comparison | revisions
     1.1 --- a/make/build.xml	Mon Sep 30 12:06:43 2013 -0300
     1.2 +++ b/make/build.xml	Mon Sep 30 21:33:38 2013 +0530
     1.3 @@ -236,6 +236,10 @@
     1.4         <fileset dir="${test.src.dir}/META-INF/services/"/>
     1.5      </copy>
     1.6  
     1.7 +    <copy todir="${build.test.classes.dir}/jdk/nashorn/internal/runtime/resources">
     1.8 +       <fileset dir="${test.src.dir}/jdk/nashorn/internal/runtime/resources"/>
     1.9 +    </copy>
    1.10 +
    1.11      <!-- tests that check nashorn internals and internal API -->
    1.12      <jar jarfile="${nashorn.internal.tests.jar}">
    1.13        <fileset dir="${build.test.classes.dir}" excludes="**/api/**"/>
    1.14 @@ -245,6 +249,7 @@
    1.15      <jar jarfile="${nashorn.api.tests.jar}">
    1.16        <fileset dir="${build.test.classes.dir}" includes="**/api/**"/>
    1.17        <fileset dir="${build.test.classes.dir}" includes="**/META-INF/**"/>
    1.18 +      <fileset dir="${build.test.classes.dir}" includes="**/resources/*.js"/>
    1.19      </jar>
    1.20  
    1.21    </target>
     2.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Mon Sep 30 12:06:43 2013 -0300
     2.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Mon Sep 30 21:33:38 2013 +0530
     2.3 @@ -91,6 +91,11 @@
     2.4       */
     2.5      public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
     2.6  
     2.7 +    // nashorn load psuedo URL prefixes
     2.8 +    private static final String LOAD_CLASSPATH = "classpath:";
     2.9 +    private static final String LOAD_FX = "fx:";
    2.10 +    private static final String LOAD_NASHORN = "nashorn:";
    2.11 +
    2.12      /* Force DebuggerSupport to be loaded. */
    2.13      static {
    2.14          DebuggerSupport.FORCELOAD = true;
    2.15 @@ -501,21 +506,26 @@
    2.16          // or a ScriptObject that has "name" and "source" (string valued) properties.
    2.17          if (src instanceof String) {
    2.18              final String srcStr = (String)src;
    2.19 -            final File file = new File(srcStr);
    2.20 -            if (srcStr.indexOf(':') != -1) {
    2.21 -                if ((source = loadInternal(srcStr, "nashorn:", "resources/")) == null &&
    2.22 -                    (source = loadInternal(srcStr, "fx:", "resources/fx/")) == null) {
    2.23 -                    URL url;
    2.24 -                    try {
    2.25 -                        //check for malformed url. if malformed, it may still be a valid file
    2.26 -                        url = new URL(srcStr);
    2.27 -                    } catch (final MalformedURLException e) {
    2.28 -                        url = file.toURI().toURL();
    2.29 +            if (srcStr.startsWith(LOAD_CLASSPATH)) {
    2.30 +                URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length()));
    2.31 +                source = (url != null)? new Source(url.toString(), url) : null;
    2.32 +            } else {
    2.33 +                final File file = new File(srcStr);
    2.34 +                if (srcStr.indexOf(':') != -1) {
    2.35 +                    if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null &&
    2.36 +                        (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) {
    2.37 +                        URL url;
    2.38 +                        try {
    2.39 +                            //check for malformed url. if malformed, it may still be a valid file
    2.40 +                            url = new URL(srcStr);
    2.41 +                        } catch (final MalformedURLException e) {
    2.42 +                            url = file.toURI().toURL();
    2.43 +                        }
    2.44 +                        source = new Source(url.toString(), url);
    2.45                      }
    2.46 -                    source = new Source(url.toString(), url);
    2.47 +                } else if (file.isFile()) {
    2.48 +                    source = new Source(srcStr, file);
    2.49                  }
    2.50 -            } else if (file.isFile()) {
    2.51 -                source = new Source(srcStr, file);
    2.52              }
    2.53          } else if (src instanceof File && ((File)src).isFile()) {
    2.54              final File file = (File)src;
    2.55 @@ -803,6 +813,18 @@
    2.56          return Context.getContextTrusted();
    2.57      }
    2.58  
    2.59 +    private URL getResourceURL(final String resName) throws IOException {
    2.60 +        // try the classPathLoader if we have and then
    2.61 +        // try the appLoader if non-null.
    2.62 +        if (classPathLoader != null) {
    2.63 +            return classPathLoader.getResource(resName);
    2.64 +        } else if (appLoader != null) {
    2.65 +            return appLoader.getResource(resName);
    2.66 +        }
    2.67 +
    2.68 +        return null;
    2.69 +    }
    2.70 +
    2.71      private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
    2.72          ScriptFunction script = null;
    2.73  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/trusted/JDK-8025629.js	Mon Sep 30 21:33:38 2013 +0530
     3.3 @@ -0,0 +1,33 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + * 
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + * 
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + * 
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + * 
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/**
    3.28 + * JDK-8025629: load function should support a way to load scripts from classpath
    3.29 + *
    3.30 + * @test
    3.31 + * @run
    3.32 + */
    3.33 +
    3.34 +load("classpath:jdk/nashorn/internal/runtime/resources/load_test.js")
    3.35 +
    3.36 +Assert.assertEquals(loadedFunc("hello"), "HELLO");
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/src/jdk/nashorn/internal/runtime/resources/load_test.js	Mon Sep 30 21:33:38 2013 +0530
     4.3 @@ -0,0 +1,28 @@
     4.4 +/*
     4.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.  Oracle designates this
    4.11 + * particular file as subject to the "Classpath" exception as provided
    4.12 + * by Oracle in the LICENSE file that accompanied this code.
    4.13 + *
    4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.17 + * version 2 for more details (a copy is included in the LICENSE file that
    4.18 + * accompanied this code).
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License version
    4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.23 + *
    4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.25 + * or visit www.oracle.com if you need additional information or have any
    4.26 + * questions.
    4.27 + */
    4.28 +
    4.29 +function loadedFunc(arg) {
    4.30 +    return arg.toUpperCase();
    4.31 +}

mercurial