author | attila |
Thu, 06 Oct 2016 16:27:47 +0200 | |
changeset 41422 | 97eda72f53b6 |
parent 38808 | 102fd16b8798 |
child 41502 | 5772044e1a21 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
26 |
package jdk.nashorn.internal.runtime; |
|
27 |
||
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
28 |
import java.lang.module.ModuleDescriptor; |
36517 | 29 |
import java.lang.reflect.Module; |
16147 | 30 |
import java.security.CodeSource; |
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
25865
diff
changeset
|
31 |
import java.util.Objects; |
16147 | 32 |
|
33 |
/** |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16151
diff
changeset
|
34 |
* Responsible for loading script generated classes. |
16147 | 35 |
* |
36 |
*/ |
|
37 |
final class ScriptLoader extends NashornLoader { |
|
19895
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
38 |
private static final String NASHORN_PKG_PREFIX = "jdk.nashorn.internal."; |
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
39 |
|
36517 | 40 |
private volatile boolean structureAccessAdded; |
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
41 |
private final Context context; |
36517 | 42 |
private final Module scriptModule; |
19895
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
43 |
|
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
44 |
/*package-private*/ Context getContext() { |
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
45 |
return context; |
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
46 |
} |
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
47 |
|
16147 | 48 |
/** |
49 |
* Constructor. |
|
50 |
*/ |
|
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
51 |
ScriptLoader(final Context context) { |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
52 |
super(context.getStructLoader()); |
19895
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
53 |
this.context = context; |
36517 | 54 |
|
55 |
// new scripts module, it's specific exports and read-edges |
|
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
56 |
scriptModule = createModule("jdk.scripting.nashorn.scripts"); |
36517 | 57 |
|
58 |
// specific exports from nashorn to new scripts module |
|
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
59 |
NASHORN_MODULE.addExports(OBJECTS_PKG, scriptModule); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
60 |
NASHORN_MODULE.addExports(RUNTIME_PKG, scriptModule); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
61 |
NASHORN_MODULE.addExports(RUNTIME_ARRAYS_PKG, scriptModule); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
62 |
NASHORN_MODULE.addExports(RUNTIME_LINKER_PKG, scriptModule); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
63 |
NASHORN_MODULE.addExports(SCRIPTS_PKG, scriptModule); |
36517 | 64 |
|
65 |
// nashorn needs to read scripts module methods,fields |
|
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
66 |
NASHORN_MODULE.addReads(scriptModule); |
16147 | 67 |
} |
68 |
||
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
69 |
private Module createModule(final String moduleName) { |
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
70 |
final Module structMod = context.getStructLoader().getModule(); |
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
71 |
final ModuleDescriptor descriptor |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
72 |
= new ModuleDescriptor.Builder(moduleName) |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
73 |
.requires(NASHORN_MODULE.getName()) |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
74 |
.requires(structMod.getName()) |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
75 |
.conceals(SCRIPTS_PKG) |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
76 |
.build(); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
77 |
|
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
78 |
final Module mod = Context.createModuleTrusted(structMod.getLayer(), descriptor, this); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
79 |
loadModuleManipulator(); |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
80 |
return mod; |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
81 |
} |
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
82 |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
83 |
@Override |
24759
31aed7d9c02a
8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents:
24720
diff
changeset
|
84 |
protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException { |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
85 |
checkPackageAccess(name); |
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
86 |
final Class<?> cl = super.loadClass(name, resolve); |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
87 |
if (!structureAccessAdded) { |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
88 |
final StructureLoader structLoader = context.getStructLoader(); |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
89 |
if (cl.getClassLoader() == structLoader) { |
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
90 |
structureAccessAdded = true; |
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
91 |
structLoader.addModuleExport(scriptModule); |
36517 | 92 |
} |
19895
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
16525
diff
changeset
|
93 |
} |
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
94 |
return cl; |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
95 |
} |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
96 |
|
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
97 |
@Override |
41422 | 98 |
protected Class<?> findClass(final String name) throws ClassNotFoundException { |
38808
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
99 |
final ClassLoader appLoader = context.getAppLoader(); |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
100 |
|
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
101 |
/* |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
102 |
* If the appLoader is null, don't bother side-delegating to it! |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
103 |
* Bootloader has been already attempted via parent loader |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
104 |
* delegation from the "loadClass" method. |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
105 |
* |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
106 |
* Also, make sure that we don't delegate to the app loader |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
107 |
* for nashorn's own classes or nashorn generated classes! |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
108 |
*/ |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
109 |
if (appLoader == null || name.startsWith(NASHORN_PKG_PREFIX)) { |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
110 |
throw new ClassNotFoundException(name); |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
111 |
} |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
112 |
|
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
113 |
/* |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
114 |
* This split-delegation is used so that caller loader |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
115 |
* based resolutions of classes would work. For example, |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
116 |
* java.sql.DriverManager uses caller's class loader to |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
117 |
* get Driver instances. Without this split-delegation |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
118 |
* a script class evaluating DriverManager.getDrivers() |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
119 |
* will not get back any JDBC driver! |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
120 |
*/ |
102fd16b8798
8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents:
38806
diff
changeset
|
121 |
return appLoader.loadClass(name); |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
122 |
} |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
123 |
|
16147 | 124 |
// package-private and private stuff below this point |
125 |
||
126 |
/** |
|
127 |
* Install a class for use by the Nashorn runtime |
|
128 |
* |
|
129 |
* @param name Binary name of class. |
|
130 |
* @param data Class data bytes. |
|
131 |
* @param cs CodeSource code source of the class bytes. |
|
132 |
* |
|
133 |
* @return Installed class. |
|
134 |
*/ |
|
135 |
synchronized Class<?> installClass(final String name, final byte[] data, final CodeSource cs) { |
|
31094
7a9fa21da791
8085810: Return value of Objects.requireNonNull call can be used
sundar
parents:
28785
diff
changeset
|
136 |
return defineClass(name, data, 0, data.length, Objects.requireNonNull(cs)); |
16147 | 137 |
} |
138 |
} |