1 16 17 package samples.transport.tcp; 18 19 import org.apache.axis.AxisEngine; 20 import org.apache.axis.AxisFault; 21 import org.apache.axis.Message; 22 import org.apache.axis.MessageContext; 23 import org.apache.axis.components.logger.LogFactory; 24 import org.apache.axis.configuration.XMLStringProvider; 25 import org.apache.axis.deployment.wsdd.WSDDConstants; 26 import org.apache.axis.server.AxisServer; 27 import org.apache.axis.utils.Options; 28 import org.apache.commons.logging.Log; 29 30 import java.io.BufferedOutputStream ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.net.MalformedURLException ; 36 import java.net.ServerSocket ; 37 import java.net.Socket ; 38 import java.net.URL ; 39 40 47 public class TCPListener implements Runnable { 48 static Log log = 49 LogFactory.getLog(TCPSender.class.getName()); 50 51 private String transportName = "TCPTransport"; 53 54 private static final String AXIS_ENGINE = "AxisEngine" ; 55 56 private int port; 57 private ServerSocket srvSocket; 58 59 private AxisEngine engine = null ; 60 61 private boolean done = false; 63 64 static final String wsdd = 65 "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" " + 66 "xmlns:java=\"" + WSDDConstants.URI_WSDD_JAVA + "\">\n" + 67 " <transport name=\"tcp\" pivot=\"java:samples.transport.tcp.TCPSender\"/>\n" + 68 " <service name=\"" + WSDDConstants.URI_WSDD + "\" provider=\"java:MSG\">\n" + 69 " <parameter name=\"allowedMethods\" value=\"AdminService\"/>\n" + 70 " <parameter name=\"className\" value=\"org.apache.axis.utils.Admin\"/>\n" + 71 " </service>\n" + 72 "</deployment>"; 73 74 public static void main (String args[]) { 75 new TCPListener(args).run(); 76 } 77 78 public TCPListener (String [] args) { 79 try { 81 Options options = new Options(args); 82 port = new URL (options.getURL()).getPort(); 83 } catch (MalformedURLException ex) { 84 log.error("Hosed URL: "+ex); 85 System.exit(1); 86 } 87 88 try { 89 srvSocket = new ServerSocket (port); 90 } catch (IOException ex) { 91 log.error("Can't create server socket on port "+port); 92 System.exit(1); 93 } 94 95 log.info("TCPListener is listening on port "+port+"."); 96 } 97 98 public void run () { 99 if (srvSocket == null) { 100 return; 101 } 102 103 Socket sock; 104 while (!done) { 105 try { 106 sock = srvSocket.accept(); 107 log.info("TCPListener received new connection: "+sock); 108 new Thread (new SocketHandler(sock)).start(); 109 } catch (IOException ex) { 110 113 log.debug("Got IOException on srvSocket.accept: "+ex); 114 } 115 } 116 } 117 118 119 public class SocketHandler implements Runnable { 120 private Socket socket; 121 public SocketHandler (Socket socket) { 122 this.socket = socket; 123 } 124 public void run () { 125 if ( engine == null ) { 127 XMLStringProvider provider = new XMLStringProvider(wsdd); 128 engine = new AxisServer(provider); 129 engine.init(); 130 } 131 132 133 134 135 136 137 138 MessageContext msgContext = new MessageContext(engine); 139 140 InputStream inp; 141 try { 142 inp = socket.getInputStream(); 143 } catch (IOException ex) { 144 log.error("Couldn't get input stream from "+socket); 145 return; 146 } 147 148 Message msg = null; 153 try { 154 StringBuffer line = new StringBuffer (); 155 int b = 0; 156 while ((b = inp.read()) != '\r') { 157 line.append((char)b); 158 } 159 if (inp.read() != '\n') { 161 log.error("Length line "+line+" was not terminated with \r\n"); 162 return; 163 } 164 165 if (line.toString().equals("ping")) { 169 socket.getOutputStream().write(new String ("\n").getBytes()); 170 return; 171 } else if (line.toString().equals("quit")) { 172 socket.getOutputStream().write(new String ("\n").getBytes()); 174 socket.close(); 175 log.error("AxisListener quitting."); 179 System.exit(0); 180 } 181 182 183 int len = Integer.parseInt(line.toString()); 185 187 193 byte[] mBytes = new byte[len]; 194 inp.read(mBytes); 195 msg = new Message(new ByteArrayInputStream (mBytes)); 196 } catch (IOException ex) { 197 log.error("Couldn't read from socket input stream: "+ex); 198 return; 199 } 200 201 202 203 204 msgContext.setRequestMessage( msg ); 205 206 207 208 msgContext.setTransportName(transportName); 209 210 try { 211 212 213 engine.invoke( msgContext ); 214 } 215 catch( Exception e ) { 216 AxisFault fault = AxisFault.makeFault(e); 217 msgContext.setResponseMessage( new Message(fault) ); 218 } 219 220 221 222 msg = msgContext.getResponseMessage(); 223 String response = null; 224 if (msg == null) { 225 response="No data"; 226 } else { 227 try { 228 response = (String ) msg.getSOAPPartAsString(); 229 } catch (AxisFault fault) { 230 msg = new Message(fault); 231 try { 232 response = (String )msg.getSOAPPartAsString(); 233 } catch (AxisFault fault2) { 234 response = fault2.dumpToString(); 235 } 236 } 237 } 238 239 try { 240 OutputStream buf = new BufferedOutputStream (socket.getOutputStream()); 241 buf.write(response.getBytes()); 244 buf.close(); 245 } catch (IOException ex) { 246 log.error("Can't write response to socket "+port+", response is: "+response); 247 } 248 } 249 } 250 } 251 252 253 | Popular Tags |