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.http.server;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.http.params.HttpParams;
025:
026: import java.io.IOException;
027: import java.net.ServerSocket;
028: import java.net.Socket;
029:
030: public class DefaultConnectionListener implements IOProcessor {
031:
032: private static Log LOG = LogFactory
033: .getLog(DefaultConnectionListener.class);
034:
035: private volatile boolean destroyed = false;
036:
037: private final int port;
038: private final HttpConnectionManager connmanager;
039: private final ConnectionListenerFailureHandler failureHandler;
040: private final HttpParams params;
041:
042: private ServerSocket serversocket = null;
043:
044: /**
045: * Use this constructor to provide a custom ConnectionListenerFailureHandler, e.g. by subclassing DefaultConnectionListenerFailureHandler
046: */
047: public DefaultConnectionListener(int port,
048: final HttpConnectionManager connmanager,
049: final ConnectionListenerFailureHandler failureHandler,
050: final HttpParams params) throws IOException {
051: super ();
052: if (connmanager == null) {
053: throw new IllegalArgumentException(
054: "Connection manager may not be null");
055: }
056: if (failureHandler == null) {
057: throw new IllegalArgumentException(
058: "Failure handler may not be null");
059: }
060: if (params == null) {
061: throw new IllegalArgumentException(
062: "HTTP parameters may not be null");
063: }
064: this .port = port;
065: this .connmanager = connmanager;
066: this .failureHandler = failureHandler;
067: this .params = params;
068: }
069:
070: public void run() {
071: try {
072: while (!Thread.interrupted()) {
073: try {
074: if (serversocket == null || serversocket.isClosed()) {
075: serversocket = new ServerSocket(port);
076: serversocket.setReuseAddress(true);
077: if (LOG.isInfoEnabled()) {
078: LOG.info("Listening on port "
079: + this .serversocket.getLocalPort());
080: }
081: }
082: LOG.debug("Waiting for incoming HTTP connection");
083: Socket socket = this .serversocket.accept();
084: if (LOG.isDebugEnabled()) {
085: LOG.debug("Incoming HTTP connection from "
086: + socket.getRemoteSocketAddress());
087: }
088: AxisHttpConnection conn = new AxisHttpConnectionImpl(
089: socket, this .params);
090: this .connmanager.process(conn);
091: } catch (Throwable ex) {
092: if (Thread.interrupted()) {
093: break;
094: }
095: if (!failureHandler.failed(this , ex)) {
096: break;
097: }
098: }
099: }
100: } finally {
101: destroy();
102: }
103: }
104:
105: public void close() throws IOException {
106: if (this .serversocket != null) {
107: this .serversocket.close();
108: }
109: }
110:
111: public void destroy() {
112: this .destroyed = true;
113: try {
114: close();
115: } catch (IOException ex) {
116: if (LOG.isWarnEnabled()) {
117: LOG.warn("I/O error closing listener", ex);
118: }
119: }
120: }
121:
122: public boolean isDestroyed() {
123: return this.destroyed;
124: }
125:
126: }
|