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 edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
023: import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
024: import org.apache.axis2.context.ConfigurationContext;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.http.params.HttpParams;
028:
029: import java.io.IOException;
030:
031: /**
032: * A simple, but configurable and extensible HTTP server.
033: */
034: public class SimpleHttpServer {
035:
036: private static Log LOG = LogFactory.getLog(SimpleHttpServer.class);
037:
038: private static final int SHUTDOWN_GRACE_PERIOD = 3000; // ms
039:
040: private HttpFactory httpFactory;
041: private final int port;
042: private final HttpParams params;
043: private final WorkerFactory workerFactory;
044:
045: private IOProcessor listener = null;
046: private ExecutorService listenerExecutor = null;
047: private HttpConnectionManager connmanager = null;
048: private ExecutorService requestExecutor = null;
049:
050: public SimpleHttpServer(ConfigurationContext configurationContext,
051: WorkerFactory workerFactory, int port) throws IOException {
052: this (
053: new HttpFactory(configurationContext, port,
054: workerFactory), port);
055: }
056:
057: public SimpleHttpServer(HttpFactory httpFactory, int port)
058: throws IOException {
059: this .httpFactory = httpFactory;
060: this .port = port;
061: this .workerFactory = httpFactory.newRequestWorkerFactory();
062: this .params = httpFactory.newRequestConnectionParams();
063: this .params.setIntParameter(AxisParams.LISTENER_PORT, port);
064: }
065:
066: public void init() throws IOException {
067: requestExecutor = httpFactory.newRequestExecutor(port);
068: connmanager = httpFactory.newRequestConnectionManager(
069: requestExecutor, workerFactory, params);
070: listenerExecutor = httpFactory.newListenerExecutor(port);
071: listener = httpFactory.newRequestConnectionListener(port,
072: connmanager, params);
073: }
074:
075: public void destroy() throws IOException, InterruptedException {
076: // Attempt to terminate the listener nicely
077: LOG.info("Shut down connection listener");
078: this .listenerExecutor.shutdownNow();
079: this .listener.destroy();
080: this .listenerExecutor.awaitTermination(SHUTDOWN_GRACE_PERIOD,
081: TimeUnit.MILLISECONDS);
082: if (!this .listenerExecutor.isTerminated()) {
083: // Terminate the listener forcibly
084: LOG.info("Force shut down connection listener");
085: this .listener.destroy();
086: // Leave it up to the garbage collector to clean up the mess
087: this .listener = null;
088: }
089: // Attempt to terminate the active processors nicely
090: LOG.info("Shut down HTTP processors");
091: this .requestExecutor.shutdownNow();
092: this .requestExecutor.awaitTermination(SHUTDOWN_GRACE_PERIOD,
093: TimeUnit.MILLISECONDS);
094: if (!this .requestExecutor.isTerminated()) {
095: // Terminate the active processors forcibly
096: LOG.info("Force shut down HTTP processors");
097: this .connmanager.shutdown();
098: // Leave it up to the garbage collector to clean up the mess
099: this .connmanager = null;
100: }
101: LOG.info("HTTP protocol handler shut down");
102: }
103:
104: public void start() {
105: this .listenerExecutor.execute(this .listener);
106: }
107:
108: public boolean isRunning() {
109: return this .listenerExecutor != null
110: && !this .listenerExecutor.isShutdown();
111: }
112:
113: public int getPort() {
114: return this.port;
115: }
116:
117: }
|