-
Notifications
You must be signed in to change notification settings - Fork 53
/
ModuleClassLoader.java
executable file
·208 lines (176 loc) · 6.84 KB
/
ModuleClassLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2007, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.module.impl;
import com.sun.enterprise.module.ModuleState;
import com.sun.enterprise.module.ModulesRegistry;
import com.sun.enterprise.module.common_impl.LogHelper;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.locks.ReentrantLock;
import com.sun.enterprise.module.HK2Module;
/**
* {@link ClassLoader} that loads classes for a module.
*
* @author dochez
*/
final class ModuleClassLoader extends ClassLoaderProxy {
private final ReentrantLock lock = new ReentrantLock();
private final ModuleImpl module;
/**
* HK2Module will be initialized when this classloader is consulted for the first time.
*/
private volatile boolean initialized = false;
private StackTraceElement[] initializerThread;
private String initializerClassName;
/** Creates a new instance of ClassLoader */
public ModuleClassLoader(ModuleImpl owner, URL[] shared, ClassLoader parent) {
super(shared, parent);
this.module = owner;
}
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
lock.lock();
try {
initialize(name);
return super.loadClass(name, resolve);
} finally {
lock.unlock();
}
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
return super.findClass(name);
} catch (ClassNotFoundException e) {
// punch in. find the provider class, no matter where we are.
ModuleImpl m = module.getRegistry().getProvidingModule(name);
if(m!=null)
return m.getPrivateClassLoader().loadClass(name);
throw e;
}
}
public URL getResource(String name) {
initialize(name);
if(name.startsWith(META_INF_SERVICES)) {
// punch in. find the service loader from any module
String serviceName = name.substring(META_INF_SERVICES.length());
ModulesRegistry reg = module.getRegistry();
for( HK2Module m : reg.getModules() ) {
List<URL> list = m.getMetadata().getDescriptors(serviceName);
if(!list.isEmpty()) return list.get(0);
}
// no such resource
return super.getResource(name);
} else {
// normal service look up
URL url = super.getResource(name);
if(url!=null)
return url;
// commons-logging looks for a class file resource for providers,
// so check for those
if(name.endsWith(".class")) {
String className = name.replace('/', '.').substring(0, name.length() - 6);
ModuleImpl m = module.getRegistry().getProvidingModule(className);
if(m!=null)
return m.getPrivateClassLoader().getResource(name);
}
return null;
}
}
public Enumeration<URL> getResources(String name) throws IOException {
initialize(name);
if(name.startsWith(META_INF_SERVICES)) {
// punch in. find the service loader from any module
String serviceName = name.substring(META_INF_SERVICES.length());
Vector<URL> urls = new Vector<URL>();
ModulesRegistry reg = module.getRegistry();
for( HK2Module m : reg.getModules() )
urls.addAll(m.getMetadata().getDescriptors(serviceName));
return urls.elements();
} else {
// normal look up
return super.getResources(name);
}
}
/**
* Lazily initialize this module if not done so yet.
*/
private void initialize(String name) {
if (initialized) return;
lock.lock();
try {
if(!initialized) {
// if we are preparing, we should just not initiate initialization.
if (module.getState().equals(ModuleState.PREPARING)) {
return;
}
initialized = true;
module.start();
// remember who started us to assist debugging.
initializerThread = Thread.currentThread().getStackTrace();
initializerClassName = name;
}
} finally {
lock.unlock();
}
}
/*package*/ void dumpState(PrintStream writer) {
writer.println("Class-Path:");
for (URL url : getURLs())
writer.println(" "+url);
if (initializerThread!=null) {
writer.println("Initialized when " + initializerClassName + " was requested by :");
for (StackTraceElement e : initializerThread) {
writer.println(" "+e.toString());
}
}
}
/**
* called by the facade class loader when it is garbage collected.
* this is a good time to see if this module should be unloaded.
*/
@Override
public void stop() {
// we should only detach if the sticky flag is not set
if (!module.isSticky()) {
LogHelper.getDefaultLogger().info("ModuleClassLoader stopped " + module.getModuleDefinition().getName());
super.stop();
module.stop();
}
}
public ModuleImpl getOwner() {
return module;
}
public String toString() {
StringBuffer s= new StringBuffer();
s.append("ModuleClassLoader(name=").append(module.getName());
s.append(", parent=").append(super.toString());
s.append(",init=").append(initialized);
s.append(",URls[]=");
for (URL url : getURLs()) {
s.append(url).append(",");
}
s.append(")");
for (ClassLoader surrogate : super.getDelegates()) {
s.append("\n ref : ").append(surrogate.toString());
}
return s.toString();
}
private static final String META_INF_SERVICES = "META-INF/services/";
}