Skip to content

Commit

Permalink
add log to code . and support config log path
Browse files Browse the repository at this point in the history
  • Loading branch information
ansjsun committed Jul 1, 2016
1 parent de7a1de commit f8774c7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 40 deletions.
47 changes: 37 additions & 10 deletions src/main/java/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.SpinnerListModel;

import org.apache.log4j.PropertyConfigurator;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.webapp.WebAppContext;
Expand Down Expand Up @@ -46,13 +46,19 @@ public static void main(String[] args) throws Exception {
ENV_MAP.put(PREFIX + "home", dim[1]);
} else if (dim[0].equals("--log")) {
ENV_MAP.put(PREFIX + "log", dim[1]);
} else if (dim[0].equals("--maven")) {
ENV_MAP.put(PREFIX + "maven", dim[1]);
}
}
}
}

getOrCreateEnv(PREFIX + "maven", "mvn");
getOrCreateEnv(PREFIX + "host", null);
getOrCreateEnv(PREFIX + "log", "log/jcoder.log");
String logPath = getOrCreateEnv(PREFIX + "log", "log/jcoder.log");

createLog4jConfig(logPath);

String home = getOrCreateEnv(PREFIX + "home", new File(System.getProperty("user.home"), ".jcoder").getAbsolutePath());
int port = Integer.parseInt(getOrCreateEnv(PREFIX + "port", "8080"));

Expand Down Expand Up @@ -92,6 +98,26 @@ public static void main(String[] args) throws Exception {
server.join();
}

/**
* config log4j setting
*
* @param logPath
*/
private static void createLog4jConfig(String logPath) {
Properties pro = new Properties();
pro.put("log4j.rootLogger", "info, stdout,R");
pro.put("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
pro.put("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout ");
pro.put("log4j.appender.stdout.layout.ConversionPattern", "%c-%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}] %m%n");
pro.put("log4j.appender.R", "org.apache.log4j.DailyRollingFileAppender");
pro.put("log4j.appender.R.File", logPath);
pro.put("log4j.appender.R.DatePattern ", " '.'yyyy-MM-dd");
pro.put("log4j.appender.R.layout", "org.apache.log4j.PatternLayout");
pro.put("log4j.appender.R.layout.ConversionPattern", "%d{HH:mm:ss} %c{1} %-5p %m%n");
pro.put("log4j.logger.org.atmosphere.cpr.AsynchronousProcessor", "FATAL");
PropertyConfigurator.configure(pro);
}

private static void parseFile(String file) throws UnsupportedEncodingException, FileNotFoundException, IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"))) {
String temp = null;
Expand All @@ -108,7 +134,7 @@ private static void parseFile(String file) throws UnsupportedEncodingException,
continue;
}

String[] split = temp.split("\\s+");
String[] split = temp.split("=");

if (split.length == 2) {
String key = split[0].trim();
Expand All @@ -129,11 +155,12 @@ private static void makeFiles(File JcoderHome) throws FileNotFoundException, IOE
if (!libDir.exists()) {
libDir.mkdirs();
wirteFile(new File(libDir, "pom.xml").getAbsolutePath(), "utf-8",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + " <modelVersion>4.0.0</modelVersion>\n"
+ " <groupId>org.nlpcn</groupId>\n" + " <artifactId>jcoder</artifactId>\n" + " <version>1.2</version>\n" + " <description>use maven to down jars</description>\n"
+ " \n" + " <dependencies>\n" + "\n" + " </dependencies>\n" + "\n" + " <build> \n" + " <defaultGoal>compile</defaultGoal>\n" + " </build>\n"
+ "</project>");
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
+ " <modelVersion>4.0.0</modelVersion>\n" + " <groupId>org.nlpcn</groupId>\n" + " <artifactId>jcoder</artifactId>\n" + " <version>1.2</version>\n"
+ " <description>use maven to down jars</description>\n" + " \n" + " <dependencies>\n" + "\n" + " </dependencies>\n" + "\n" + " <build> \n"
+ " <defaultGoal>compile</defaultGoal>\n" + " </build>\n" + "</project>");
}

