-
Notifications
You must be signed in to change notification settings - Fork 55
/
XmlConfigServer.java
68 lines (57 loc) · 1.99 KB
/
XmlConfigServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.monitorjbl.json.server;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class XmlConfigServer implements ConfigServer {
public static final Logger log = LoggerFactory.getLogger(XmlConfigServer.class);
private boolean running;
private Thread thread;
public synchronized void start(final int port) {
if(thread != null) {
throw new IllegalStateException("Server is already running");
}
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Server server = new Server(port);
final XmlWebApplicationContext xmlBasedContext = new XmlWebApplicationContext();
//System.out.println(xmlBasedContext.getEnvironment().getClass());
xmlBasedContext.setConfigLocation("classpath:context.xml");
final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(xmlBasedContext));
final ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
server.setHandler(context);
running = true;
server.start();
log.info("Server started");
while(running) {
Thread.sleep(1);
}
server.stop();
log.info("Server stopped");
} catch(Exception e) {
log.error("Server exception", e);
throw new RuntimeException(e);
}
}
});
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new XmlConfigServer().start(9090);
}
}