KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.ServerSocket JavaDoc;
35 import java.net.Socket JavaDoc;
36
37 /**
38  * Class HTTPTransportReceiver
39  */

40 public class TCPServer extends TransportListener implements Runnable JavaDoc {
41     private int port = 8000;
42     private ServerSocket JavaDoc 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 JavaDoc 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 JavaDoc(port);
57         } catch (DeploymentException e1) {
58             throw new AxisFault(e1);
59         } catch (InterruptedException JavaDoc e1) {
60             throw new AxisFault(e1);
61         } catch (IOException JavaDoc 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 JavaDoc(port);
70         } catch (IOException JavaDoc e1) {
71             throw new AxisFault(e1);
72         }
73     }
74
75     public void run() {
76         while (started) {
77             Socket JavaDoc socket = null;
78             try {
79                 try {
80                     socket = serversocket.accept();
81                 } catch (java.io.InterruptedIOException JavaDoc iie) {
82                 } catch (Exception JavaDoc 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 JavaDoc thread = new Thread JavaDoc(this);
103         thread.start();
104     }
105
106     /* (non-Javadoc)
107      * @see org.apache.axis2.transport.TransportListener#replyToEPR(java.lang.String)
108      */

109     public EndpointReference replyToEPR(String JavaDoc 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     /* (non-Javadoc)
116      * @see org.apache.axis2.transport.TransportListener#stop()
117      */

118     public void stop() throws AxisFault {
119         try {
120             this.serversocket.close();
121             started = false;
122         } catch (IOException JavaDoc 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 JavaDoc) param.getValue());
133         }
134
135     }
136     public static void main(String JavaDoc[] args) throws AxisFault, NumberFormatException JavaDoc {
137         if (args.length != 2) {
138             System.out.println("TCPServer repositoryLocation port");
139         } else {
140             File JavaDoc repository = new File JavaDoc(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