KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > http > SimpleHTTPServer


1 /*
2  * Copyright 2004,2005 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 package org.apache.axis2.transport.http;
17
18 import org.apache.axis2.addressing.AddressingConstants;
19 import org.apache.axis2.addressing.EndpointReference;
20 import org.apache.axis2.clientapi.ListenerManager;
21 import org.apache.axis2.context.ConfigurationContext;
22 import org.apache.axis2.context.ConfigurationContextFactory;
23 import org.apache.axis2.description.Parameter;
24 import org.apache.axis2.description.TransportInDescription;
25 import org.apache.axis2.engine.AxisFault;
26 import org.apache.axis2.transport.TransportListener;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.net.ServerSocket JavaDoc;
33 import java.net.Socket JavaDoc;
34
35 /**
36  * This is a simple implementation of an HTTP server for processing
37  * SOAP requests via Apache's xml-axis. This is not intended for production
38  * use. Its intended uses are for demos, debugging, and performance
39  * profiling.
40  * Note this classes uses static objects to provide a thread pool, so you should
41  * not use multiple instances of this class in the same JVM/classloader unless
42  * you want bad things to happen at shutdown.
43  */

44 public class SimpleHTTPServer extends TransportListener implements Runnable JavaDoc {
45     /**
46      * Field log
47      */

48     protected Log log = LogFactory.getLog(SimpleHTTPServer.class.getName());
49
50     /**
51      * Field systemContext
52      */

53     protected ConfigurationContext configurationContext;
54
55     /**
56      * Field serverSocket
57      */

58     protected ServerSocket JavaDoc serverSocket;
59
60     /**
61      * are we stopped?
62      * latch to true if stop() is called
63      */

64     private boolean stopped = false;
65     
66     private boolean chuncked = false;
67     private int port;
68
69     public SimpleHTTPServer() {
70     }
71
72     /**
73      * Constructor SimpleHTTPServer
74      *
75      * @param systemContext
76      */

77     public SimpleHTTPServer(ConfigurationContext systemContext, ServerSocket JavaDoc serverSoc) {
78         this.configurationContext = systemContext;
79         this.serverSocket = serverSoc;
80     }
81
82     /**
83      * Constructor SimpleHTTPServer
84      *
85      * @param dir
86      * @throws AxisFault
87      */

88     public SimpleHTTPServer(String JavaDoc dir, ServerSocket JavaDoc serverSoc) throws AxisFault {
89         try {
90             this.serverSocket = serverSoc;
91             // Class erClass = Class.forName("org.apache.axis2.deployment.EngineContextFactoryImpl");
92
ConfigurationContextFactory erfac = new ConfigurationContextFactory();
93             this.configurationContext = erfac.buildConfigurationContext(dir);
94             Thread.sleep(2000);
95         } catch (Exception JavaDoc e1) {
96             throw new AxisFault("Thread interuptted", e1);
97         }
98     }
99
100     /**
101      * stop the server if not already told to.
102      *
103      * @throws Throwable
104      */

105     protected void finalize() throws Throwable JavaDoc {
106         stop();
107         super.finalize();
108     }
109
110     /**
111      * Accept requests from a given TCP port and send them through the
112      * Axis engine for processing.
113      */

114     public void run() {
115         try {
116             while (!stopped) {
117                     // Accept and process requests from the socket
118
Socket JavaDoc socket = null;
119                     try {
120                         socket = serverSocket.accept();
121                     } catch (java.io.InterruptedIOException JavaDoc iie) {
122                     } catch (Exception JavaDoc e) {
123                         log.debug(e);
124                         break;
125                     }
126                     if (socket != null) {
127                         configurationContext.getThreadPool().addWorker(new HTTPWorker(configurationContext,socket));
128                     }
129             }
130         } catch (IOException JavaDoc e) {
131             log.error(e);
132         }
133         stop();
134         log.info("Simple Axis Server Quit");
135     }
136
137     /**
138      * Obtain the serverSocket that that SimpleAxisServer is listening on.
139      *
140      * @return
141      */

142     public ServerSocket JavaDoc getServerSocket() {
143         return serverSocket;
144     }
145
146     /**
147      * Set the serverSocket this server should listen on.
148      * (note : changing this will not affect a running server, but if you
149      * stop() and then start() the server, the new socket will be used).
150      *
151      * @param serverSocket
152      */

153     public void setServerSocket(ServerSocket JavaDoc serverSocket) {
154         this.serverSocket = serverSocket;
155     }
156
157     /**
158      * Start this server as a NON-daemon.
159      *
160      * @throws Exception
161      */

162     public void start() throws AxisFault {
163         if(serverSocket == null){
164             serverSocket = ListenerManager.openSocket(port);
165         }
166         
167         Thread JavaDoc newThread = new Thread JavaDoc(this);
168         newThread.start();
169     }
170
171     /**
172      * Stop this server. Can be called safely if the system is already stopped,
173      * or if it was never started.
174      * This will interrupt any pending accept().
175      */

176     public void stop() {
177         log.info("stop called");
178
179         // recognise use before we are live
180
if (stopped) {
181             return;
182         }
183
184         /*
185          * Close the server socket cleanly, but avoid fresh accepts while
186          * the socket is closing.
187          */

188         stopped = true;
189         try {
190             if (serverSocket != null) {
191                 serverSocket.close();
192
193                 // while(socket != null){
194
// try {
195
// //make sure all sockets closed by the time
196
// //else we got in to lot of trouble testing
197
// Thread.sleep(1000);
198
// } catch (InterruptedException e1) {
199
// log.error(e1);
200
// }
201
// }
202
}
203         } catch (IOException JavaDoc e) {
204             log.info(e);
205         } finally {
206             serverSocket = null;
207         }
208         log.info("Simple Axis Server Quits");
209     }
210
211     /**
212      * Method getSystemContext
213      *
214      * @return
215      */

216     public ConfigurationContext getSystemContext() {
217         return configurationContext;
218     }
219
220     /**
221      * Method main
222      *
223      * @param args
224      * @throws Exception
225      */

226     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
227         if (args.length != 2) {
228             System.out.println("SimpleHTTPServer repositoryLocation port");
229         }
230         ServerSocket JavaDoc serverSoc = null;
231         serverSoc = new ServerSocket JavaDoc(Integer.parseInt(args[1]));
232         SimpleHTTPServer reciver = new SimpleHTTPServer(args[0], serverSoc);
233         System.out.println(
234             "starting SimpleHTTPServer in port "
235                 + args[1]
236                 + " using the repository "
237                 + new File JavaDoc(args[0]).getAbsolutePath());
238         reciver.setServerSocket(serverSoc);
239         Thread JavaDoc thread = new Thread JavaDoc(reciver);
240         thread.setDaemon(true);
241         try {
242             System.out.println("[Axis2] Using the Repository " + new File JavaDoc(args[0]).getAbsolutePath());
243             System.out.println("[Axis2] Starting the SimpleHTTPServer on port "+ args[1]);
244             thread.start();
245              System.out.println("[Axis2] SimpleHTTPServer started");
246             System.in.read();
247         } finally {
248             reciver.stop();
249         }
250     }
251     /* (non-Javadoc)
252      * @see org.apache.axis2.transport.TransportListener#replyToEPR(java.lang.String)
253      */

254     public EndpointReference replyToEPR(String JavaDoc serviceName) {
255         return new EndpointReference(
256             AddressingConstants.WSA_REPLY_TO,
257             "http://127.0.0.1:" + (serverSocket.getLocalPort()) + "/axis/services/" + serviceName);
258     }
259
260     public void init(ConfigurationContext axisConf, TransportInDescription transprtIn)
261         throws AxisFault {
262         this.configurationContext = axisConf;
263         Parameter param = transprtIn.getParameter(PARAM_PORT);
264         if (param != null) {
265             this.port = Integer.parseInt((String JavaDoc) param.getValue());
266         }
267     }
268
269 }
270
Popular Tags