1 16 package org.apache.axis2.transport.tcp; 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.deployment.DeploymentException; 24 import org.apache.axis2.description.Parameter; 25 import org.apache.axis2.description.TransportInDescription; 26 import org.apache.axis2.engine.AxisFault; 27 import org.apache.axis2.transport.TransportListener; 28 import org.apache.axis2.transport.http.SimpleHTTPServer; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.net.ServerSocket ; 35 import java.net.Socket ; 36 37 40 public class TCPServer extends TransportListener implements Runnable { 41 private int port = 8000; 42 private ServerSocket serversocket; 43 private boolean started = false; 44 private ConfigurationContext configContext; 45 46 protected Log log = LogFactory.getLog(SimpleHTTPServer.class.getName()); 47 public TCPServer() { 48 } 49 50 public TCPServer(int port, String dir) throws AxisFault { 51 try { 52 ConfigurationContextFactory erfac = new ConfigurationContextFactory(); 53 ConfigurationContext configContext = erfac.buildConfigurationContext(dir); 54 this.configContext = configContext; 55 Thread.sleep(3000); 56 serversocket = new ServerSocket (port); 57 } catch (DeploymentException e1) { 58 throw new AxisFault(e1); 59 } catch (InterruptedException e1) { 60 throw new AxisFault(e1); 61 } catch (IOException e1) { 62 throw new AxisFault(e1); 63 } 64 } 65 66 public TCPServer(int port, ConfigurationContext configContext) throws AxisFault { 67 try { 68 this.configContext = configContext; 69 serversocket = new ServerSocket (port); 70 } catch (IOException e1) { 71 throw new AxisFault(e1); 72 } 73 } 74 75 public void run() { 76 while (started) { 77 Socket socket = null; 78 try { 79 try { 80 socket = serversocket.accept(); 81 } catch (java.io.InterruptedIOException iie) { 82 } catch (Exception e) { 83 log.debug(e); 84 break; 85 } 86 if (socket != null) { 87 configContext.getThreadPool().addWorker(new TCPWorker(configContext, socket)); 88 } 89 } catch (AxisFault e) { 90 log.error(e); 91 e.printStackTrace(); 92 } 93 } 94 95 } 96 97 public synchronized void start() throws AxisFault { 98 if (serversocket == null) { 99 serversocket = ListenerManager.openSocket(port); 100 } 101 started = true; 102 Thread thread = new Thread (this); 103 thread.start(); 104 } 105 106 109 public EndpointReference replyToEPR(String serviceName) throws AxisFault { 110 return new EndpointReference( 111 AddressingConstants.WSA_REPLY_TO, 112 "tcp://127.0.0.1:" + (serversocket.getLocalPort()) + "/axis/services/" + serviceName); 113 } 114 115 118 public void stop() throws AxisFault { 119 try { 120 this.serversocket.close(); 121 started = false; 122 } catch (IOException e) { 123 throw new AxisFault(e); 124 } 125 } 126 127 public void init(ConfigurationContext axisConf, TransportInDescription transprtIn) 128 throws AxisFault { 129 this.configContext = axisConf; 130 Parameter param = transprtIn.getParameter(PARAM_PORT); 131 if (param != null) { 132 int port = Integer.parseInt((String ) param.getValue()); 133 } 134 135 } 136 public static void main(String [] args) throws AxisFault, NumberFormatException { 137 if (args.length != 2) { 138 System.out.println("TCPServer repositoryLocation port"); 139 } else { 140 File repository = new File (args[0]); 141 if(!repository.exists()){ 142 System.out.print("Repository file does not exists .. initializing repository"); 143 } 144 TCPServer tcpServer = new TCPServer(Integer.parseInt(args[1]), repository.getAbsolutePath()); 145 System.out.println( 146 "[Axis2] Using the Repository " + repository.getAbsolutePath()); 147 System.out.println("[Axis2] Starting the TCP Server on port " + args[1]); 148 tcpServer.start(); 149 } 150 } 151 152 } 153 | Popular Tags |