Skip to content

Commit 82985cf

Browse files
author
孙健
committed
up
1 parent b233fda commit 82985cf

File tree

4 files changed

+223
-15
lines changed

4 files changed

+223
-15
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,5 @@ public URLClassLoader getParentClassLoader() {
238238
return parentClassLoader;
239239
}
240240

241+
241242
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,4 @@ public boolean check() throws CodeException, IOException {
302302
}
303303

304304

305-
public static void main(String[] args) {
306-
long start = System.currentTimeMillis() ;
307-
for (int i = 0; i < 1000000; i++) {
308-
// System.out.println("1234567890");
309-
LOG.info("1234567890");
310-
}
311-
System.out.println(System.currentTimeMillis()-start);
312-
}
313305
}

src/main/java/org/nlpcn/jcoder/util/Testing.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
import java.nio.file.SimpleFileVisitor;
1111
import java.nio.file.attribute.BasicFileAttributes;
1212
import java.util.ArrayList;
13-
import java.util.Arrays;
13+
import java.util.EnumSet;
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
17-
import java.util.ResourceBundle;
18-
import java.util.UUID;
1917

18+
import javax.servlet.DispatcherType;
19+
20+
import org.eclipse.jetty.server.Server;
21+
import org.eclipse.jetty.servlet.ServletContextHandler;
2022
import org.nlpcn.commons.lang.util.FileFinder;
2123
import org.nlpcn.commons.lang.util.IOUtil;
22-
import org.nlpcn.commons.lang.util.ObjConver;
2324
import org.nlpcn.commons.lang.util.StringUtil;
24-
import org.nlpcn.jcoder.server.rpc.domain.RpcRequest;
2525
import org.nutz.http.Http;
2626
import org.nutz.ioc.Ioc;
2727
import org.nutz.ioc.impl.NutIoc;
@@ -43,7 +43,7 @@ public class Testing {
4343

4444
private static final Logger LOG = LoggerFactory.getLogger(Testing.class);
4545

46-
public static final String CODE_RUN = "123__CODE__RUN";
46+
private static final String IOC_PATH = "src/test/resources/ioc.js";
4747

4848
/**
4949
* instan task by ioc
@@ -97,7 +97,6 @@ public static <T> T instance(Class<T> c) throws Exception {
9797

9898
}
9999

100-
101100
/**
102101
* 对比本地代码和线上代码的不同
103102
*
@@ -140,4 +139,51 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
140139
});
141140
}
142141

142+
/**
143+
* 以本地测试服务的方式启动
144+
*
145+
* @param port
146+
* @param iocPath
147+
* @param jcoderHome
148+
* @throws Exception
149+
*/
150+
public static void startServer(int port, String iocPath, String jcoderHome, String[] packages) throws Exception {
151+
if (port <= 0) {
152+
port = 8080;
153+
}
154+
if (StringUtil.isBlank(iocPath)) {
155+
iocPath = IOC_PATH;
156+
}
157+
158+
if (StringUtil.isBlank(jcoderHome)) {
159+
jcoderHome = new File(System.getProperty("user.home"), ".jcoder").getAbsolutePath();
160+
}
161+
162+
System.setProperty(StaticValue.PREFIX + "home", jcoderHome);
163+
System.setProperty(StaticValue.PREFIX + "port", String.valueOf(port));
164+
165+
Server server = new Server(port);
166+
167+
Ioc ioc = new NutIoc(new JsonLoader(iocPath));
168+
StaticValue.setSystemIoc(ioc);
169+
StaticValue.setUserIoc(ioc);
170+
171+
TestingFilter.init(packages);
172+
173+
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
174+
context.addFilter(TestingFilter.class, "/api/*", EnumSet.of(DispatcherType.REQUEST));
175+
server.setHandler(context);
176+
177+
server.start();
178+
179+
System.out.println("jcoder testing server start ok");
180+
181+
server.join();
182+
183+
}
184+
185+
public static void main(String[] args) throws Exception {
186+
Testing.startServer(8080, "/Users/sunjian/Documents/workspace/infcn_mobile/src/test/resources/ioc.js", null, new String[]{"cn"});
187+
}
188+
143189
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package org.nlpcn.jcoder.util;
2+
3+
import java.io.IOException;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Method;
6+
import java.lang.reflect.Modifier;
7+
import java.net.URL;
8+
import java.util.Enumeration;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import javax.servlet.FilterChain;
14+
import javax.servlet.FilterConfig;
15+
import javax.servlet.ServletException;
16+
import javax.servlet.ServletRequest;
17+
import javax.servlet.ServletResponse;
18+
import javax.servlet.http.HttpServletRequest;
19+
import javax.servlet.http.HttpServletResponse;
20+
21+
import org.nlpcn.commons.lang.util.StringUtil;
22+
import org.nlpcn.jcoder.run.annotation.DefaultExecute;
23+
import org.nlpcn.jcoder.run.annotation.Execute;
24+
import org.nlpcn.jcoder.run.java.ClassUtil;
25+
import org.nlpcn.jcoder.run.java.DynamicEngine;
26+
import org.nlpcn.jcoder.run.mvc.processor.ApiAdaptorProcessor;
27+
import org.nlpcn.jcoder.run.mvc.processor.ApiCrossOriginProcessor;
28+
import org.nlpcn.jcoder.run.mvc.view.JsonView;
29+
import org.nlpcn.jcoder.util.StaticValue;
30+
import org.nutz.ioc.loader.annotation.Inject;
31+
import org.nutz.lang.Mirror;
32+
import org.nutz.mvc.ActionContext;
33+
import org.nutz.mvc.ActionInfo;
34+
import org.nutz.mvc.Mvcs;
35+
import org.nutz.mvc.NutFilter;
36+
import org.nutz.resource.Scans;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
39+
40+
public class TestingFilter extends NutFilter {
41+
42+
private static final Logger LOG = LoggerFactory.getLogger(TestingFilter.class);
43+
44+
private static Map<String,Method> methods = null ;
45+
46+
public static void init(String... packages) throws IOException {
47+
Map<String,Method> tempMethods = new HashMap<>() ;
48+
for (String pk : packages) {
49+
List<Class<?>> list = Scans.me().scanPackage(pk);
50+
51+
list.forEach(cla -> {
52+
for (Method method : cla.getMethods()) {
53+
String name = cla.getSimpleName() ;
54+
if (method.getAnnotationsByType(Execute.class) != null || method.getAnnotationsByType(DefaultExecute.class) != null) {
55+
56+
tempMethods.put(cla.getName().substring(beginIndex, endIndex), value)
57+
}
58+
}
59+
});
60+
}
61+
}
62+
63+
@Override
64+
public void init(FilterConfig conf) throws ServletException {
65+
sc = conf.getServletContext();
66+
}
67+
68+
@Override
69+
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
70+
71+
HttpServletRequest request = (HttpServletRequest) req;
72+
HttpServletResponse response = (HttpServletResponse) resp;
73+
74+
Mvcs.setIoc(StaticValue.getUserIoc()); // reset ioc
75+
76+
Mvcs.setServletContext(sc);
77+
ActionContext ac = new ActionContext();
78+
try {
79+
Mvcs.set("testing_filter", request, response);
80+
81+
ac.setRequest(request).setResponse(response).setServletContext(request.getServletContext());
82+
Mvcs.setActionContext(ac);
83+
84+
new ApiCrossOriginProcessor().process(ac);
85+
86+
Class<?> clz = Class.forName("cn.com.infcn.mobile.api.LoginAction");
87+
88+
ac.setModule(clz.newInstance());
89+
90+
Map<String, Method> methodMap = new HashMap<>();
91+
for (Method method : clz.getMethods()) {
92+
if (!Modifier.isPublic(method.getModifiers()) || method.isBridge() || method.getDeclaringClass() != clz) {
93+
continue;
94+
}
95+
96+
if (Mirror.getAnnotationDeep(method, DefaultExecute.class) != null || Mirror.getAnnotationDeep(method, Execute.class) != null) {
97+
98+
if (methodMap.containsKey(method.getName())) {
99+
throw new RuntimeException("method name repeated: " + method.getName());
100+
}
101+
102+
methodMap.put(method.getName(), method);
103+
}
104+
}
105+
106+
Method method = methodMap.get("login");
107+
108+
if (method == null) {
109+
throw new RuntimeException("not found method : ");
110+
}
111+
112+
ac.setMethod(methodMap.get("login"));
113+
114+
ActionInfo actionInfo = new ActionInfo();
115+
116+
actionInfo.setMethod(ac.getMethod());
117+
actionInfo.setModuleType(clz);
118+
119+
ApiAdaptorProcessor apiAdaptorProcessor = new ApiAdaptorProcessor();
120+
apiAdaptorProcessor.init(Mvcs.getNutConfig(), actionInfo);
121+
apiAdaptorProcessor.process(ac);
122+
123+
Mirror<?> mirror = Mirror.me(clz);
124+
125+
for (Field field : mirror.getFields()) {
126+
Inject inject = field.getAnnotation(Inject.class);
127+
if (inject != null) {
128+
field.setAccessible(true);
129+
if (field.getType().equals(org.apache.log4j.Logger.class)) {
130+
LOG.warn("org.apache.log4j.Logger Deprecated please use org.slf4j.Logger by LoggerFactory");
131+
mirror.setValue(ac.getModule(), field, org.apache.log4j.Logger.getLogger(ac.getModule().getClass()));
132+
} else if (field.getType().equals(org.slf4j.Logger.class)) {
133+
mirror.setValue(ac.getModule(), field, LoggerFactory.getLogger(ac.getModule().getClass()));
134+
} else {
135+
mirror.setValue(ac.getModule(), field,
136+
StaticValue.getUserIoc().get(field.getType(), StringUtil.isBlank(inject.value()) ? field.getName() : inject.value()));
137+
}
138+
field.setAccessible(false);
139+
}
140+
}
141+
142+
Object invoke = ac.getMethod().invoke(ac.getModule(), ac.getMethodArgs());
143+
144+
new JsonView().render(request, response, invoke);
145+
} catch (Throwable e) {
146+
e.printStackTrace();
147+
try {
148+
new JsonView().render(request, response, e);
149+
} catch (Throwable e1) {
150+
// TODO Auto-generated catch block
151+
e1.printStackTrace();
152+
}
153+
} finally {
154+
Mvcs.set(null, null, null);
155+
Mvcs.ctx().removeReqCtx();
156+
Mvcs.setServletContext(null);
157+
if (request.getSession(false) != null && request.getSession(false).getAttribute("user") == null) { //if session is empty
158+
request.getSession().invalidate();
159+
}
160+
}
161+
162+
}
163+
164+
@Override
165+
public void destroy() {
166+
System.out.println("destroy");
167+
}
168+
169+
}

0 commit comments

Comments
 (0)