File iocFile = new File(JcoderHome, "ioc.js"); // create ioc file
Expand Down Expand Up @@ -176,7 +203,7 @@ private static String getOrCreateEnv(String key, String def) {
if (value != null) {
System.setProperty(key, value);
}

return value;
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/nlpcn/jcoder/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.ioc.provider.ComboIocProvider;
import org.nutz.mvc.ioc.provider.AnnotationIocProvider;

@Modules(packages = {"org.nlpcn.jcoder.controller"}, scanPackage = true)
@IocBy(type = ComboIocProvider.class, args = {"*org.nutz.ioc.loader.json.JsonLoader", "*org.nutz.ioc.loader.annotation.AnnotationIocLoader", "org.nlpcn.jcoder"})
@Modules(packages = { "org.nlpcn.jcoder.controller" }, scanPackage = true)
@IocBy(type = AnnotationIocProvider.class, args = { "org.nlpcn.jcoder" })
@Encoding(input = "UTF-8", output = "UTF-8")
@SetupBy(SiteSetup.class)
public class App {

}

4 changes: 2 additions & 2 deletions src/main/java/org/nlpcn/jcoder/controller/JarAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ private void authValidateView(Long groupId) {
@Ok("redirect:/jar/list")
public Object remove(@Param("path") String path) throws IOException {
if (JarService.removeJar(new File(path))) {
return StaticValue.okMessage("成功删除jar:" + path);
return StaticValue.okMessage("delete jar:" + path);
} else {
return StaticValue.errMessage("失败删除jar:" + path + " 可能是因为这不是一个jar包,或者jar包是maven管理的");
return StaticValue.errMessage("delete jar fail :" + path + " may be it is not a jar or it a maven jar");
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/nlpcn/jcoder/service/JarService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class JarService {

private static final String CONFIG_PATH = JAR_PATH + "/config.properties";

private static final String MAVEN_PATH = "MAVEN_PATH";
private static final String MAVEN_PATH = "maven";

private static final String MD5 = "MD5";

Expand Down Expand Up @@ -104,6 +104,10 @@ public static String getMavenPath() {
}

if (StringUtil.isBlank(mavenPath)) {
mavenPath = System.getProperty(StaticValue.PREFIX + "maven");
}

if(StringUtil.isBlank(mavenPath)){
String home = getPathByVar("MAVEN_HOME");
if (StringUtil.isBlank(home)) {
home = getPathByVar("M2_HOME");
Expand All @@ -114,6 +118,7 @@ public static String getMavenPath() {
mavenPath = home + "/bin/mvn";
}
}

return mavenPath;
}

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/nlpcn/jcoder/util/StaticValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.apache.log4j.Logger;
import org.nlpcn.commons.lang.util.MD5;
import org.nlpcn.commons.lang.util.StringUtil;
import org.nlpcn.jcoder.App;
import org.nlpcn.jcoder.run.mvc.ApiUrlMappingImpl;
import org.nlpcn.jcoder.util.dao.BasicDao;
Expand All @@ -22,14 +21,13 @@ public class StaticValue {

private static final Logger LOG = Logger.getLogger(StaticValue.class);

private static final String PREFIX = "jcoder_";
public static final String PREFIX = "jcoder_";

public static final String HOME = System.getProperty(PREFIX + "home");
public static final String HOST = System.getProperty(PREFIX + "host");
public static final String LOG_PATH = System.getProperty(PREFIX + "log");
public static final String HOME = getValueOrCreate("home", new File(System.getProperty("user.home"), ".jcoder").getAbsolutePath());
public static final String HOST = getValueOrCreate("host", null);
public static final String LOG_PATH = getValueOrCreate("log", "log/jcoder.log");

public static final File HOME_FILE = new File(HOME);

public static final File RESOURCE_FILE = new File(HOME_FILE, "resource");
public static final File LIB_FILE = new File(HOME_FILE, "lib");

Expand Down Expand Up @@ -67,6 +65,15 @@ public static String errMessage(Exception e) {
return error;
}

private static String getValueOrCreate(String key, String def) {
String value = System.getProperty(PREFIX + key);
if (value == null) {
return def;
} else {
return value;
}
}

/**
* 失败消息
*
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/ioc.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/resources/log4j.properties

This file was deleted.

0 comments on commit f8774c7

Please sign in to comment.