Skip to content

Commit cd4f52b

Browse files
author
孙健
committed
fix classloader by filter
1 parent 4bc09ca commit cd4f52b

File tree

21 files changed

+221
-124
lines changed

21 files changed

+221
-124
lines changed

src/main/java/org/nlpcn/jcoder/controller/SystemAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void list(HttpServletRequest req) {
4949
@Ok("raw")
5050
public void export(HttpServletResponse response) throws IOException {
5151

52-
BackupService backupService = StaticValue.getBean(BackupService.class, "backupService");
52+
BackupService backupService = StaticValue.getSystemIoc().get(BackupService.class, "backupService");
5353

5454
String timeStamp = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
5555
response.setContentType("application/octet-stream");
@@ -67,7 +67,7 @@ public String uploadJar(@Param("file") TempFile[] fileList) throws Exception {
6767
throw new Exception("up file can not empty");
6868
}
6969

70-
BackupService backupService = StaticValue.getBean(BackupService.class, "backupService");
70+
BackupService backupService = StaticValue.getSystemIoc().get(BackupService.class, "backupService");
7171

7272
StringBuilder sb = new StringBuilder();
7373

src/main/java/org/nlpcn/jcoder/domain/CodeInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class CodeInfo {
2424

25-
private static final Logger LOG = LoggerFactory.getLogger(CodeInfo.class) ;
25+
private static final Logger LOG = LoggerFactory.getLogger(CodeInfo.class);
2626

2727
private Class<?> classz;
2828

@@ -113,6 +113,7 @@ public ExecuteMethod getDefaultMethod() {
113113

114114
/**
115115
* 通过方法名称获得方法
116+
*
116117
* @param methodName
117118
* @return
118119
*/
@@ -122,6 +123,7 @@ public ExecuteMethod getExecuteMethod(String methodName) {
122123

123124
/**
124125
* 执行方法的抽象类
126+
*
125127
* @author Ansj
126128
*
127129
*/

src/main/java/org/nlpcn/jcoder/filter/JcoderFilter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.servlet.http.HttpServletResponse;
1212

1313
import org.nlpcn.commons.lang.util.StringUtil;
14+
import org.nlpcn.jcoder.run.java.DynamicEngine;
1415
import org.nlpcn.jcoder.run.mvc.ApiActionHandler;
1516
import org.nlpcn.jcoder.run.mvc.view.JsonView;
1617
import org.nlpcn.jcoder.util.ApiException;
@@ -71,9 +72,9 @@ private void _doFilter(final FilterChain chain, HttpServletRequest request, Http
7172
Mvcs.setIoc(StaticValue.getUserIoc()); // reset ioc
7273

7374
Mvcs.setServletContext(sc);
74-
75+
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
7576
try {
76-
77+
Thread.currentThread().setContextClassLoader(DynamicEngine.getInstance().getParentClassLoader());
7778
Mvcs.set(this.selfName, request, response);
7879

7980
if (!apiHandler.handle(request, response)) {
@@ -85,6 +86,7 @@ private void _doFilter(final FilterChain chain, HttpServletRequest request, Http
8586
}
8687

8788
} finally {
89+
Thread.currentThread().setContextClassLoader(contextClassLoader);
8890
Mvcs.set(null, null, null);
8991
Mvcs.ctx().removeReqCtx();
9092
Mvcs.setServletContext(null);

src/main/java/org/nlpcn/jcoder/job/CheckTaskJob.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class CheckTaskJob implements Runnable {
1717
@Override
1818
public void run() {
1919

20-
TaskService taskService = StaticValue.getBean(TaskService.class, "taskService");
20+
TaskService taskService = StaticValue.getSystemIoc().get(TaskService.class, "taskService");
2121

2222
while (true) {
2323
try {

src/main/java/org/nlpcn/jcoder/run/java/ClassUtil.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@
99
import java.net.JarURLConnection;
1010
import java.net.URL;
1111
import java.net.URLDecoder;
12-
import java.util.ArrayList;
1312
import java.util.Enumeration;
1413
import java.util.HashMap;
15-
import java.util.List;
1614
import java.util.Map;
1715
import java.util.jar.JarEntry;
1816
import java.util.jar.JarFile;
1917

2018
import org.nlpcn.jcoder.util.JcoderIOUtil;
2119

22-
import com.google.common.base.Joiner;
23-
2420
/**
2521
* 类相关的工具类
2622
*

src/main/java/org/nlpcn/jcoder/run/java/DynamicEngine.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ public static DynamicEngine getInstance() {
3939

4040
/**
4141
* 刷新instance
42-
* @throws TaskException
42+
*
43+
* @throws TaskException
4344
*/
4445
public static void flush(URLClassLoader classLoader) throws TaskException {
4546
ourInstance = new DynamicEngine(classLoader);
4647
// if class load change , to flush all task
4748
synchronized (StaticValue.MAPPING) {
48-
StaticValue.getBean(TaskService.class, "taskService").initTaskFromDB();
49+
StaticValue.getSystemIoc().get(TaskService.class, "taskService").initTaskFromDB();
4950
}
5051

5152
}
@@ -122,9 +123,9 @@ public Class<?> javaCodeToClass(String fullClassName, String javaCode) throws IO
122123
clazz = dynamicClassLoader.loadClass(fullClassName, jco);
123124
} catch (Exception e) {
124125
e.printStackTrace();
125-
LOG.error(e.getMessage(),e);
126+
LOG.error(e.getMessage(), e);
126127
} catch (Error e) {
127-
LOG.error(e.getMessage(),e);
128+
LOG.error(e.getMessage(), e);
128129
throw new CodeException(e.toString());
129130
} finally {
130131
if (dynamicClassLoader != null) {
@@ -141,8 +142,7 @@ public Class<?> javaCodeToClass(String fullClassName, String javaCode) throws IO
141142

142143
return clazz;
143144
}
144-
145-
145+
146146
public byte[] javaCode2Bytes(String fullClassName, String javaCode) throws IOException, CodeException {
147147
Class<?> clazz = null;
148148
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
@@ -152,7 +152,7 @@ public byte[] javaCode2Bytes(String fullClassName, String javaCode) throws IOExc
152152
jfiles.add(new CharSequenceJavaFileObject(fullClassName, javaCode));
153153

154154
List<String> options = new ArrayList<String>();
155-
155+
156156
options.add("-source");
157157
options.add("1.6");
158158
options.add("-target");
@@ -162,7 +162,6 @@ public byte[] javaCode2Bytes(String fullClassName, String javaCode) throws IOExc
162162
options.add("-classpath");
163163
options.add(this.classpath);
164164
options.add("-parameters");
165-
166165

167166
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, jfiles);
168167
boolean success = task.call();
@@ -178,12 +177,12 @@ public byte[] javaCode2Bytes(String fullClassName, String javaCode) throws IOExc
178177
dynamicClassLoader.loadClass(name, inner);
179178
}
180179
}
181-
return jco.getBytes() ;
180+
return jco.getBytes();
182181
} catch (Exception e) {
183182
e.printStackTrace();
184-
LOG.error(e.getMessage(),e);
183+
LOG.error(e.getMessage(), e);
185184
} catch (Error e) {
186-
LOG.error(e.getMessage(),e);
185+
LOG.error(e.getMessage(), e);
187186
throw new CodeException(e.toString());
188187
} finally {
189188
if (dynamicClassLoader != null) {
@@ -198,7 +197,7 @@ public byte[] javaCode2Bytes(String fullClassName, String javaCode) throws IOExc
198197
throw new CodeException(error.toString());
199198
}
200199

201-
return null ;
200+
return null;
202201
}
203202

204203
public <T> T javaCodeToObject(String fullClassName, String javaCode) throws IllegalAccessException, InstantiationException, IOException, CodeException {
@@ -239,9 +238,4 @@ public URLClassLoader getParentClassLoader() {
239238
return parentClassLoader;
240239
}
241240

242-
public Class<?> javaCodeToClassFile(String string, String sourcePath) {
243-
// TODO Auto-generated method stub
244-
return null;
245-
}
246-
247241
}

src/main/java/org/nlpcn/jcoder/run/java/JavaRunner.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ private void _instance() {
173173

174174
Mirror<?> mirror = Mirror.me(codeInfo.getClassz());
175175

176-
ClassLoader defaultClassLoader = Thread.currentThread().getContextClassLoader();
177-
178-
Thread.currentThread().setContextClassLoader(DynamicEngine.getInstance().getParentClassLoader());
179176
for (Field field : mirror.getFields()) {
180177
Inject inject = field.getAnnotation(Inject.class);
181178
if (inject != null) {
@@ -191,7 +188,6 @@ private void _instance() {
191188
field.setAccessible(false);
192189
}
193190
}
194-
Thread.currentThread().setContextClassLoader(defaultClassLoader);
195191

196192
if (codeInfo.isSingle()) {
197193
codeInfo.setJavaObject(objInstance);

src/main/java/org/nlpcn/jcoder/run/mvc/JcoderActionChain.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Iterator;
55
import java.util.List;
66

7+
import org.nlpcn.jcoder.run.java.DynamicEngine;
78
import org.nlpcn.jcoder.run.mvc.processor.ApiMethodInvokeProcessor;
89
import org.nutz.lang.Lang;
910
import org.nutz.mvc.ActionChain;
@@ -46,7 +47,7 @@ public JcoderActionChain(List<Processor> list, ApiMethodInvokeProcessor invokePr
4647
}
4748

4849
public void doChain(ActionContext ac) {
49-
if (null != head)
50+
if (null != head) {
5051
try {
5152
head.process(ac);
5253
} catch (Throwable e) {
@@ -57,8 +58,9 @@ public void doChain(ActionContext ac) {
5758
throw Lang.wrapThrow(ee);
5859
}
5960
}
61+
}
6062
}
61-
63+
6264
public ApiMethodInvokeProcessor getInvokeProcessor() {
6365
return invokeProcessor;
6466
}

src/main/java/org/nlpcn/jcoder/run/mvc/processor/ApiActionFiltersProcessor.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,49 @@
2121
*/
2222
public class ApiActionFiltersProcessor extends AbstractProcessor {
2323

24-
protected List<ActionFilter> filters = new ArrayList<ActionFilter>();
25-
26-
protected Processor proxyProcessor;
27-
28-
protected Processor lastProcessor;
29-
30-
public void init(NutConfig config, ActionInfo ai) throws Throwable {
31-
ObjectInfo<? extends ActionFilter>[] filterInfos = ai.getFilterInfos();
32-
if (null != filterInfos) {
33-
for (int i = 0; i < filterInfos.length; i++) {
34-
ActionFilter filter = evalObj(config, filterInfos[i]);
35-
filters.add(filter);
36-
if (filter instanceof Processor) {
37-
Processor processor = (Processor)filter;
38-
if (proxyProcessor == null) {
39-
proxyProcessor = processor;
40-
lastProcessor = processor;
41-
} else {
42-
processor.setNext(proxyProcessor);
43-
proxyProcessor = processor;
44-
}
45-
}
46-
}
47-
}
48-
}
24+
protected List<ActionFilter> filters = new ArrayList<ActionFilter>();
4925

50-
public void process(ActionContext ac) throws Throwable {
51-
for (ActionFilter filter : filters) {
52-
View view = filter.match(ac);
53-
if (null != view) {
54-
ac.setMethodReturn(view);
55-
renderView(ac);
56-
return;
57-
}
58-
}
59-
if (proxyProcessor == null) {
60-
doNext(ac);
61-
} else {
62-
if (lastProcessor != null)
63-
lastProcessor.setNext(next);
64-
proxyProcessor.process(ac);
65-
}
66-
}
26+
protected Processor proxyProcessor;
27+
28+
protected Processor lastProcessor;
29+
30+
public void init(NutConfig config, ActionInfo ai) throws Throwable {
31+
ObjectInfo<? extends ActionFilter>[] filterInfos = ai.getFilterInfos();
32+
if (null != filterInfos) {
33+
for (int i = 0; i < filterInfos.length; i++) {
34+
ActionFilter filter = evalObj(config, filterInfos[i]);
35+
filters.add(filter);
36+
if (filter instanceof Processor) {
37+
Processor processor = (Processor) filter;
38+
if (proxyProcessor == null) {
39+
proxyProcessor = processor;
40+
lastProcessor = processor;
41+
} else {
42+
processor.setNext(proxyProcessor);
43+
proxyProcessor = processor;
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
public void process(ActionContext ac) throws Throwable {
51+
52+
for (ActionFilter filter : filters) {
53+
View view = filter.match(ac);
54+
if (null != view) {
55+
ac.setMethodReturn(view);
56+
renderView(ac);
57+
return;
58+
}
59+
}
60+
61+
if (proxyProcessor == null) {
62+
doNext(ac);
63+
} else {
64+
if (lastProcessor != null)
65+
lastProcessor.setNext(next);
66+
proxyProcessor.process(ac);
67+
}
68+
}
6769
}

src/main/java/org/nlpcn/jcoder/scheduler/TaskJob.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.nlpcn.jcoder.scheduler;
22

33
import org.nlpcn.jcoder.domain.Task;
4+
import org.nlpcn.jcoder.run.java.DynamicEngine;
45
import org.nlpcn.jcoder.run.java.JavaRunner;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
@@ -32,13 +33,16 @@ public long getStartTime() {
3233
@Override
3334
public void run() {
3435
over = false;
36+
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
3537
try {
38+
Thread.currentThread().setContextClassLoader(DynamicEngine.getInstance().getParentClassLoader());
3639
new JavaRunner(task).compile().instance().execute() ;
3740
} catch (Exception e) {
3841
e.printStackTrace();
3942
LOG.error(e.getMessage(),e);
4043
} finally {
4144
over = true;
45+
Thread.currentThread().setContextClassLoader(contextClassLoader);
4246
ThreadManager.removeTaskIfOver(this.getName());
4347
}
4448

0 commit comments

Comments
 (0)