KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > tcp > TCPWorker


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  * Runtime state of the engine
17  */

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 JavaDoc;
34 import javax.xml.stream.XMLInputFactory;
35 import javax.xml.stream.XMLStreamReader;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStreamReader JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.Reader JavaDoc;
40 import java.net.Socket JavaDoc;
41
42 public class TCPWorker implements AxisWorker {
43     protected Log log = LogFactory.getLog(getClass().getName());
44     private ConfigurationContext configurationContext;
45     private Socket JavaDoc socket;
46
47     public TCPWorker(ConfigurationContext configurationContext, Socket JavaDoc 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 JavaDoc(socket.getInputStream());
56             TransportOutDescription transportOut =
57                 configurationContext.getAxisConfiguration().getTransportOut(
58                     new QName JavaDoc(Constants.TRANSPORT_TCP));
59             msgContext =
60                 new MessageContext(
61                     configurationContext,
62                     configurationContext.getAxisConfiguration().getTransportIn(
63                         new QName JavaDoc(Constants.TRANSPORT_TCP)),
64                     transportOut);
65             msgContext.setServerSide(true);
66             OutputStream JavaDoc 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 JavaDoc e) {
75                 throw new AxisFault(e.getMessage(), e);
76             }
77             engine.receive(msgContext);
78
79         } catch (Throwable JavaDoc 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 JavaDoc 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 JavaDoc e1) {
96                     // TODO Auto-generated catch block
97
e1.printStackTrace();
98                 }
99             }
100         }
101
102     }
103
104 }
105
Popular Tags