1 18 package org.apache.axis2.transport.tcp; 19 20 import org.apache.axis2.Constants; 21 import org.apache.axis2.context.ConfigurationContext; 22 import org.apache.axis2.context.MessageContext; 23 import org.apache.axis2.description.TransportOutDescription; 24 import org.apache.axis2.engine.AxisEngine; 25 import org.apache.axis2.engine.AxisFault; 26 import org.apache.axis2.om.impl.llom.builder.StAXBuilder; 27 import org.apache.axis2.soap.SOAPEnvelope; 28 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder; 29 import org.apache.axis2.util.threadpool.AxisWorker; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 import javax.xml.namespace.QName ; 34 import javax.xml.stream.XMLInputFactory; 35 import javax.xml.stream.XMLStreamReader; 36 import java.io.IOException ; 37 import java.io.InputStreamReader ; 38 import java.io.OutputStream ; 39 import java.io.Reader ; 40 import java.net.Socket ; 41 42 public class TCPWorker implements AxisWorker { 43 protected Log log = LogFactory.getLog(getClass().getName()); 44 private ConfigurationContext configurationContext; 45 private Socket socket; 46 47 public TCPWorker(ConfigurationContext configurationContext, Socket socket) { 48 this.configurationContext = configurationContext; 49 this.socket = socket; 50 } 51 52 public void doWork() { 53 MessageContext msgContext = null; 54 try { 55 Reader in = new InputStreamReader (socket.getInputStream()); 56 TransportOutDescription transportOut = 57 configurationContext.getAxisConfiguration().getTransportOut( 58 new QName (Constants.TRANSPORT_TCP)); 59 msgContext = 60 new MessageContext( 61 configurationContext, 62 configurationContext.getAxisConfiguration().getTransportIn( 63 new QName (Constants.TRANSPORT_TCP)), 64 transportOut); 65 msgContext.setServerSide(true); 66 OutputStream out = socket.getOutputStream(); 67 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out); 68 69 AxisEngine engine = new AxisEngine(configurationContext); 70 try { 71 XMLStreamReader xmlreader = XMLInputFactory.newInstance().createXMLStreamReader(in); 72 StAXBuilder builder = new StAXSOAPModelBuilder(xmlreader); 73 msgContext.setEnvelope((SOAPEnvelope) builder.getDocumentElement()); 74 } catch (Exception e) { 75 throw new AxisFault(e.getMessage(), e); 76 } 77 engine.receive(msgContext); 78 79 } catch (Throwable e) { 80 try { 81 AxisEngine engine = new AxisEngine(configurationContext); 82 if (msgContext != null) { 83 msgContext.setProperty(MessageContext.TRANSPORT_OUT, socket.getOutputStream()); 84 engine.handleFault(msgContext, e); 85 } 86 } catch (Exception e1) { 87 log.error(e); 88 e.printStackTrace(); 89 } 90 91 } finally { 92 if (socket != null) { 93 try { 94 this.socket.close(); 95 } catch (IOException e1) { 96 e1.printStackTrace(); 98 } 99 } 100 } 101 102 } 103 104 } 105 | Popular Tags |