1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.launcher; |
12 | |
|
13 | |
import org.mule.util.ClassUtils; |
14 | |
|
15 | |
import java.io.IOException; |
16 | |
import java.lang.reflect.Field; |
17 | |
import java.lang.reflect.Method; |
18 | |
import java.net.JarURLConnection; |
19 | |
import java.net.URL; |
20 | |
import java.net.URLClassLoader; |
21 | |
import java.net.URLStreamHandler; |
22 | |
import java.net.URLStreamHandlerFactory; |
23 | |
import java.util.Collection; |
24 | |
import java.util.Vector; |
25 | |
import java.util.jar.JarFile; |
26 | |
|
27 | |
import sun.net.www.protocol.jar.Handler; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class GoodCitizenClassLoader extends URLClassLoader |
38 | |
{ |
39 | |
|
40 | |
public GoodCitizenClassLoader(URL[] urls, ClassLoader parent) |
41 | |
{ |
42 | 0 | super(urls, parent, new NonCachingURLStreamHandlerFactory()); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public void close() |
49 | |
{ |
50 | |
|
51 | |
try |
52 | |
{ |
53 | 0 | Class<URLClassLoader> clazz = URLClassLoader.class; |
54 | 0 | Field ucp = clazz.getDeclaredField("ucp"); |
55 | 0 | ucp.setAccessible(true); |
56 | 0 | Object urlClassPath = ucp.get(this); |
57 | 0 | Field loaders = urlClassPath.getClass().getDeclaredField("loaders"); |
58 | 0 | loaders.setAccessible(true); |
59 | 0 | Collection<?> jarLoaders = (Collection<?>) loaders.get(urlClassPath); |
60 | 0 | for (Object jarLoader : jarLoaders) |
61 | |
{ |
62 | |
try |
63 | |
{ |
64 | 0 | Field loader = jarLoader.getClass().getDeclaredField("jar"); |
65 | 0 | loader.setAccessible(true); |
66 | 0 | Object jarFile = loader.get(jarLoader); |
67 | 0 | ((JarFile) jarFile).close(); |
68 | |
} |
69 | 0 | catch (Throwable t) |
70 | |
{ |
71 | |
|
72 | 0 | } |
73 | |
} |
74 | |
} |
75 | 0 | catch (Throwable t) |
76 | |
{ |
77 | |
|
78 | 0 | } |
79 | |
|
80 | |
try |
81 | |
{ |
82 | |
|
83 | 0 | Class<ClassLoader> clazz = ClassLoader.class; |
84 | 0 | Field nativeLibraries = clazz.getDeclaredField("nativeLibraries"); |
85 | 0 | nativeLibraries.setAccessible(true); |
86 | 0 | Vector<?> nativelib = (Vector<?>) nativeLibraries.get(this); |
87 | 0 | for (Object lib : nativelib) |
88 | |
{ |
89 | 0 | Method finalize = lib.getClass().getDeclaredMethod("finalize"); |
90 | 0 | finalize.setAccessible(true); |
91 | 0 | finalize.invoke(lib); |
92 | 0 | } |
93 | |
} |
94 | 0 | catch (Exception ex) |
95 | |
{ |
96 | |
|
97 | 0 | } |
98 | |
|
99 | |
try |
100 | |
{ |
101 | |
|
102 | 0 | final Class clazz = ClassUtils.loadClass("org.codehaus.groovy.transform.ASTTransformationVisitor", getClass()); |
103 | 0 | final Field compUnit = clazz.getDeclaredField("compUnit"); |
104 | 0 | compUnit.setAccessible(true); |
105 | |
|
106 | 0 | compUnit.set(null, null); |
107 | |
} |
108 | 0 | catch (Throwable t) |
109 | |
{ |
110 | |
|
111 | 0 | } |
112 | 0 | } |
113 | |
|
114 | 0 | protected static class NonCachingURLStreamHandlerFactory implements URLStreamHandlerFactory |
115 | |
{ |
116 | |
public URLStreamHandler createURLStreamHandler(String protocol) |
117 | |
{ |
118 | 0 | return new NonCachingJarResourceURLStreamHandler(); |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private static class NonCachingJarResourceURLStreamHandler extends Handler |
127 | |
{ |
128 | |
public NonCachingJarResourceURLStreamHandler() |
129 | |
{ |
130 | 0 | super(); |
131 | 0 | } |
132 | |
|
133 | |
@Override |
134 | |
protected java.net.URLConnection openConnection(URL u) throws IOException |
135 | |
{ |
136 | 0 | JarURLConnection c = new sun.net.www.protocol.jar.JarURLConnection(u, this); |
137 | 0 | c.setUseCaches(false); |
138 | 0 | return c; |
139 | |
} |
140 | |
} |
141 | |
} |