Skip to content

Commit

Permalink
fix view null result , and fix default method
Browse files Browse the repository at this point in the history
  • Loading branch information
ansjsun committed Jul 12, 2016
1 parent d53684c commit c44576f
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 48 deletions.
33 changes: 18 additions & 15 deletions src/main/java/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
String[] dim = arg.split("=");
parseFile(dim[1]);
break;
}else{
} else {
System.err.println("are you sure ? -f not -f=file ! it not can use!");
}
}
Expand All @@ -50,8 +50,8 @@ public static void main(String[] args) throws Exception {
ENV_MAP.put(PREFIX + "maven", dim[1]);
}
}
}else if(!arg.startsWith("-f")){
System.err.println("arg : "+arg +" can use ! not find = over it ");
} else if (!arg.startsWith("-f")) {
System.err.println("arg : " + arg + " can use ! not find = over it ");
}
}

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

System.setProperty("java.awt.headless", "true"); //support kaptcha

Server server = new Server(port);

ProtectionDomain domain = Bootstrap.class.getProtectionDomain();

URL location = domain.getCodeSource().getLocation();

WebAppContext context = new WebAppContext();
Expand All @@ -92,9 +92,9 @@ public static void main(String[] args) throws Exception {
} else {
context.setWar("src/main/webapp");
}

server.setHandler(context);

server.start();
server.join();
}
Expand All @@ -108,12 +108,16 @@ public static void main(String[] args) throws Exception {
*/
private static void createLog4jConfig(File log4jFile, String logPath) throws FileNotFoundException, IOException {

if (log4jFile.exists()) {
return;
}

StringBuilder sb = new StringBuilder();

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

sb.append(logPath);

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


File tmpDir = new File(JcoderHome, "tmp"); // create tmp dir
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}

File pluginDir = new File(JcoderHome, "plugins"); // create tmp dir
if (!pluginDir.exists()) {
pluginDir.mkdirs();
Expand All @@ -186,7 +189,7 @@ private static void makeFiles(File JcoderHome, String logPath) throws FileNotFou
if (!resourceDir.exists()) {
resourceDir.mkdirs();
}

File iocFile = new File(JcoderHome, "/resource/ioc.js"); // create ioc file
if (!iocFile.exists()) {
wirteFile(iocFile.getAbsolutePath(), "utf-8", "var ioc = {\n\n};");
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/nlpcn/jcoder/run/java/JavaRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public JavaRunner compile() {

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

codeInfo.setClassz(clz);

if (single != null) {
codeInfo.setSingle(single.value());
}
Expand Down Expand Up @@ -197,8 +199,7 @@ public Task getTask() {
private static final Object[] DEFAULT_ARG = new Object[0];

/**
* execte task defaultExecute if not found , it execute excutemehtod , if
* not found it throw Exception
* execte task defaultExecute if not found , it execute excutemehtod , if not found it throw Exception
*
* @return
*
Expand All @@ -217,8 +218,7 @@ public Object execute() {
}

/**
* execte task defaultExecute if not found , it execute excutemehtod , if
* not found it throw Exception
* execte task defaultExecute if not found , it execute excutemehtod , if not found it throw Exception
*
* @return
*
Expand Down Expand Up @@ -258,8 +258,6 @@ public boolean check() throws CodeException, IOException {

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

clz.getAnnotation(Single.class);

MapCount<String> mc = new MapCount<>();
// set execute method
for (Method method : clz.getMethods()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void process(ActionContext ac) throws Throwable {
if (re != null) {
view.render(ac.getRequest(), ac.getResponse(), re);
} else if (err == null) {
view.render(ac.getRequest(), ac.getResponse(), StaticValue.OK);
view.render(ac.getRequest(), ac.getResponse(), null);
}

doNext(ac);
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/nlpcn/jcoder/run/mvc/view/JsonView.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
} else {
resp.setStatus(httpStatus);
}

if (obj == null) {
obj = result;
}

if (obj == null) {
return;
}


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

if (obj == null) {
obj = result;
}

if (obj == null) {
return;
}

resp.getWriter().write(toString(obj));
resp.flushBuffer();
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/nlpcn/jcoder/run/mvc/view/JsonpView.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
} else {
resp.setStatus(httpStatus);
}

if (obj == null) {
obj = result;
}

if (obj == null) {
return;
}

resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("text/javascript");
// crossorigin
resp.addHeader("Access-Control-Allow-Origin", ORIGIN);
resp.addHeader("Access-Control-Allow-Methods", METHODS);
resp.addHeader("Access-Control-Allow-Headers", HEADERS);
resp.addHeader("Access-Control-Allow-Credentials", CREDENTIALS);
if (obj == null) {
obj = result;
}

if (obj == null) {
return;
}

StringBuilder sb = new StringBuilder();

Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/nlpcn/jcoder/run/mvc/view/TextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
public class TextView implements View {

private static final String ORIGIN = "*";
private static final String METHODS = "get, post, put, delete, options";
private static final String HEADERS = "origin, content-type, accept";
private static final String CREDENTIALS = "true";

private int httpStatus = ApiException.OK;
private Object result;

Expand All @@ -40,16 +45,23 @@ public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
} else {
resp.setStatus(httpStatus);
}

resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("text/html");

if (obj == null) {
obj = result;
}

if (obj == null) {
return;
}

resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("text/html");
// crossorigin
resp.addHeader("Access-Control-Allow-Origin", ORIGIN);
resp.addHeader("Access-Control-Allow-Methods", METHODS);
resp.addHeader("Access-Control-Allow-Headers", HEADERS);
resp.addHeader("Access-Control-Allow-Credentials", CREDENTIALS);


resp.getWriter().write(obj.toString());
resp.flushBuffer();
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/apidoc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1>{{y.name}}</h1>

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

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

<h2 ng-show="y.sub.length>0">Parameter</h2>
<table ng-show="y.sub.length>0">
Expand Down
38 changes: 30 additions & 8 deletions src/test/java/org/nlpcn/jcoder/run/java/CronTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
package org.nlpcn.jcoder.run.java;


import java.util.Date;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.log4j.Logger;
import org.nlpcn.commons.lang.util.IOUtil;
import org.nlpcn.jcoder.run.annotation.DefaultExecute;
import org.nlpcn.jcoder.util.DateUtils;
import org.nutz.ioc.loader.annotation.Inject;

public class CronTest {

@Inject
private Logger log ;
private Logger log;

@DefaultExecute
public void execute() throws InterruptedException {
Thread.sleep(10000L);
log.info(DateUtils.formatDate(new Date(), DateUtils.SDF_FORMAT));
public void execute() throws InterruptedException, UnsupportedEncodingException, FileNotFoundException {

log.info("中文");

log.info("中文");
log.info("中文");

log.info("中文");
log.info("中文");

log.info("中文");
log.info("中文");

log.info("中文");
log.info("中文");

log.info("中文");
List<String> readFile2List = IOUtil.readFile2List("C:\\pdf\\log\\jcoder.log", "utf-8");

for (String string : readFile2List) {
System.out.println("aaaaaaaaaaaa"+string);
}

}

}
Loading

0 comments on commit c44576f

Please sign in to comment.