001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.transport.tcp;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.addressing.EndpointReference;
025: import org.apache.axis2.context.ConfigurationContext;
026: import org.apache.axis2.context.ConfigurationContextFactory;
027: import org.apache.axis2.context.MessageContext;
028: import org.apache.axis2.context.SessionContext;
029: import org.apache.axis2.description.Parameter;
030: import org.apache.axis2.description.TransportInDescription;
031: import org.apache.axis2.engine.ListenerManager;
032: import org.apache.axis2.i18n.Messages;
033: import org.apache.axis2.transport.TransportListener;
034: import org.apache.axis2.transport.http.server.HttpUtils;
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: import java.io.File;
039: import java.io.IOException;
040: import java.net.ServerSocket;
041: import java.net.Socket;
042: import java.net.SocketException;
043:
044: /**
045: * Class TCPServer
046: */
047: public class TCPServer implements Runnable, TransportListener {
048: private int port = 8000;
049: private boolean started = false;
050: private static final Log log = LogFactory.getLog(TCPServer.class);
051: private ConfigurationContext configContext;
052: private ServerSocket serversocket;
053: private String hostAddress = null;
054: private String contextPath;
055:
056: public TCPServer() {
057: }
058:
059: public TCPServer(int port, ConfigurationContext configContext)
060: throws AxisFault {
061: try {
062: this .configContext = configContext;
063: serversocket = new ServerSocket(port);
064:
065: ListenerManager listenerManager = configContext
066: .getListenerManager();
067: TransportInDescription trsIn = new TransportInDescription(
068: Constants.TRANSPORT_TCP);
069: trsIn.setReceiver(this );
070: if (listenerManager == null) {
071: listenerManager = new ListenerManager();
072: listenerManager.init(configContext);
073: }
074: listenerManager.addListener(trsIn, true);
075: contextPath = configContext.getServiceContextPath();
076:
077: } catch (IOException e1) {
078: throw AxisFault.makeFault(e1);
079: }
080: }
081:
082: public TCPServer(int port, String dir) throws AxisFault {
083: this (port, ConfigurationContextFactory
084: .createConfigurationContextFromFileSystem(dir, null));
085: }
086:
087: public void init(ConfigurationContext axisConf,
088: TransportInDescription transprtIn) throws AxisFault {
089: this .configContext = axisConf;
090:
091: Parameter param = transprtIn.getParameter(PARAM_PORT);
092:
093: if (param != null) {
094: this .port = Integer.parseInt((String) param.getValue());
095: }
096: param = transprtIn.getParameter(HOST_ADDRESS);
097: if (param != null) {
098: hostAddress = ((String) param.getValue()).trim();
099: }
100: contextPath = configContext.getServiceContextPath();
101: }
102:
103: public static void main(String[] args) throws AxisFault,
104: NumberFormatException {
105: if (args.length != 2) {
106: System.out.println("TCPServer repositoryLocation port");
107: } else {
108: File repository = new File(args[0]);
109:
110: if (!repository.exists()) {
111: System.out
112: .print("Repository file does not exists .. initializing repository");
113: }
114:
115: TCPServer tcpServer = new TCPServer(Integer
116: .parseInt(args[1]), repository.getAbsolutePath());
117:
118: System.out.println("[Axis2] Using the Repository "
119: + repository.getAbsolutePath());
120: System.out
121: .println("[Axis2] Starting the TCP Server on port "
122: + args[1]);
123: tcpServer.start();
124: Runtime.getRuntime().addShutdownHook(new Thread(tcpServer));
125: }
126: }
127:
128: public void run() {
129: while (started) {
130: Socket socket = null;
131:
132: try {
133: socket = serversocket.accept();
134: } catch (java.io.InterruptedIOException iie) {
135: } catch (Exception e) {
136: log.debug(e);
137:
138: break;
139: }
140:
141: if (socket != null) {
142: configContext.getThreadPool().execute(
143: new TCPWorker(configContext, socket));
144: }
145: }
146: }
147:
148: public synchronized void start() throws AxisFault {
149: if (serversocket == null) {
150: serversocket = openSocket(port);
151: }
152: started = true;
153: this .configContext.getThreadPool().execute(this );
154: }
155:
156: /**
157: * Controls the number of server sockets kept open.
158: */
159: public ServerSocket openSocket(int port) throws AxisFault {
160: for (int i = 0; i < 5; i++) {
161: try {
162: return new ServerSocket(port + i);
163: } catch (IOException e) {
164: // What I'm gonna do here. Try again.
165: }
166: }
167:
168: throw new AxisFault(Messages.getMessage("failedToOpenSocket"));
169: }
170:
171: /*
172: * (non-Javadoc)
173: * @see org.apache.axis2.transport.TransportListener#stop()
174: */
175: public void stop() throws AxisFault {
176: try {
177: this .serversocket.close();
178: started = false;
179: } catch (IOException e) {
180: throw AxisFault.makeFault(e);
181: }
182: }
183:
184: public ConfigurationContext getConfigurationContext() {
185: return this .configContext;
186: }
187:
188: /**
189: * I fthe hostAddress parameter is present in axis2.xml then the EPR will be
190: * created by taking the hostAddres into account
191: * (non-Javadoc)
192: *
193: * @see org.apache.axis2.transport.TransportListener#getEPRForService(String, String)
194: */
195: public EndpointReference getEPRForService(String serviceName,
196: String ip) throws AxisFault {
197: EndpointReference[] epRsForService = getEPRsForService(
198: serviceName, ip);
199: return epRsForService != null ? epRsForService[0] : null;
200: }
201:
202: public EndpointReference[] getEPRsForService(String serviceName,
203: String ip) throws AxisFault {
204: //if host address is present
205: if (hostAddress != null) {
206: if (serversocket != null) {
207: // todo this has to fix
208: return new EndpointReference[] { new EndpointReference(
209: hostAddress + "/" + contextPath + serviceName) };
210: } else {
211: log
212: .debug("Unable to generate EPR for the transport tcp");
213: return null;
214: }
215: }
216: if (ip == null) {
217: try {
218: ip = HttpUtils.getIpAddress(configContext
219: .getAxisConfiguration());
220: } catch (SocketException e) {
221: throw AxisFault.makeFault(e);
222: }
223: }
224: if (serversocket != null) {
225: // todo this has to fix
226: return new EndpointReference[] { new EndpointReference(
227: "tcp://" + ip + ":" + (serversocket.getLocalPort())
228: + "/" + contextPath + "/" + serviceName) };
229: } else {
230: log.debug("Unable to generate EPR for the transport tcp");
231: return null;
232: }
233: }
234:
235: public SessionContext getSessionContext(
236: MessageContext messageContext) {
237: return null;
238: }
239:
240: public void destroy() {
241: this.configContext = null;
242: }
243: }
|