KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > axis2 > listener > soap > AxisJettyServer


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.axis2.listener.soap;
23
24 import java.net.SocketException JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
27
28 import org.apache.axis2.AxisFault;
29 import org.apache.axis2.Constants;
30 import org.apache.axis2.addressing.EndpointReference;
31 import org.apache.axis2.context.ConfigurationContext;
32 import org.apache.axis2.description.Parameter;
33 import org.apache.axis2.description.TransportInDescription;
34 import org.apache.axis2.engine.ListenerManager;
35 import org.apache.axis2.transport.TransportListener;
36 import org.apache.axis2.transport.http.server.HttpUtils;
37 import org.mortbay.jetty.Server;
38 import org.mortbay.jetty.handler.ContextHandlerCollection;
39 import org.mortbay.jetty.servlet.Context;
40 import org.mortbay.jetty.servlet.ServletHolder;
41
42 /**
43  * Create a Jetty server that will be the axis2 transportlistener. This Jetty
44  * server is used to replace the SimpleHTTPServer provided by axis that have
45  * quite bad performances.
46  *
47  * @author Christophe Hamerling - EBM WebSourcing
48  *
49  */

50 public class AxisJettyServer implements TransportListener {
51
52     /**
53      * The Jetty server
54      */

55     private Server server;
56
57     /**
58      * The host address
59      */

60     private String JavaDoc hostAddress = null;
61
62     /**
63      * The server port
64      */

65     protected int port;
66
67     /**
68      * The axis2 configuration context (built from axis2.xml config file)
69      */

70     protected ConfigurationContext configurationContext;
71
72     /**
73      * Creates a new instance of AxisJettyServer
74      *
75      * @param configurationContext
76      * @param port
77      */

78     public AxisJettyServer(ConfigurationContext configurationContext, int port)
79         throws AxisFault {
80         this.port = port;
81         this.configurationContext = configurationContext;
82         this.server = new Server(this.port);
83         
84         // create context handlers
85
ContextHandlerCollection contexts = new ContextHandlerCollection();
86         server.setHandler(contexts);
87
88         // create the axis context
89
Context axis2 = new Context(contexts, "/axis2", Context.SESSIONS);
90
91         // create servlet holders
92
ServletHolder axisServlet = new ServletHolder(new AxisJettyServlet(
93             configurationContext));
94         axisServlet.setName("AxisServlet");
95         axisServlet.setInitOrder(1);
96
97         // REST servlet holder
98
/*
99          * ServletHolder axisRESTServlet = new ServletHolder(
100          * org.apache.axis2.transport.http.AxisRESTServlet.class);
101          * axisRESTServlet.setName("AxisRESTServlet");
102          */

103
104         // admin servlet holder
105
/*
106          * ServletHolder axisAdminServlet = new ServletHolder(
107          * org.apache.axis2.transport.http.AxisAdminServlet.class);
108          * axisAdminServlet.setName("AxisAdminServlet");
109          */

110
111         // do servlet mappings
112
axis2.addServlet(axisServlet, "/services/*");
113         // axis2.addServlet(axisRESTServlet, "/rest/*");
114
axis2.addServlet(axisServlet, "/servlet/AxisServlet");
115         // axis2.addServlet(axisAdminServlet, "/axis2-admin/*");
116

117         // AXIS
118
ListenerManager listenerManager = configurationContext
119             .getListenerManager();
120         TransportInDescription trsIn = new TransportInDescription(new QName JavaDoc(
121             Constants.TRANSPORT_HTTP));
122         trsIn.setReceiver(this);
123         if (listenerManager == null) {
124             listenerManager = new ListenerManager();
125             listenerManager.init(configurationContext);
126         }
127         listenerManager.addListener(trsIn, true);
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.apache.axis2.transport.TransportListener#start()
134      */

135     public void start() throws AxisFault {
136         // start the jetty server in a separate thread
137
Runnable JavaDoc httpListenerRunnable = new Runnable JavaDoc() {
138             public void run() {
139                 try {
140                     server.start();
141                 } catch (Exception JavaDoc e) {
142                     e.printStackTrace(System.err);
143                 }
144             }
145         };
146         Thread JavaDoc httpThread = new Thread JavaDoc(httpListenerRunnable,
147             "Jetty started Thread");
148
149         try {
150             httpThread.start();
151             httpThread.join();
152         } catch (InterruptedException JavaDoc e) {
153             throw new AxisFault(e.getMessage());
154         } catch (Exception JavaDoc e) {
155             throw new AxisFault(e.getMessage());
156         }
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.apache.axis2.transport.TransportListener#stop()
163      */

164     public void stop() throws AxisFault {
165         try {
166             // stop jetty server
167
server.stop();
168         } catch (InterruptedException JavaDoc e) {
169             throw AxisFault.makeFault(e);
170         } catch (Exception JavaDoc e) {
171             throw AxisFault.makeFault(e);
172         }
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see org.apache.axis2.transport.TransportListener#init(org.apache.axis2.context.ConfigurationContext,
179      * org.apache.axis2.description.TransportInDescription)
180      */

181     public void init(ConfigurationContext axisConf,
182         TransportInDescription transprtIn) throws AxisFault {
183         try {
184             this.configurationContext = axisConf;
185
186             Parameter param = transprtIn.getParameter(PARAM_PORT);
187
188             if (param != null) {
189                 this.port = Integer.parseInt((String JavaDoc) param.getValue());
190             }
191             param = transprtIn.getParameter(HOST_ADDRESS);
192             if (param != null) {
193                 hostAddress = ((String JavaDoc) param.getValue()).trim();
194             }
195         } catch (Exception JavaDoc e) {
196             throw AxisFault.makeFault(e);
197         }
198     }
199
200     /*
201      * (non-Javadoc)
202      *
203      * @see org.apache.axis2.transport.TransportListener#getEPRsForService(java.lang.String,
204      * java.lang.String)
205      */

206     public EndpointReference[] getEPRsForService(String JavaDoc serviceName, String JavaDoc ip)
207         throws AxisFault {
208         // if host address is present
209
if (hostAddress != null) {
210             if (server != null) {
211                 return new EndpointReference[] {new EndpointReference(
212                     hostAddress + "/"
213                         + configurationContext.getServiceContextPath() + "/"
214                         + serviceName)};
215             } else {
216                 throw new AxisFault(
217                     "Unable to generate EPR for the transport : http");
218             }
219         }
220         // if the host address is not present
221
String JavaDoc localAddress;
222         if (ip != null) {
223             localAddress = ip;
224         } else {
225             try {
226                 localAddress = HttpUtils.getIpAddress();
227                 if (localAddress == null) {
228                     localAddress = "127.0.0.1";
229                 }
230             } catch (SocketException JavaDoc e) {
231                 throw AxisFault.makeFault(e);
232             }
233         }
234         if (server != null) {
235             return new EndpointReference[] {new EndpointReference("http://"
236                 + localAddress + ":" + port + "/"
237                 + configurationContext.getServiceContextPath() + "/"
238                 + serviceName)};
239         } else {
240             throw new AxisFault(
241                 "Unable to generate EPR for the transport : http");
242         }
243     }
244
245     /**
246      *
247      * @return
248      */

249     public ConfigurationContext getConfigurationContext() {
250         return this.configurationContext;
251     }
252
253     /*
254      * (non-Javadoc)
255      *
256      * @see org.apache.axis2.transport.TransportListener#getEPRsForService(java.lang.String,
257      * java.lang.String)
258      */

259     public EndpointReference getEPRForService(String JavaDoc serviceName, String JavaDoc ip)
260         throws AxisFault {
261         return getEPRsForService(serviceName, ip)[0];
262     }
263 }
264
Popular Tags