99import java .io .UnsupportedEncodingException ;
1010import java .net .URL ;
1111import java .security .ProtectionDomain ;
12- import java .util .HashMap ;
13- import java .util .Map ;
1412
1513import org .eclipse .jetty .server .Server ;
1614import org .eclipse .jetty .server .handler .HandlerList ;
@@ -21,17 +19,17 @@ public class Bootstrap {
2119
2220 private static final String PREFIX = "jcoder_" ;
2321
24- private static final Map <String , String > ENV_MAP = new HashMap <>();
25-
2622 public static void main (String [] args ) throws Exception {
2723
2824 for (String arg : args ) {
29- if (arg .startsWith ("-f" ) && arg .contains ("=" )) {
30- String [] dim = arg .split ("=" );
31- parseFile (dim [1 ]);
32- break ;
33- } else {
34- System .err .println ("are you sure ? -f not -f=file ! it not can use!" );
25+ if (arg .startsWith ("-f" )) {
26+ if (arg .contains ("=" )) {
27+
28+ String [] dim = arg .split ("=" );
29+ parseFile (dim [1 ]);
30+ } else {
31+ System .err .println ("are you sure ? -f not -f=file ! it not can use!" );
32+ }
3533 }
3634 }
3735
@@ -40,15 +38,15 @@ public static void main(String[] args) throws Exception {
4038 String [] dim = arg .split ("=" );
4139 if (dim .length >= 2 ) {
4240 if (dim [0 ].equals ("--host" )) {
43- ENV_MAP . put (PREFIX + "host" , dim [1 ]);
41+ putEnv (PREFIX + "host" , dim [1 ]);
4442 } else if (dim [0 ].equals ("--port" )) {
45- ENV_MAP . put (PREFIX + "port" , dim [1 ]);
43+ putEnv (PREFIX + "port" , dim [1 ]);
4644 } else if (dim [0 ].equals ("--home" )) {
47- ENV_MAP . put (PREFIX + "home" , dim [1 ]);
45+ putEnv (PREFIX + "home" , dim [1 ]);
4846 } else if (dim [0 ].equals ("--log" )) {
49- ENV_MAP . put (PREFIX + "log" , dim [1 ]);
47+ putEnv (PREFIX + "log" , dim [1 ]);
5048 } else if (dim [0 ].equals ("--maven" )) {
51- ENV_MAP . put (PREFIX + "maven" , dim [1 ]);
49+ putEnv (PREFIX + "maven" , dim [1 ]);
5250 }
5351 }
5452 } else if (!arg .startsWith ("-f" )) {
@@ -64,7 +62,7 @@ public static void main(String[] args) throws Exception {
6462
6563 int port = Integer .parseInt (getOrCreateEnv (PREFIX + "port" , "8080" ));
6664
67- System .setProperty ("java.awt.headless" , "true" ); //support kaptcha
65+ System .setProperty ("java.awt.headless" , "true" ); // support kaptcha
6866
6967 Server server = new Server (port );
7068
@@ -95,7 +93,7 @@ public static void main(String[] args) throws Exception {
9593
9694 HandlerList list = new HandlerList ();
9795
98- WebAppContext web = new WebAppContext (); //add a web site in jcoder
96+ WebAppContext web = new WebAppContext (); // add a web site in jcoder
9997
10098 web .setContextPath ("/web/" );
10199
@@ -132,16 +130,14 @@ private static void createLog4jConfig(File log4jFile, String logPath) throws Fil
132130
133131 StringBuilder sb = new StringBuilder ();
134132
135- sb .append ("log4j.rootLogger=info, stdout,R\n " + "log4j.appender.stdout.Encoding=UTF-8\n " + "log4j.appender.R.Encoding=UTF-8\n "
136- + "log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n " + "log4j.appender.stdout.layout=org.apache.log4j.PatternLayout \n "
137- + "log4j.appender.stdout.layout.ConversionPattern=%c-%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}] %m%n\n " + "\n "
133+ 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 "
134+ + "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 "
138135 + "log4j.appender.R=org.apache.log4j.DailyRollingFileAppender\n " + "log4j.appender.R.File=" );
139136
140137 sb .append (logPath );
141138
142139 sb .append ("\n " + "log4j.appender.R.DatePattern = '.'yyyy-MM-dd\n " + "log4j.appender.R.layout=org.apache.log4j.PatternLayout\n "
143- + "log4j.appender.R.layout.ConversionPattern=%d{HH:mm:ss} %c{1} %-5p %m%n\n " + "\n " + "## Disable other log \n "
144- + "log4j.logger.org.atmosphere.cpr.AsynchronousProcessor=FATAL" );
140+ + "log4j.appender.R.layout.ConversionPattern=%d{HH:mm:ss} %c{1} %-5p %m%n\n " + "\n " + "## Disable other log \n " + "log4j.logger.org.atmosphere.cpr.AsynchronousProcessor=FATAL" );
145141
146142 wirteFile (log4jFile .getAbsolutePath (), "utf-8" , sb .toString ());
147143
@@ -170,7 +166,7 @@ private static void parseFile(String file) throws UnsupportedEncodingException,
170166 if (!key .startsWith (PREFIX )) {
171167 key = PREFIX + key ;
172168 }
173- ENV_MAP . put (key , split [1 ].trim ());
169+ putEnv (key , split [1 ].trim ());
174170 } else {
175171 System .err .println (temp + " format err" );
176172 }
@@ -184,12 +180,11 @@ private static void makeFiles(File JcoderHome, String logPath) throws FileNotFou
184180 if (!libDir .exists ()) {
185181 libDir .mkdirs ();
186182 wirteFile (new File (libDir , "pom.xml" ).getAbsolutePath (), "utf-8" ,
187- "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n "
188- + "<project xmlns=\" http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" \n "
189- + " xsi:schemaLocation=\" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" >\n "
190- + " <modelVersion>4.0.0</modelVersion>\n " + " <groupId>org.nlpcn</groupId>\n " + " <artifactId>jcoder</artifactId>\n " + " <version>1.2</version>\n "
191- + " <description>use maven to down jars</description>\n " + " \n " + " <dependencies>\n " + "\n " + " </dependencies>\n " + "\n " + " <build> \n "
192- + " <defaultGoal>compile</defaultGoal>\n " + " </build>\n " + "</project>" );
183+ "<?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 "
184+ + " 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 "
185+ + " <groupId>org.nlpcn</groupId>\n " + " <artifactId>jcoder</artifactId>\n " + " <version>1.2</version>\n " + " <description>use maven to down jars</description>\n "
186+ + " \n " + " <dependencies>\n " + "\n " + " </dependencies>\n " + "\n " + " <build> \n " + " <defaultGoal>compile</defaultGoal>\n " + " </build>\n "
187+ + "</project>" );
193188 }
194189
195190 File tmpDir = new File (JcoderHome , "tmp" ); // create tmp dir
@@ -216,31 +211,45 @@ private static void makeFiles(File JcoderHome, String logPath) throws FileNotFou
216211 createLog4jConfig (new File (resourceDir , "log4j.properties" ), logPath );
217212 }
218213
214+ /**
215+ * get a env by key , if not exits , to put it
216+ * @param key
217+ * @param def
218+ * @return
219+ */
219220 private static String getOrCreateEnv (String key , String def ) {
220221
221222 if (!key .startsWith (PREFIX )) {
222223 key = PREFIX + key ;
223224 }
224225
225- String value = ENV_MAP .get (key );
226-
227- if (value == null ) {
228- value = System .getProperty (key );
229- }
226+ String value = System .getProperty (key );
230227
231228 if (value == null ) {
232229 value = System .getenv (key );
233230 }
234231
235232 if (value == null ) {
233+ putEnv (key , def );
236234 value = def ;
237235 }
238236
237+ return value ;
238+ }
239+
240+ /**
241+ * put var to java env
242+ * @param key
243+ * @param value
244+ */
245+ private static void putEnv (String key , String value ) {
246+ if (!key .startsWith (PREFIX )) {
247+ key = PREFIX + key ;
248+ }
239249 if (value != null ) {
250+ System .out .println ("put env to system.propertie: " +key +" ============= " +value );
240251 System .setProperty (key , value );
241252 }
242-
243- return value ;
244253 }
245254
246255 /**
0 commit comments