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