-
Notifications
You must be signed in to change notification settings - Fork 330
/
JettyServer.java
90 lines (74 loc) · 3.1 KB
/
JettyServer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.jbake.launcher;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.jbake.app.configuration.JBakeConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.IOException;
/**
* Provides Jetty server related functions
*
* @author Jonathan Bullock <a href="mailto:jonbullock@gmail.com">jonbullock@gmail.com</a>
*/
public class JettyServer implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);
private Server server;
@Deprecated
public void run(String resourceBase, String port) {
LOGGER.warn("DEPRECATED. This method will be removed in the next major release. Use run(String resourceBase, JBakeConfiguration config) instead.");
run(resourceBase, "/", "localhost", Integer.parseInt(port));
}
public void run(String resourceBase, JBakeConfiguration configuration) {
run(resourceBase, configuration.getServerContextPath(), configuration.getServerHostname(), configuration.getServerPort());
}
/**
* Run Jetty web server serving out supplied path on supplied port
*
* @param resourceBase Base directory for resources to be served
* @param port Required server port
*/
private void run(String resourceBase, String contextPath, String hostname, int port) {
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setHost(hostname);
connector.setPort(port);
server.addConnector(connector);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{"index"});
resource_handler.setResourceBase(resourceBase);
ContextHandler contextHandler = new ContextHandler();
contextHandler.setContextPath(contextPath);
contextHandler.setHandler(resource_handler);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{contextHandler, new DefaultHandler()});
server.setHandler(handlers);
LOGGER.info("Serving out contents of: [{}] on http://{}:{}{}", resourceBase, hostname, port, contextHandler.getContextPath());
LOGGER.info("(To stop server hit CTRL-C)");
try {
server.start();
server.join();
} catch (Exception e) {
LOGGER.error("unable to start server", e);
}
}
public boolean isStarted() {
return server != null && server.isStarted();
}
@Override
public void close() throws IOException {
if (server.isRunning()) {
try {
server.stop();
} catch (Exception e) {
LOGGER.error("unable to stop server");
}
}
}
}