1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.quickstart;
19
20 import java.io.File;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.mortbay.http.SocketListener;
25 import org.mortbay.jetty.Server;
26 import org.mortbay.jetty.servlet.WebApplicationContext;
27
28 /***
29 * To start a Jetty server used by the QuickStart application.
30 */
31 public class JettyServer {
32 /***
33 * The system property name used to specify a directory of webapps.
34 */
35 public static final String WEBAPPS_DIR_PROPERTY = "webapps.dir";
36
37 public static void startServer(int port, String context, List pathPriority, Map paths, String resolver) throws Exception {
38 try {
39 Server server = new Server();
40 SocketListener socketListener = new SocketListener();
41 socketListener.setPort(port);
42 server.addListener(socketListener);
43
44 WebApplicationContext ctx;
45 if (resolver == null) {
46 ctx = new MultiWebApplicationContext(pathPriority, paths);
47 } else {
48 ctx = new MultiWebApplicationContext(pathPriority, paths, resolver);
49 }
50 ctx.setClassLoader(Thread.currentThread().getContextClassLoader());
51 ctx.setContextPath(context);
52 server.addContext(null, ctx);
53
54
55 String webappsDir = System.getProperty(WEBAPPS_DIR_PROPERTY);
56 if (webappsDir != null && new File(webappsDir).exists()) {
57 server.addWebApplications(webappsDir);
58 }
59
60 server.start();
61 } catch (Exception e) {
62 e.printStackTrace();
63 }
64 }
65 }