Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/LoggerBridgeTest/CustomSystemClassLoader.java
41161 views
1
/*
2
* Copyright (c) 2015, 2018, 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
import java.io.File;
25
import java.io.IOException;
26
import java.net.URL;
27
import java.nio.file.Files;
28
import java.security.AllPermission;
29
import java.security.Permissions;
30
import java.security.ProtectionDomain;
31
import java.util.concurrent.ConcurrentHashMap;
32
33
34
/**
35
* A custom ClassLoader to load the concrete LoggerFinder class
36
* with all permissions.
37
*
38
* @author danielfuchs
39
*/
40
public class CustomSystemClassLoader extends ClassLoader {
41
42
43
private final ConcurrentHashMap<String, Class<?>> classes = new ConcurrentHashMap<>();
44
45
public CustomSystemClassLoader() {
46
super();
47
}
48
public CustomSystemClassLoader(ClassLoader parent) {
49
super(parent);
50
}
51
52
private Class<?> defineFinderClass(String name)
53
throws ClassNotFoundException {
54
55
Class<?> loggerFinderClass = classes.get(name);
56
if (loggerFinderClass != null) return loggerFinderClass;
57
58
final Object obj = getClassLoadingLock(name);
59
synchronized(obj) {
60
loggerFinderClass = classes.get(name);
61
if (loggerFinderClass != null) return loggerFinderClass;
62
63
URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
64
File file = new File(url.getPath(), name+".class");
65
if (file.canRead()) {
66
try {
67
byte[] b = Files.readAllBytes(file.toPath());
68
Permissions perms = new Permissions();
69
perms.add(new AllPermission());
70
loggerFinderClass = defineClass(
71
name, b, 0, b.length, new ProtectionDomain(
72
this.getClass().getProtectionDomain().getCodeSource(),
73
perms));
74
classes.put(name, loggerFinderClass);
75
System.out.println("Loaded " + name);
76
return loggerFinderClass;
77
} catch (Throwable ex) {
78
ex.printStackTrace();
79
throw new ClassNotFoundException(name, ex);
80
}
81
} else {
82
throw new ClassNotFoundException(name,
83
new IOException(file.toPath() + ": can't read"));
84
}
85
}
86
}
87
88
@Override
89
public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
90
if (name.equals("LogProducerFinder") || name.startsWith("LogProducerFinder$")) {
91
Class<?> c = defineFinderClass(name);
92
if (resolve) {
93
resolveClass(c);
94
}
95
return c;
96
}
97
return super.loadClass(name, resolve);
98
}
99
100
@Override
101
protected Class<?> findClass(String name) throws ClassNotFoundException {
102
if (name.equals("LogProducerFinder") || name.startsWith("LogProducerFinder$")) {
103
return defineFinderClass(name);
104
}
105
return super.findClass(name);
106
}
107
108
}
109
110