Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ansjsun committed Jul 1, 2016
1 parent b998eef commit de7a1de
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 65 deletions.
139 changes: 99 additions & 40 deletions src/main/java/Bootstrap.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,79 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.Map;

import javax.swing.SpinnerListModel;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.webapp.WebAppContext;

public class Bootstrap {

private static final String PREFIX = "jcoder_";

private static final Map<String, String> ENV_MAP = new HashMap<>();

public static void main(String[] args) throws Exception {
String host = null;
int port = 8080;
String contextPath = "/";
boolean forceHttps = false;

for (String arg : args) {
if (arg.startsWith("-f") && arg.contains("=")) {
String[] dim = arg.split("=");
parseFile(dim[1]);
break;
}
}

for (String arg : args) {
if (arg.startsWith("--") && arg.contains("=")) {
String[] dim = arg.split("=");
if (dim.length >= 2) {
if (dim[0].equals("--host")) {
host = dim[1];
ENV_MAP.put(PREFIX + "host", dim[1]);
} else if (dim[0].equals("--port")) {
port = Integer.parseInt(dim[1]);
} else if (dim[0].equals("--prefix")) {
contextPath = dim[1];
} else if (dim[0].equals("--jcoder.home")) {
System.setProperty("jcoder.home", dim[1]);
ENV_MAP.put(PREFIX + "port", dim[1]);
} else if (dim[0].equals("--home")) {
ENV_MAP.put(PREFIX + "home", dim[1]);
} else if (dim[0].equals("--log")) {
ENV_MAP.put(PREFIX + "log", dim[1]);
}
}
}
}

Server server = new Server(port);

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

System.setProperty(JAVA_HOME_NAME, JcoderHome.getAbsolutePath());
Server server = new Server(port);

ProtectionDomain domain = Bootstrap.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();

WebAppContext context = new WebAppContext();

makeFiles(JcoderHome);
File jcoderHome = new File(home);

makeFiles(jcoderHome);

context.setTempDirectory(new File(JcoderHome, "tmp"));
context.setContextPath(contextPath);
context.setTempDirectory(new File(jcoderHome, "tmp"));
context.setContextPath("/");
context.setServer(server);

context.setWelcomeFiles(new String[] { "Home.jsp" });

context.setExtraClasspath(new File(JcoderHome, "resource").getAbsolutePath());
context.setExtraClasspath(new File(jcoderHome, "resource").getAbsolutePath());

context.setInitParameter("org.eclipse.jetty.servlet.DefaultServlet.useFileMappedB uffer", "false");

Expand All @@ -64,10 +84,6 @@ public static void main(String[] args) throws Exception {
context.setWar("src/main/webapp");
}

if (forceHttps) {
context.setInitParameter("org.scalatra.ForceHttps", "true");
}

server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", "-1");
context.setMaxFormContentSize(-1);
server.setHandler(context);
Expand All @@ -76,17 +92,48 @@ public static void main(String[] args) throws Exception {
server.join();
}

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;

while ((temp = br.readLine()) != null) {

temp = temp.trim();

if (StringUtil.isBlank(temp)) {
continue;
}

if (temp.trim().charAt(0) == '#') {
continue;
}

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

if (split.length == 2) {
String key = split[0].trim();
if (!key.startsWith(PREFIX)) {
key = PREFIX + key;
}
ENV_MAP.put(key, split[1].trim());
} else {
System.err.println(temp + " format err");
}

}
}
}

private static void makeFiles(File JcoderHome) throws FileNotFoundException, IOException {
File libDir = new File(JcoderHome, "lib"); // create jar dir
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 All @@ -98,27 +145,39 @@ private static void makeFiles(File JcoderHome) throws FileNotFoundException, IOE
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}


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

private static final String JAVA_HOME_NAME = "jcoder.home";
private static final String EVN_HOME_NAME = "JCODER_HOME";
private static String getOrCreateEnv(String key, String def) {

private static File getJcoderHome() {
String home = System.getProperty(JAVA_HOME_NAME);
if (home != null && home.length() > 0) {
return new File(home);
if (!key.startsWith(PREFIX)) {
key = PREFIX + key;
}
home = System.getenv(EVN_HOME_NAME);
if (home != null && home.length() > 0) {
return new File(home);

String value = ENV_MAP.get(key);

if (value == null) {
value = System.getProperty(key);
}
return new File(System.getProperty("user.home"), ".jcoder");

if (value == null) {
value = System.getenv(key);
}

if (value == null) {
value = def;
}

if (value != null) {
System.setProperty(key, value);
}

return value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/nlpcn/jcoder/controller/JarAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void downDevSDK(HttpServletResponse response, @Param("resource") boolean
}

if (resource) { // is incloud resource dir
File file = new File(StaticValue.HOME + "/resource");
File file = StaticValue.RESOURCE_FILE;
String basePath = new File(StaticValue.HOME).getAbsolutePath();
Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class ResourceAction {

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

private static final File RESOURCE_ROOT = new File(StaticValue.HOME + "/resource");
private static final File RESOURCE_ROOT = StaticValue.RESOURCE_FILE;

@Inject
private TaskService taskService;
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/org/nlpcn/jcoder/util/StaticValue.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nlpcn.jcoder.util;

import java.io.File;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
Expand All @@ -21,7 +22,16 @@ public class StaticValue {

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

public static final String HOME = System.getProperty("jcoder.home");
private 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 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");

private static Ioc systemIoc;

Expand All @@ -43,9 +53,7 @@ public class StaticValue {

public static final String SYSTEM_SPLIT = "SYSTEM_SPLIT_ANSJ";

public static final String LOG_PATH = initLogPath();

//api路径的映射
// api路径的映射
public static final ApiUrlMappingImpl MAPPING = new ApiUrlMappingImpl();

/**
Expand All @@ -59,21 +67,6 @@ public static String errMessage(Exception e) {
return error;
}

/**
* 加载log文件的日志
*
* @return
*/
private static String initLogPath() {
String path = ResourceBundle.getBundle("config").getString("PrintConsole.LogFilePath");

if (StringUtil.isBlank(path)) {
path = "log/jcoder.log";
}

return path;
}

/**
* 失败消息
*
Expand Down Expand Up @@ -203,8 +196,7 @@ public static String getResource(String key) {
public static void initIOC() {
NutzUtil.init(App.class);
}



/**
* md5 code
*
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/config.properties

This file was deleted.

0 comments on commit de7a1de

Please sign in to comment.