View Javadoc

1   /*
2    * $Id: JettyServer.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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              // Add in extra webapps dir (see WW-1387)
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  }