Skip to content

Commit c44576f

Browse files
committed
fix view null result , and fix default method
1 parent d53684c commit c44576f

File tree

10 files changed

+384
-48
lines changed

10 files changed

+384
-48
lines changed

src/main/java/Bootstrap.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
2929
String[] dim = arg.split("=");
3030
parseFile(dim[1]);
3131
break;
32-
}else{
32+
} else {
3333
System.err.println("are you sure ? -f not -f=file ! it not can use!");
3434
}
3535
}
@@ -50,8 +50,8 @@ public static void main(String[] args) throws Exception {
5050
ENV_MAP.put(PREFIX + "maven", dim[1]);
5151
}
5252
}
53-
}else if(!arg.startsWith("-f")){
54-
System.err.println("arg : "+arg +" can use ! not find = over it ");
53+
} else if (!arg.startsWith("-f")) {
54+
System.err.println("arg : " + arg + " can use ! not find = over it ");
5555
}
5656
}
5757

@@ -63,11 +63,11 @@ public static void main(String[] args) throws Exception {
6363
int port = Integer.parseInt(getOrCreateEnv(PREFIX + "port", "8080"));
6464

6565
System.setProperty("java.awt.headless", "true"); //support kaptcha
66-
66+
6767
Server server = new Server(port);
68-
68+
6969
ProtectionDomain domain = Bootstrap.class.getProtectionDomain();
70-
70+
7171
URL location = domain.getCodeSource().getLocation();
7272

7373
WebAppContext context = new WebAppContext();
@@ -92,9 +92,9 @@ public static void main(String[] args) throws Exception {
9292
} else {
9393
context.setWar("src/main/webapp");
9494
}
95-
95+
9696
server.setHandler(context);
97-
97+
9898
server.start();
9999
server.join();
100100
}
@@ -108,12 +108,16 @@ public static void main(String[] args) throws Exception {
108108
*/
109109
private static void createLog4jConfig(File log4jFile, String logPath) throws FileNotFoundException, IOException {
110110

111+
if (log4jFile.exists()) {
112+
return;
113+
}
114+
111115
StringBuilder sb = new StringBuilder();
112116

113-
sb.append(
114-
"log4j.rootLogger=info, stdout,R\n" + "log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n" + "log4j.appender.stdout.layout=org.apache.log4j.PatternLayout \n"
115-
+ "log4j.appender.stdout.layout.ConversionPattern=%c-%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}] %m%n\n" + "\n"
116-
+ "log4j.appender.R=org.apache.log4j.DailyRollingFileAppender\n" + "log4j.appender.R.File=");
117+
sb.append("log4j.rootLogger=info, stdout,R\n" + "log4j.appender.stdout.Encoding=UTF-8\n" + "log4j.appender.R.Encoding=UTF-8\n"
118+
+ "log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n" + "log4j.appender.stdout.layout=org.apache.log4j.PatternLayout \n"
119+
+ "log4j.appender.stdout.layout.ConversionPattern=%c-%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}] %m%n\n" + "\n"
120+
+ "log4j.appender.R=org.apache.log4j.DailyRollingFileAppender\n" + "log4j.appender.R.File=");
117121

118122
sb.append(logPath);
119123

@@ -170,12 +174,11 @@ private static void makeFiles(File JcoderHome, String logPath) throws FileNotFou
170174
+ " <defaultGoal>compile</defaultGoal>\n" + " </build>\n" + "</project>");
171175
}
172176

173-
174177
File tmpDir = new File(JcoderHome, "tmp"); // create tmp dir
175178
if (!tmpDir.exists()) {
176179
tmpDir.mkdirs();
177180
}
178-
181+
179182
File pluginDir = new File(JcoderHome, "plugins"); // create tmp dir
180183
if (!pluginDir.exists()) {
181184
pluginDir.mkdirs();
@@ -186,7 +189,7 @@ private static void makeFiles(File JcoderHome, String logPath) throws FileNotFou
186189
if (!resourceDir.exists()) {
187190
resourceDir.mkdirs();
188191
}
189-
192+
190193
File iocFile = new File(JcoderHome, "/resource/ioc.js"); // create ioc file
191194
if (!iocFile.exists()) {
192195
wirteFile(iocFile.getAbsolutePath(), "utf-8", "var ioc = {\n\n};");

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public JavaRunner compile() {
7272

7373
Single single = clz.getAnnotation(Single.class);
7474

75+
codeInfo.setClassz(clz);
76+
7577
if (single != null) {
7678
codeInfo.setSingle(single.value());
7779
}
@@ -197,8 +199,7 @@ public Task getTask() {
197199
private static final Object[] DEFAULT_ARG = new Object[0];
198200

199201
/**
200-
* execte task defaultExecute if not found , it execute excutemehtod , if
201-
* not found it throw Exception
202+
* execte task defaultExecute if not found , it execute excutemehtod , if not found it throw Exception
202203
*
203204
* @return
204205
*
@@ -217,8 +218,7 @@ public Object execute() {
217218
}
218219

219220
/**
220-
* execte task defaultExecute if not found , it execute excutemehtod , if
221-
* not found it throw Exception
221+
* execte task defaultExecute if not found , it execute excutemehtod , if not found it throw Exception
222222
*
223223
* @return
224224
*
@@ -258,8 +258,6 @@ public boolean check() throws CodeException, IOException {
258258

259259
Class<?> clz = (Class<?>) de.javaCodeToClass(pack + "." + className, code);
260260

261-
clz.getAnnotation(Single.class);
262-
263261
MapCount<String> mc = new MapCount<>();
264262
// set execute method
265263
for (Method method : clz.getMethods()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void process(ActionContext ac) throws Throwable {
3838
if (re != null) {
3939
view.render(ac.getRequest(), ac.getResponse(), re);
4040
} else if (err == null) {
41-
view.render(ac.getRequest(), ac.getResponse(), StaticValue.OK);
41+
view.render(ac.getRequest(), ac.getResponse(), null);
4242
}
4343

4444
doNext(ac);

src/main/java/org/nlpcn/jcoder/run/mvc/view/JsonView.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
4444
} else {
4545
resp.setStatus(httpStatus);
4646
}
47+
48+
if (obj == null) {
49+
obj = result;
50+
}
51+
52+
if (obj == null) {
53+
return;
54+
}
4755

56+
4857
resp.setHeader("Cache-Control", "no-cache");
4958
resp.setContentType("application/json");
5059
// crossorigin
@@ -53,14 +62,6 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
5362
resp.addHeader("Access-Control-Allow-Headers", HEADERS);
5463
resp.addHeader("Access-Control-Allow-Credentials", CREDENTIALS);
5564

56-
if (obj == null) {
57-
obj = result;
58-
}
59-
60-
if (obj == null) {
61-
return;
62-
}
63-
6465
resp.getWriter().write(toString(obj));
6566
resp.flushBuffer();
6667
}

src/main/java/org/nlpcn/jcoder/run/mvc/view/JsonpView.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,23 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
4242
} else {
4343
resp.setStatus(httpStatus);
4444
}
45+
46+
if (obj == null) {
47+
obj = result;
48+
}
49+
50+
if (obj == null) {
51+
return;
52+
}
53+
4554
resp.setHeader("Cache-Control", "no-cache");
4655
resp.setContentType("text/javascript");
4756
// crossorigin
4857
resp.addHeader("Access-Control-Allow-Origin", ORIGIN);
4958
resp.addHeader("Access-Control-Allow-Methods", METHODS);
5059
resp.addHeader("Access-Control-Allow-Headers", HEADERS);
5160
resp.addHeader("Access-Control-Allow-Credentials", CREDENTIALS);
52-
if (obj == null) {
53-
obj = result;
54-
}
5561

56-
if (obj == null) {
57-
return;
58-
}
5962

6063
StringBuilder sb = new StringBuilder();
6164

src/main/java/org/nlpcn/jcoder/run/mvc/view/TextView.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
public class TextView implements View {
1919

20+
private static final String ORIGIN = "*";
21+
private static final String METHODS = "get, post, put, delete, options";
22+
private static final String HEADERS = "origin, content-type, accept";
23+
private static final String CREDENTIALS = "true";
24+
2025
private int httpStatus = ApiException.OK;
2126
private Object result;
2227

@@ -40,16 +45,23 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
4045
} else {
4146
resp.setStatus(httpStatus);
4247
}
43-
44-
resp.setHeader("Cache-Control", "no-cache");
45-
resp.setContentType("text/html");
48+
4649
if (obj == null) {
4750
obj = result;
4851
}
4952

5053
if (obj == null) {
5154
return;
5255
}
56+
57+
resp.setHeader("Cache-Control", "no-cache");
58+
resp.setContentType("text/html");
59+
// crossorigin
60+
resp.addHeader("Access-Control-Allow-Origin", ORIGIN);
61+
resp.addHeader("Access-Control-Allow-Methods", METHODS);
62+
resp.addHeader("Access-Control-Allow-Headers", HEADERS);
63+
resp.addHeader("Access-Control-Allow-Credentials", CREDENTIALS);
64+
5365

5466
resp.getWriter().write(obj.toString());
5567
resp.flushBuffer();

src/main/webapp/apidoc/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ <h1>{{y.name}}</h1>
6969

7070
<span class="method {{m|lowercase}}" ng-repeat="m in y.methods">{{m}}</span>
7171

72-
<pre class="prettyprint prettyprinted"><code><span class="pln">{{atx}}/api/{{x.name}}{{y.defaultExecute?'':'/'+y.name}}</span></code></pre>
72+
<pre class="prettyprint prettyprinted"><code><span class="pln">{{atx}}/api/{{x.name}}/{{y.name}}</span></code></pre>
7373

7474
<h2 ng-show="y.sub.length>0">Parameter</h2>
7575
<table ng-show="y.sub.length>0">
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
package org.nlpcn.jcoder.run.java;
22

3-
4-
import java.util.Date;
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.UnsupportedEncodingException;
6+
import java.util.List;
57

68
import org.apache.log4j.Logger;
9+
import org.nlpcn.commons.lang.util.IOUtil;
710
import org.nlpcn.jcoder.run.annotation.DefaultExecute;
8-
import org.nlpcn.jcoder.util.DateUtils;
911
import org.nutz.ioc.loader.annotation.Inject;
1012

1113
public class CronTest {
12-
14+
1315
@Inject
14-
private Logger log ;
16+
private Logger log;
1517

1618
@DefaultExecute
17-
public void execute() throws InterruptedException {
18-
Thread.sleep(10000L);
19-
log.info(DateUtils.formatDate(new Date(), DateUtils.SDF_FORMAT));
19+
public void execute() throws InterruptedException, UnsupportedEncodingException, FileNotFoundException {
20+
21+
log.info("中文");
22+
23+
log.info("中文");
24+
log.info("中文");
25+
26+
log.info("中文");
27+
log.info("中文");
28+
29+
log.info("中文");
30+
log.info("中文");
31+
32+
log.info("中文");
33+
log.info("中文");
34+
35+
log.info("中文");
36+
List<String> readFile2List = IOUtil.readFile2List("C:\\pdf\\log\\jcoder.log", "utf-8");
37+
38+
for (String string : readFile2List) {
39+
System.out.println("aaaaaaaaaaaa"+string);
40+
}
41+
2042
}
2143

2244
}

0 commit comments

Comments
 (0)