SunAttachAPIResolver.java
3.99 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.annotation.Nullable
* org.apache.felix.scr.annotations.Deactivate
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceRegistration
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.deserfw.impl.attach;
import com.adobe.cq.deserfw.api.AttachAPIResolver;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.apache.felix.scr.annotations.Deactivate;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SunAttachAPIResolver
implements AttachAPIResolver {
private static final String ATTACH_VM_CLASSNAME = "com.sun.tools.attach.VirtualMachine";
private static final Logger log = LoggerFactory.getLogger(AttachAPIResolver.class);
private URLClassLoader loader = null;
@Nullable
@Override
public Class<?> getAttachAPIClass() {
if (this.loader != null) {
this.teardown();
}
try {
return ClassLoader.getSystemClassLoader().loadClass("com.sun.tools.attach.VirtualMachine");
}
catch (ClassNotFoundException cnfe) {
log.debug("Sun Attach API not found. Trying to load tools.jar");
try {
Iterator<File> i$ = this.getPossibleToolsJars().iterator();
if (i$.hasNext()) {
File f = i$.next();
try {
this.loader = new URLClassLoader(new URL[]{f.toURI().toURL()}, ClassLoader.getSystemClassLoader());
Class result = this.loader.loadClass("com.sun.tools.attach.VirtualMachine");
log.info("Loaded {} from {}", (Object)result.getName(), (Object)f.getAbsolutePath());
return result;
}
catch (Throwable t) {
this.teardown();
log.debug("Unable to load {}. See nested exception", (Object)f, (Object)t);
throw cnfe;
}
}
}
catch (ClassNotFoundException cnfe2) {
log.info("Sun Attach API not found.");
}
return null;
}
}
@Deactivate
public void teardown() {
if (this.loader != null) {
try {
this.loader.close();
this.loader = null;
}
catch (IOException e) {
log.warn("Unable to tear down tools loader", (Throwable)e);
}
}
}
public List<File> getPossibleToolsJars() {
ArrayList<File> jars = new ArrayList<File>();
String homePath = System.getProperty("java.home");
if (homePath == null || homePath.length() == 0) {
return jars;
}
File home = new File(homePath);
File fromJRE = new File(home, "lib/tools.jar");
this.addTools(fromJRE, jars);
if ("jre".equalsIgnoreCase(home.getName())) {
File fromJDK = home.getParentFile();
this.addTools(fromJDK, jars);
}
return jars;
}
private void addTools(File parent, List<File> jars) {
File tools = new File(parent, "lib/tools.jar");
if (tools.exists() && tools.canRead()) {
jars.add(tools);
}
}
public static ServiceRegistration registerService(BundleContext ctx) {
Hashtable<String, Object> props = new Hashtable<String, Object>();
props.put("service.ranking", 100);
props.put("service.description", "AttachAPIResolver for Sun JVMs");
return ctx.registerService(AttachAPIResolver.class.getName(), (Object)new SunAttachAPIResolver(), props);
}
}