author | attila |
Mon, 09 Nov 2015 14:03:37 +0100 | |
changeset 33684 | 1bfe8ffd9baf |
parent 31549 | b627094c5649 |
child 36517 | 41a1c20eb619 |
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 |
||
28 |
import java.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.net.MalformedURLException; |
|
31 |
import java.net.URL; |
|
32 |
import java.net.URLClassLoader; |
|
16522 | 33 |
import java.security.CodeSource; |
34 |
import java.security.Permission; |
|
35 |
import java.security.PermissionCollection; |
|
36 |
import java.security.Permissions; |
|
16147 | 37 |
import java.security.SecureClassLoader; |
38 |
||
39 |
/** |
|
19895
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
19236
diff
changeset
|
40 |
* Superclass for Nashorn class loader classes. |
16147 | 41 |
*/ |
42 |
abstract class NashornLoader extends SecureClassLoader { |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
43 |
private static final String OBJECTS_PKG = "jdk.nashorn.internal.objects"; |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
44 |
private static final String RUNTIME_PKG = "jdk.nashorn.internal.runtime"; |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
45 |
private static final String RUNTIME_ARRAYS_PKG = "jdk.nashorn.internal.runtime.arrays"; |
16522 | 46 |
private static final String RUNTIME_LINKER_PKG = "jdk.nashorn.internal.runtime.linker"; |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
47 |
private static final String SCRIPTS_PKG = "jdk.nashorn.internal.scripts"; |
16522 | 48 |
|
49 |
private static final Permission[] SCRIPT_PERMISSIONS; |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
50 |
|
16522 | 51 |
static { |
52 |
/* |
|
53 |
* Generated classes get access to runtime, runtime.linker, objects, scripts packages. |
|
54 |
* Note that the actual scripts can not access these because Java.type, Packages |
|
55 |
* prevent these restricted packages. And Java reflection and JSR292 access is prevented |
|
56 |
* for scripts. In other words, nashorn generated portions of script classes can access |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
57 |
* classes in these implementation packages. |
16522 | 58 |
*/ |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
59 |
SCRIPT_PERMISSIONS = new Permission[] { |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
60 |
new RuntimePermission("accessClassInPackage." + RUNTIME_PKG), |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
61 |
new RuntimePermission("accessClassInPackage." + RUNTIME_LINKER_PKG), |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
62 |
new RuntimePermission("accessClassInPackage." + OBJECTS_PKG), |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
63 |
new RuntimePermission("accessClassInPackage." + SCRIPTS_PKG), |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
64 |
new RuntimePermission("accessClassInPackage." + RUNTIME_ARRAYS_PKG) |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
65 |
}; |
16522 | 66 |
} |
67 |
||
19895
965b12eb322e
8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents:
19236
diff
changeset
|
68 |
NashornLoader(final ClassLoader parent) { |
16147 | 69 |
super(parent); |
70 |
} |
|
71 |
||
72 |
protected static void checkPackageAccess(final String name) { |
|
73 |
final int i = name.lastIndexOf('.'); |
|
74 |
if (i != -1) { |
|
75 |
final SecurityManager sm = System.getSecurityManager(); |
|
76 |
if (sm != null) { |
|
16522 | 77 |
final String pkgName = name.substring(0, i); |
78 |
switch (pkgName) { |
|
79 |
case RUNTIME_PKG: |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
19097
diff
changeset
|
80 |
case RUNTIME_ARRAYS_PKG: |
16522 | 81 |
case RUNTIME_LINKER_PKG: |
82 |
case OBJECTS_PKG: |
|
83 |
case SCRIPTS_PKG: |
|
84 |
// allow it. |
|
85 |
break; |
|
86 |
default: |
|
87 |
sm.checkPackageAccess(pkgName); |
|
88 |
} |
|
16147 | 89 |
} |
90 |
} |
|
91 |
} |
|
92 |
||
16522 | 93 |
@Override |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
19895
diff
changeset
|
94 |
protected PermissionCollection getPermissions(final CodeSource codesource) { |
16522 | 95 |
final Permissions permCollection = new Permissions(); |
96 |
for (final Permission perm : SCRIPT_PERMISSIONS) { |
|
97 |
permCollection.add(perm); |
|
98 |
} |
|
99 |
return permCollection; |
|
100 |
} |
|
101 |
||
16147 | 102 |
/** |
103 |
* Create a secure URL class loader for the given classpath |
|
104 |
* @param classPath classpath for the loader to search from |
|
33684
1bfe8ffd9baf
8141541: Simplify Nashorn's Context class loader handling
attila
parents:
31549
diff
changeset
|
105 |
* @param parent the parent class loader for the new class loader |
16147 | 106 |
* @return the class loader |
107 |
*/ |
|
33684
1bfe8ffd9baf
8141541: Simplify Nashorn's Context class loader handling
attila
parents:
31549
diff
changeset
|
108 |
static ClassLoader createClassLoader(final String classPath, final ClassLoader parent) { |
16147 | 109 |
final URL[] urls = pathToURLs(classPath); |
110 |
return URLClassLoader.newInstance(urls, parent); |
|
111 |
} |
|
112 |
||
113 |
/* |
|
114 |
* Utility method for converting a search path string to an array |
|
115 |
* of directory and JAR file URLs. |
|
116 |
* |
|
117 |
* @param path the search path string |
|
118 |
* @return the resulting array of directory and JAR file URLs |
|
119 |
*/ |
|
120 |
private static URL[] pathToURLs(final String path) { |
|
121 |
final String[] components = path.split(File.pathSeparator); |
|
122 |
URL[] urls = new URL[components.length]; |
|
123 |
int count = 0; |
|
124 |
while(count < components.length) { |
|
125 |
final URL url = fileToURL(new File(components[count])); |
|
126 |
if (url != null) { |
|
127 |
urls[count++] = url; |
|
128 |
} |
|
129 |
} |
|
130 |
if (urls.length != count) { |
|
131 |
final URL[] tmp = new URL[count]; |
|
132 |
System.arraycopy(urls, 0, tmp, 0, count); |
|
133 |
urls = tmp; |
|
134 |
} |
|
135 |
return urls; |
|
136 |
} |
|
137 |
||
138 |
/* |
|
139 |
* Returns the directory or JAR file URL corresponding to the specified |
|
140 |
* local file name. |
|
141 |
* |
|
142 |
* @param file the File object |
|
143 |
* @return the resulting directory or JAR file URL, or null if unknown |
|
144 |
*/ |
|
145 |
private static URL fileToURL(final File file) { |
|
146 |
String name; |
|
147 |
try { |
|
148 |
name = file.getCanonicalPath(); |
|
149 |
} catch (final IOException e) { |
|
150 |
name = file.getAbsolutePath(); |
|
151 |
} |
|
152 |
name = name.replace(File.separatorChar, '/'); |
|
153 |
if (!name.startsWith("/")) { |
|
154 |
name = "/" + name; |
|
155 |
} |
|
156 |
// If the file does not exist, then assume that it's a directory |
|
157 |
if (!file.isFile()) { |
|
31549
b627094c5649
8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents:
25865
diff
changeset
|
158 |
name += "/"; |
16147 | 159 |
} |
160 |
try { |
|
161 |
return new URL("file", "", name); |
|
162 |
} catch (final MalformedURLException e) { |
|
163 |
throw new IllegalArgumentException("file"); |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |