-
Notifications
You must be signed in to change notification settings - Fork 53
/
JAUtilities.java
executable file
·299 lines (243 loc) · 11 KB
/
JAUtilities.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (c) 2014, 2024 Oracle 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 org.glassfish.hk2.xml.internal;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.LoaderClassPath;
import javassist.NotFoundException;
import javassist.bytecode.ClassFile;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.utilities.cache.Computable;
import org.glassfish.hk2.utilities.cache.HybridCacheEntry;
import org.glassfish.hk2.utilities.cache.LRUHybridCache;
import org.glassfish.hk2.utilities.general.GeneralUtilities;
import org.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.Logger;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
import org.glassfish.hk2.xml.internal.alt.clazz.ClassAltClassImpl;
import org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean;
/**
* @author jwells
*
*/
public class JAUtilities {
private final static String ID_PREFIX = "XmlServiceUID-";
private final static boolean DEBUG_PREGEN = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return Boolean.parseBoolean(
System.getProperty("org.jvnet.hk2.properties.xmlservice.jaxb.pregenerated", "false"));
}
});
/* package */ final static boolean DEBUG_GENERATION_TIMING = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return Boolean.parseBoolean(
System.getProperty("org.jvnet.hk2.properties.xmlservice.jaxb.generationtime", "false"));
}
});
public final static String GET = "get";
public final static String SET = "set";
public final static String IS = "is";
public final static String LOOKUP = "lookup";
public final static String ADD = "add";
public final static String REMOVE = "remove";
public final static String JAXB_DEFAULT_STRING = "##default";
public final static String JAXB_DEFAULT_DEFAULT = "\u0000";
private final ReentrantLock lock = new ReentrantLock();
private final ClassReflectionHelper classReflectionHelper;
private final ClassPool defaultClassPool;
private final CtClass superClazz;
private final Computer computer;
private final LRUHybridCache<Class<?>, ModelImpl> interface2ModelCache;
private final AtomicLong idGenerator = new AtomicLong();
private static Set<ClassLoader> getClassLoaders(final Class<?> myClass) {
return AccessController.doPrivileged(new PrivilegedAction<Set<ClassLoader>>() {
@Override
public Set<ClassLoader> run() {
Set<ClassLoader> retVal = new LinkedHashSet<ClassLoader>();
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl != null) {
retVal.add(ccl);
}
retVal.add(myClass.getClassLoader());
return retVal;
}
});
}
/* package */ JAUtilities(ClassReflectionHelper classReflectionHelper) {
defaultClassPool = ClassPool.getDefault();
for (ClassLoader cl : getClassLoaders(this.getClass())) {
defaultClassPool.appendClassPath(new LoaderClassPath(cl));
}
this.classReflectionHelper = classReflectionHelper;
try {
superClazz = defaultClassPool.get(BaseHK2JAXBBean.class.getName());
}
catch (NotFoundException e) {
throw new MultiException(e);
}
computer = new Computer(this);
interface2ModelCache = new LRUHybridCache<Class<?>, ModelImpl>(Integer.MAX_VALUE - 1, computer);
}
/**
* Gets the XmlService wide unique identifier
* @return A unique identifier for unkeyed multi-children
*/
public String getUniqueId() {
return ID_PREFIX + idGenerator.getAndAdd(1L);
}
/**
* Goes from interface name (fully qualified) to the associated Model
*
* @param iFace Fully qualified interface name (not proxy)
* @return The Model for the interface
*/
public ModelImpl getModel(Class<?> iFace) {
HybridCacheEntry<ModelImpl> entry = interface2ModelCache.compute(iFace);
return entry.getValue();
}
public void convertRootAndLeaves(Class<?> root, boolean mustConvertAll) {
lock.lock();
try {
long currentTime = 0L;
if (DEBUG_GENERATION_TIMING) {
computer.numGenerated = 0;
computer.numPreGenerated = 0;
currentTime = System.currentTimeMillis();
}
ModelImpl rootModel = interface2ModelCache.compute(root).getValue();
if (!mustConvertAll) {
if (DEBUG_GENERATION_TIMING) {
currentTime = System.currentTimeMillis() - currentTime;
Logger.getLogger().debug("Took " + currentTime + " to perform " +
computer.numGenerated + " generations with " +
computer.numPreGenerated + " pre generated with a lazy parser");
}
return;
}
HashSet<Class<?>> cycles = new HashSet<Class<?>>();
cycles.add(root);
convertAllRootAndLeaves(rootModel, cycles);
if (DEBUG_GENERATION_TIMING) {
currentTime = System.currentTimeMillis() - currentTime;
Logger.getLogger().debug("Took " + currentTime + " milliseconds to perform " +
computer.numGenerated + " generations with " +
computer.numPreGenerated + " pre generated with a non-lazy parser");
}
} finally {
lock.unlock();
}
}
private void convertAllRootAndLeaves(ModelImpl rootModel, HashSet<Class<?>> cycles) {
for (ParentedModel parentModel : rootModel.getAllChildren()) {
Class<?> convertMe = parentModel.getChildModel().getOriginalInterfaceAsClass();
// Remove cycles
if (cycles.contains(convertMe)) continue;
cycles.add(convertMe);
ModelImpl childModel = interface2ModelCache.compute(convertMe).getValue();
convertAllRootAndLeaves(childModel, cycles);
}
}
private CtClass getBaseClass() {
return superClazz;
}
private ClassPool getClassPool() {
return defaultClassPool;
}
public int getNumGenerated() {
return computer.numGenerated;
}
public int getNumPreGenerated() {
return computer.numPreGenerated;
}
private final class Computer implements Computable<Class<?>, HybridCacheEntry<ModelImpl>> {
private final JAUtilities jaUtilities;
private int numGenerated;
private int numPreGenerated;
private Computer(JAUtilities jaUtilities) {
this.jaUtilities = jaUtilities;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.utilities.cache.Computable#compute(java.lang.Object)
*/
@Override
public HybridCacheEntry<ModelImpl> compute(Class<?> key) {
String iFaceName = key.getName();
String proxyName = Utilities.getProxyNameFromInterfaceName(iFaceName);
Class<?> proxyClass = GeneralUtilities.loadClass(key.getClassLoader(), proxyName);
if (proxyClass == null) {
numGenerated++;
if (DEBUG_PREGEN) {
Logger.getLogger().debug("Pregenerating proxy for " + key.getName());
}
// Generate the proxy
try {
CtClass generated = Generator.generate(new ClassAltClassImpl(key, classReflectionHelper),
jaUtilities.getBaseClass(),
jaUtilities.getClassPool());
if (ClassFile.MAJOR_VERSION > ClassFile.JAVA_8) {
proxyClass = generated.toClass(key);
} else {
proxyClass = generated.toClass(key.getClassLoader(), key.getProtectionDomain());
}
}
catch (RuntimeException re) {
throw new RuntimeException("Could not compile proxy for class " + iFaceName, re);
}
catch (Throwable th) {
throw new RuntimeException("Could not compile proxy for class " + iFaceName, th);
}
}
else {
if (DEBUG_PREGEN) {
Logger.getLogger().debug("Proxy for " + key.getName() + " was pregenerated");
}
numPreGenerated++;
}
Method getModelMethod;
try {
getModelMethod = proxyClass.getMethod(Generator.STATIC_GET_MODEL_METHOD_NAME, new Class<?>[0]);
}
catch (NoSuchMethodException e) {
throw new AssertionError("This proxy must have been generated with an old generator, it has no __getModel method for " + key.getName());
}
ModelImpl retVal;
try {
retVal = (ModelImpl) ReflectionHelper.invoke(null, getModelMethod, new Object[0], false);
}
catch (RuntimeException re) {
throw re;
}
catch (Throwable e) {
throw new RuntimeException(e);
}
if (retVal == null) {
throw new AssertionError("The __getModel method on " + proxyClass.getName() + " returned null");
}
retVal.setJAUtilities(jaUtilities, key.getClassLoader());
return interface2ModelCache.createCacheEntry(key, retVal, false);
}
}
}