KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > http > JettyAxisServer


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.http;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.utils.Messages;
21 import org.apache.axis.utils.Options;
22 import org.apache.commons.logging.Log;
23 import org.mortbay.http.HttpContext;
24 import org.mortbay.http.HttpServer;
25 import org.mortbay.http.SocketListener;
26 import org.mortbay.http.handler.ResourceHandler;
27 import org.mortbay.jetty.servlet.ServletHandler;
28
29 import java.net.MalformedURLException JavaDoc;
30
31 public class JettyAxisServer {
32     protected static Log log =
33             LogFactory.getLog(JettyAxisServer.class.getName());
34
35     /**
36      * Jetty HTTP Server *
37      */

38     HttpServer server = new HttpServer();
39
40     /**
41      * Socket Listener *
42      */

43     SocketListener listener = new SocketListener();
44
45     /**
46      * HTTP Context
47      */

48     HttpContext context = new HttpContext();
49
50     public JettyAxisServer() {
51         // Create a context
52
context.setContextPath("/axis/*");
53         server.addContext(context);
54       
55         // Create a servlet container
56
ServletHandler servlets = new ServletHandler();
57         context.addHandler(servlets);
58
59         // Map a servlet onto the container
60
servlets.addServlet("AdminServlet", "/servlet/AdminServlet",
61                 "org.apache.axis.transport.http.AdminServlet");
62         servlets.addServlet("AxisServlet", "/servlet/AxisServlet",
63                 "org.apache.axis.transport.http.AxisServlet");
64         servlets.addServlet("AxisServlet", "/services/*",
65                 "org.apache.axis.transport.http.AxisServlet");
66         servlets.addServlet("AxisServlet", "*.jws",
67                 "org.apache.axis.transport.http.AxisServlet");
68         context.addHandler(new ResourceHandler());
69     }
70
71     /**
72      * Set the port
73      *
74      * @param port
75      */

76     public void setPort(int port) {
77         listener.setPort(port);
78         server.addListener(listener);
79     }
80
81     /**
82      * Set the resource base
83      *
84      * @param dir
85      */

86     public void setResourceBase(String JavaDoc dir) {
87         context.setResourceBase(dir);
88     }
89
90     /**
91      * Start the server
92      *
93      * @throws Exception
94      */

95     public void start() throws Exception JavaDoc {
96         server.start();
97         log.info(
98                 Messages.getMessage("start00", "JettyAxisServer",
99                         new Integer JavaDoc(listener.getServerSocket().getLocalPort()).toString()));
100     }
101
102     public static void main(String JavaDoc[] args) {
103         Options opts = null;
104         try {
105             opts = new Options(args);
106         } catch (MalformedURLException JavaDoc e) {
107             log.error(Messages.getMessage("malformedURLException00"), e);
108             return;
109         }
110         JettyAxisServer server = new JettyAxisServer();
111         server.setPort(opts.getPort());
112         String JavaDoc dir = opts.isValueSet('d');
113         if (dir == null) {
114             // Serve static content from the context
115
dir = System.getProperty("jetty.home", ".") + "/webapps/axis/";
116         }
117         server.setResourceBase(dir);
118         
119         // Start the http server
120
try {
121             server.start();
122         } catch (Exception JavaDoc e) {
123             log.error(Messages.getMessage("exception00"), e);
124         }
125     }
126 }
Popular Tags