Skip to content

Commit 9f52486

Browse files
committed
fix host name and schuede bug
1 parent 1191d01 commit 9f52486

File tree

8 files changed

+73
-59
lines changed

8 files changed

+73
-59
lines changed

src/main/java/Bootstrap.java

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.io.UnsupportedEncodingException;
1010
import java.net.URL;
1111
import java.security.ProtectionDomain;
12-
import java.util.HashMap;
13-
import java.util.Map;
1412

1513
import org.eclipse.jetty.server.Server;
1614
import 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
/**

src/main/java/org/nlpcn/jcoder/controller/ApiAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ApiAction {
3535
*
3636
* @return api info
3737
*/
38-
@At("/api")
38+
@At("/apidoc/info")
3939
@Ok("json")
4040
@Filters(@By(type = CrossOriginFilter.class))
4141
public Object api(@Param(value = "type", df = "1") int type) {

src/main/java/org/nlpcn/jcoder/filter/JcoderFilter.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import javax.servlet.FilterChain;
66
import javax.servlet.FilterConfig;
7-
import javax.servlet.ServletContext;
87
import javax.servlet.ServletException;
98
import javax.servlet.ServletRequest;
109
import javax.servlet.ServletResponse;
@@ -41,9 +40,7 @@ public void doFilter(final ServletRequest req, final ServletResponse resp, final
4140
if (path.startsWith("/api/")) {
4241
_doFilter(chain, request, response);
4342
} else {
44-
if (host == null) {
45-
super.doFilter(request, response, chain);
46-
} else if (host.equals(request.getServerName())) {
43+
if (host == null || host.equals(request.getServerName()) || request.getServletPath().startsWith("/apidoc")) {
4744
super.doFilter(request, response, chain);
4845
} else {
4946
_doAuthoErr(response);

src/main/java/org/nlpcn/jcoder/scheduler/QuartzSchedulerManager.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class QuartzSchedulerManager {
2323

2424
private static final Logger LOG = Logger.getLogger(QuartzSchedulerManager.class);
2525

26-
private static Scheduler scheduler = init();
26+
private static Scheduler scheduler ;
2727

2828
/**
2929
* 初始化
@@ -57,7 +57,7 @@ private static Scheduler init() {
5757
* @param scheduleStr
5858
* @throws SchedulerException
5959
*/
60-
public static synchronized boolean addJob(Task task) throws SchedulerException {
60+
protected static synchronized boolean addJob(Task task) throws SchedulerException {
6161
// task已激活
6262
if (task.getStatus() == 0) {
6363
LOG.info(task.getName() + " status not active to skip!");
@@ -75,12 +75,12 @@ public static synchronized boolean addJob(Task task) throws SchedulerException {
7575
}
7676
}
7777

78-
public static JobDetail makeJobDetail(Task task) {
78+
protected static JobDetail makeJobDetail(Task task) {
7979
JobDetail job = JobBuilder.newJob(QuartzJob.class).withIdentity(task.getName()).build();
8080
return job;
8181
}
8282

83-
public static Trigger makeTrigger(Task task) {
83+
protected static Trigger makeTrigger(Task task) {
8484
String scheduleStr = task.getScheduleStr();
8585
if (StringUtil.isBlank(task.getScheduleStr()) || "while".equalsIgnoreCase(scheduleStr)) {
8686
return TriggerBuilder.newTrigger().build();
@@ -96,7 +96,7 @@ public static Trigger makeTrigger(Task task) {
9696
* @throws SchedulerException
9797
* @throws Exception
9898
*/
99-
public static synchronized boolean stopTaskJob(String taskName) throws SchedulerException {
99+
protected static synchronized boolean stopTaskJob(String taskName) throws SchedulerException {
100100
scheduler.deleteJob(JobKey.jobKey(taskName));
101101
return true;
102102
}
@@ -117,14 +117,16 @@ public static synchronized boolean checkExists(String taskName) throws Scheduler
117117
*
118118
* @throws SchedulerException
119119
*/
120-
public static void startScheduler() throws SchedulerException {
120+
protected static void startScheduler() throws SchedulerException {
121121
scheduler = init();
122122
}
123123

124-
public static void stopScheduler() throws SchedulerException {
125-
scheduler.clear();
126-
scheduler.shutdown();
127-
scheduler = null;
124+
protected static void stopScheduler() throws SchedulerException {
125+
if (scheduler != null) {
126+
scheduler.clear();
127+
scheduler.shutdown();
128+
scheduler = null;
129+
}
128130

129131
}
130132

@@ -134,7 +136,7 @@ public static void stopScheduler() throws SchedulerException {
134136
* @return
135137
* @throws SchedulerException
136138
*/
137-
public static List<Task> getTaskList() throws SchedulerException {
139+
protected static List<Task> getTaskList() throws SchedulerException {
138140
if (scheduler == null) {
139141
return Collections.emptyList();
140142
}

src/main/java/org/nlpcn/jcoder/service/TaskService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ public void initTaskFromDB() throws TaskException {
218218
HashSet<String> taskSet = new HashSet<>();
219219

220220
list.forEach(ti -> taskSet.add(ti.getName()));
221+
222+
ThreadManager.stopScheduler();
223+
224+
ThreadManager.startScheduler();
221225

222226
for (Task task : search) {
223227
try {

src/main/java/org/nlpcn/jcoder/util/StaticValue.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class StaticValue {
3131
public static final File LIB_FILE = new File(HOME_FILE, "lib");
3232
public static final File PLUGIN_FILE = new File(HOME_FILE, "plugins");
3333

34-
3534
private static Ioc systemIoc;
3635

3736
private static Ioc userIoc;
@@ -68,6 +67,9 @@ public static String errMessage(Exception e) {
6867

6968
private static String getValueOrCreate(String key, String def) {
7069
String value = System.getProperty(PREFIX + key);
70+
if (LOG.isDebugEnabled()) {
71+
LOG.debug("get property " + key + ":" + value);
72+
}
7173
if (value == null) {
7274
return def;
7375
} else {
@@ -138,10 +140,10 @@ public static Ioc getUserIoc() {
138140
}
139141
return userIoc;
140142
}
141-
143+
142144
// default ioc is userIoc
143-
public static Ioc getIoc(){
144-
return getUserIoc() ;
145+
public static Ioc getIoc() {
146+
return getUserIoc();
145147
}
146148

147149
public static Ioc getSystemIoc() {

src/main/webapp/apidoc/vendor/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var apiApp = angular.module('apiApp', []);
22
apiApp.controller('apiCtrl', function($scope, $http, $location) {
33

44
//'vendor/api.json'
5-
$http.get('/api').success(function(response) {
5+
$http.get('/apidoc/info').success(function(response) {
66
$scope.datas = response;
77

88
$scope.atx = $location.$$protocol+"://"+$location.$$host+":"+$location.$$port;

src/test/resources/log4j.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
log4j.rootLogger=info, stdout,R
22

3-
log4j.appender.stdout.Encoding=UTF-8
4-
log4j.appender.R.Encoding=UTF-8
3+
log4j.appender.stdout.Encoding=UTF-8
4+
log4j.appender.R.Encoding=UTF-8
55

66
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
77
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

0 commit comments

Comments
 (0)