ScriptLoader.java revision 1880:c84e9bd100e9
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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
26package jdk.nashorn.internal.runtime;
27
28import java.lang.module.ModuleDescriptor;
29import java.lang.module.ModuleDescriptor.Modifier;
30import java.lang.reflect.Module;
31import java.security.CodeSource;
32import java.util.Objects;
33import java.util.Set;
34
35/**
36 * Responsible for loading script generated classes.
37 *
38 */
39final class ScriptLoader extends NashornLoader {
40    private static final String NASHORN_PKG_PREFIX = "jdk.nashorn.internal.";
41
42    private volatile boolean structureAccessAdded;
43    private final Context context;
44    private final Module scriptModule;
45
46    /*package-private*/ Context getContext() {
47        return context;
48    }
49
50    /**
51     * Constructor.
52     */
53    ScriptLoader(final Context context) {
54        super(context.getStructLoader());
55        this.context = context;
56
57        // new scripts module, it's specific exports and read-edges
58        scriptModule = createModule("jdk.scripting.nashorn.scripts");
59
60        // specific exports from nashorn to new scripts module
61        NASHORN_MODULE.addExports(OBJECTS_PKG, scriptModule);
62        NASHORN_MODULE.addExports(RUNTIME_PKG, scriptModule);
63        NASHORN_MODULE.addExports(RUNTIME_ARRAYS_PKG, scriptModule);
64        NASHORN_MODULE.addExports(RUNTIME_LINKER_PKG, scriptModule);
65        NASHORN_MODULE.addExports(SCRIPTS_PKG, scriptModule);
66
67        // nashorn needs to read scripts module methods,fields
68        NASHORN_MODULE.addReads(scriptModule);
69    }
70
71    private Module createModule(final String moduleName) {
72        final Module structMod = context.getStructLoader().getModule();
73        final ModuleDescriptor.Builder builder =
74            ModuleDescriptor.newModule(moduleName, Set.of(Modifier.SYNTHETIC))
75                    .requires("java.logging")
76                    .requires(NASHORN_MODULE.getName())
77                    .requires(structMod.getName())
78                    .packages(Set.of(SCRIPTS_PKG));
79
80        if (Context.javaSqlFound) {
81            builder.requires("java.sql");
82        }
83
84        if (Context.javaSqlRowsetFound) {
85            builder.requires("java.sql.rowset");
86        }
87
88        final ModuleDescriptor descriptor = builder.build();
89
90        final Module mod = Context.createModuleTrusted(structMod.getLayer(), descriptor, this);
91        loadModuleManipulator();
92        return mod;
93    }
94
95    @Override
96    protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
97        checkPackageAccess(name);
98        final Class<?> cl = super.loadClass(name, resolve);
99        if (!structureAccessAdded) {
100            final StructureLoader structLoader = context.getStructLoader();
101            if (cl.getClassLoader() == structLoader) {
102                structureAccessAdded = true;
103                structLoader.addModuleExport(scriptModule);
104            }
105        }
106        return cl;
107    }
108
109    @Override
110    protected Class<?> findClass(final String name) throws ClassNotFoundException {
111        final ClassLoader appLoader = context.getAppLoader();
112
113        /*
114         * If the appLoader is null, don't bother side-delegating to it!
115         * Bootloader has been already attempted via parent loader
116         * delegation from the "loadClass" method.
117         *
118         * Also, make sure that we don't delegate to the app loader
119         * for nashorn's own classes or nashorn generated classes!
120         */
121        if (appLoader == null || name.startsWith(NASHORN_PKG_PREFIX)) {
122            throw new ClassNotFoundException(name);
123        }
124
125        /*
126         * This split-delegation is used so that caller loader
127         * based resolutions of classes would work. For example,
128         * java.sql.DriverManager uses caller's class loader to
129         * get Driver instances. Without this split-delegation
130         * a script class evaluating DriverManager.getDrivers()
131         * will not get back any JDBC driver!
132         */
133        return appLoader.loadClass(name);
134    }
135
136    // package-private and private stuff below this point
137
138    /**
139     * Install a class for use by the Nashorn runtime
140     *
141     * @param name Binary name of class.
142     * @param data Class data bytes.
143     * @param cs CodeSource code source of the class bytes.
144     *
145     * @return Installed class.
146     */
147    synchronized Class<?> installClass(final String name, final byte[] data, final CodeSource cs) {
148        return defineClass(name, data, 0, data.length, Objects.requireNonNull(cs));
149    }
150}
151