1 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 ; 31 import java.io.IOException ; 32 import java.net.ServerSocket ; 33 import java.net.Socket ; 34 35 44 public class SimpleHTTPServer extends TransportListener implements Runnable { 45 48 protected Log log = LogFactory.getLog(SimpleHTTPServer.class.getName()); 49 50 53 protected ConfigurationContext configurationContext; 54 55 58 protected ServerSocket serverSocket; 59 60 64 private boolean stopped = false; 65 66 private boolean chuncked = false; 67 private int port; 68 69 public SimpleHTTPServer() { 70 } 71 72 77 public SimpleHTTPServer(ConfigurationContext systemContext, ServerSocket serverSoc) { 78 this.configurationContext = systemContext; 79 this.serverSocket = serverSoc; 80 } 81 82 88 public SimpleHTTPServer(String dir, ServerSocket serverSoc) throws AxisFault { 89 try { 90 this.serverSocket = serverSoc; 91 ConfigurationContextFactory erfac = new ConfigurationContextFactory(); 93 this.configurationContext = erfac.buildConfigurationContext(dir); 94 Thread.sleep(2000); 95 } catch (Exception e1) { 96 throw new AxisFault("Thread interuptted", e1); 97 } 98 } 99 100 105 protected void finalize() throws Throwable { 106 stop(); 107 super.finalize(); 108 } 109 110 114 public void run() { 115 try { 116 while (!stopped) { 117 Socket socket = null; 119 try { 120 socket = serverSocket.accept(); 121 } catch (java.io.InterruptedIOException iie) { 122 } catch (Exception 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 e) { 131 log.error(e); 132 } 133 stop(); 134 log.info("Simple Axis Server Quit"); 135 } 136 137 142 public ServerSocket getServerSocket() { 143 return serverSocket; 144 } 145 146 153 public void setServerSocket(ServerSocket serverSocket) { 154 this.serverSocket = serverSocket; 155 } 156 157 162 public void start() throws AxisFault { 163 if(serverSocket == null){ 164 serverSocket = ListenerManager.openSocket(port); 165 } 166 167 Thread newThread = new Thread (this); 168 newThread.start(); 169 } 170 171 176 public void stop() { 177 log.info("stop called"); 178 179 if (stopped) { 181 return; 182 } 183 184 188 stopped = true; 189 try { 190 if (serverSocket != null) { 191 serverSocket.close(); 192 193 } 203 } catch (IOException e) { 204 log.info(e); 205 } finally { 206 serverSocket = null; 207 } 208 log.info("Simple Axis Server Quits"); 209 } 210 211 216 public ConfigurationContext getSystemContext() { 217 return configurationContext; 218 } 219 220 226 public static void main(String [] args) throws Exception { 227 if (args.length != 2) { 228 System.out.println("SimpleHTTPServer repositoryLocation port"); 229 } 230 ServerSocket serverSoc = null; 231 serverSoc = new ServerSocket (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 (args[0]).getAbsolutePath()); 238 reciver.setServerSocket(serverSoc); 239 Thread thread = new Thread (reciver); 240 thread.setDaemon(true); 241 try { 242 System.out.println("[Axis2] Using the Repository " + new File (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 254 public EndpointReference replyToEPR(String 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 ) param.getValue()); 266 } 267 } 268 269 } 270 | Popular Tags |