Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncherLoader.java
41159 views
1
/*
2
* Copyright (c) 2005, 2021, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
package sun.jvm.hotspot;
26
27
import java.io.*;
28
import java.net.*;
29
import java.util.*;
30
import java.security.*;
31
32
/**
33
* SA uses native debugger back-end library - libsaproc.so on Unix platforms.
34
* Starting from 5.0, in Solaris & Linux JDK "libsaproc.so" is shipped with JDK
35
* and is kept jre/lib/cpu directory (where all other JDK platform libraries
36
* are kept). This implies that always that jre copy of libsaproc.so will be
37
* used and the copy of libsaproc.so built from SA sources here will not
38
* be used at all. We can override libsaproc.so using this class loader
39
* as System class loader using "java.system.class.loader" property. This
40
* class loader loads classes paths specified paths using the System property
41
* "java.class.path". Because, this class loader loads SA debugger classes
42
* (among other classes), JVM calls findLibrary override here. In this
43
* findLibrary, we first check the library in the directories specified through
44
* "sa.library.path" System property. This way updated/latest SA native library
45
* can be loaded instead of the one from JDK's jre/lib directory.
46
*/
47
public class SALauncherLoader extends URLClassLoader {
48
49
/**
50
* Checks native libraries under directories specified using
51
* the System property "sa.library.path".
52
*/
53
public String findLibrary(String name) {
54
name = System.mapLibraryName(name);
55
for (int i = 0; i < libpaths.length; i++) {
56
File file = new File(new File(libpaths[i]), name);
57
if (file.exists()) {
58
return file.getAbsolutePath();
59
}
60
}
61
return null;
62
}
63
64
public SALauncherLoader(ClassLoader parent) {
65
super(getClassPath(), parent);
66
String salibpath = System.getProperty("sa.library.path");
67
if (salibpath != null) {
68
libpaths = salibpath.split(File.pathSeparator);
69
} else {
70
libpaths = new String[0];
71
}
72
}
73
74
/**
75
* Override loadClass so we can checkPackageAccess.
76
*/
77
public synchronized Class loadClass(String name, boolean resolve)
78
throws ClassNotFoundException {
79
int i = name.lastIndexOf('.');
80
if (i != -1) {
81
@SuppressWarnings("removal")
82
SecurityManager sm = System.getSecurityManager();
83
if (sm != null) {
84
sm.checkPackageAccess(name.substring(0, i));
85
}
86
}
87
88
Class clazz = findLoadedClass(name);
89
if (clazz != null) return clazz;
90
91
/*
92
* NOTE: Unlike 'usual' class loaders, we do *not* delegate to
93
* the parent loader first. We attempt to load the class
94
* ourselves first and use parent loader only if we can't load.
95
* This is because the parent of this loader is 'default'
96
* System loader that can 'see' all SA classes in classpath and
97
* so will load those if delegated. And if parent loads SA classes,
98
* then JVM won't call findNative override in this class.
99
*/
100
try {
101
return findClass(name);
102
} catch (ClassNotFoundException cnfe) {
103
return (super.loadClass(name, resolve));
104
}
105
}
106
107
/**
108
* allow any classes loaded from classpath to exit the VM.
109
*/
110
protected PermissionCollection getPermissions(CodeSource codesource) {
111
PermissionCollection perms = super.getPermissions(codesource);
112
perms.add(new RuntimePermission("exitVM"));
113
return perms;
114
}
115
116
//-- Internals only below this point
117
118
private String[] libpaths;
119
120
private static URL[] getClassPath() {
121
final String s = System.getProperty("java.class.path");
122
final File[] path = (s == null) ? new File[0] : getClassPath(s);
123
124
return pathToURLs(path);
125
}
126
127
private static URL[] pathToURLs(File[] path) {
128
URL[] urls = new URL[path.length];
129
for (int i = 0; i < path.length; i++) {
130
urls[i] = getFileURL(path[i]);
131
}
132
return urls;
133
}
134
135
private static File[] getClassPath(String cp) {
136
String[] tmp = cp.split(File.pathSeparator);
137
File[] paths = new File[tmp.length];
138
for (int i = 0; i < paths.length; i++) {
139
paths[i] = new File(tmp[i].equals("")? "." : tmp[i]);
140
}
141
return paths;
142
}
143
144
private static URL getFileURL(File file) {
145
try {
146
file = file.getCanonicalFile();
147
} catch (IOException e) {
148
e.printStackTrace();
149
}
150
151
try {
152
return file.toURI().toURL();
153
} catch (MalformedURLException mue) {
154
throw new InternalError(mue.getMessage());
155
}
156
}
157
}
158
159