TestJarLoader.java
5.8 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
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
package com.zhaoonline.support.gateway.apijar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.zhaoonline.support.gateway.utils.Utils;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.NotFoundException;
public class TestJarLoader {
public static String classParentDir = "D://test/";
@Before
public void before() throws IOException {
}
/**
* Method name: createAndloadJarDynamic <BR>
* Description: parenet classLoader是无法loaderClass的 <BR>
* Remark: <BR>
* @throws NotFoundException
* @throws CannotCompileException
* @throws IOException void<BR>
*/
String jarDir=classParentDir + "testJar.jar";
@Test
public void createAndloadJarDynamic() throws NotFoundException, CannotCompileException, IOException {
Utils.deleteDirectory(Paths.get(classParentDir));
Set<PosixFilePermission> perms=new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
Files.createDirectories(Paths.get(classParentDir));
Map<String, Class<?>> properties = new HashMap<String, Class<?>>();
properties.put("name", java.lang.String.class);
properties.put("id", java.lang.String.class);
createDynamicJarWithSimplePojo(properties, "com.test.User", jarDir);
// ClassLoader parenclassLoader=Thread.currentThread().getContextClassLoader();
ClassLoader parenclassLoader=getClass().getClassLoader();
// ClassLoader parenclassLoader=new ClassLoader() {
// };
//新增加的com.test.User是由getClass().getClassLoader()创建的,所以能够class存在的
Class<?> userClassBeforeLoad=null;
try {
userClassBeforeLoad = parenclassLoader.loadClass("com.test.User");
Assert.assertNotNull(userClassBeforeLoad);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Assert.assertNull(userClassBeforeLoad);
}
URLClassLoader urlClassLoader=JarLoader.loadFromJar(Thread.currentThread().getContextClassLoader(),jarDir);
Class<?> userClassAfterLoad;
try {
userClassAfterLoad = urlClassLoader.loadClass("com.test.User");
Assert.assertNotNull(userClassAfterLoad);
} catch (ClassNotFoundException e) {
e.printStackTrace();
fail();
}
urlClassLoader.close();
Class<?> userClassAfterLoadAndClosed;
try {
userClassAfterLoadAndClosed = urlClassLoader.loadClass("com.test.User");
Assert.assertNotNull(userClassAfterLoadAndClosed);
} catch (ClassNotFoundException e) {
e.printStackTrace();
fail();
}
Class<?> userClassBeforeLoad2=null;
try {
userClassBeforeLoad2 = parenclassLoader.loadClass("com.test.User");
Assert.assertNotNull(userClassBeforeLoad2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void loadJarDynamicFromExistedFile() throws NotFoundException, CannotCompileException, IOException {
// ClassLoader parenclassLoader=Thread.currentThread().getContextClassLoader();
ClassLoader parenclassLoader=getClass().getClassLoader();
Class<?> userClassBeforeLoad=null;
try {
userClassBeforeLoad = parenclassLoader.loadClass("com.test.User");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Assert.assertNull(userClassBeforeLoad);
}
URLClassLoader urlClassLoader=JarLoader.loadFromJar(Thread.currentThread().getContextClassLoader(),jarDir);
Class<?> userClassAfterLoad;
try {
userClassAfterLoad = urlClassLoader.loadClass("com.test.User");
Assert.assertNotNull(userClassAfterLoad);
} catch (ClassNotFoundException e) {
e.printStackTrace();
fail();
}
urlClassLoader.close();
//在urlClassLoader关闭之后就不能继续加入新的jar了。但是已经Load的class还是可以进行再次装载的
Class<?> userClassAfterLoadAndClosed;
try {
userClassAfterLoadAndClosed = urlClassLoader.loadClass("com.test.User");
Assert.assertNotNull(userClassAfterLoadAndClosed);
} catch (ClassNotFoundException e) {
e.printStackTrace();
fail();
}
Class<?> userClassBeforeLoad2=null;
try {
userClassBeforeLoad2 = parenclassLoader.loadClass("com.test.User");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Assert.assertNull(userClassBeforeLoad2);
}
}
public static void createDynamicJarWithSimplePojo(Map<String, Class<?>> properties, String classname, String jarSavaDir)
throws NotFoundException, CannotCompileException, IOException {
File tempJarFile = new File(jarSavaDir);
if(tempJarFile.exists()){
return ;
}
CtClass classCreator = PojoGenerator.generate(classname, properties);
Class claszz = classCreator.toClass();
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
FileOutputStream tempJarFilestream = new FileOutputStream(tempJarFile);
JarOutputStream outputStream = new JarOutputStream(tempJarFilestream, manifest);
String packageName = classCreator.getPackageName();
JarEntry jarPackage = new JarEntry(packageName.replace(".", "/") + "/");
outputStream.putNextEntry(jarPackage);
JarEntry jarAdd = new JarEntry(claszz.getName().replace(".", "/") + ".class");
outputStream.putNextEntry(jarAdd);
byte[] bytes = classCreator.toBytecode();
outputStream.write(bytes);
outputStream.close();
tempJarFilestream.close();
}
}