001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java $
003: * $Revision:575207 $
004: * $Date:2007-09-13 09:57:05 +0200 (Thu, 13 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.mockup;
033:
034: import java.io.IOException;
035: import java.net.InetSocketAddress;
036:
037: import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
038: import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
039: import org.apache.http.nio.NHttpServiceHandler;
040: import org.apache.http.nio.reactor.IOEventDispatch;
041: import org.apache.http.nio.reactor.IOReactorExceptionHandler;
042: import org.apache.http.nio.reactor.IOReactorStatus;
043: import org.apache.http.nio.reactor.ListenerEndpoint;
044: import org.apache.http.params.HttpParams;
045:
046: /**
047: * Trivial test server based on HttpCore NIO
048: *
049: * @author Oleg Kalnichevski
050: */
051: public class TestHttpServer {
052:
053: private final DefaultListeningIOReactor ioReactor;
054: private final HttpParams params;
055:
056: private volatile IOReactorThread thread;
057: private ListenerEndpoint endpoint;
058:
059: public TestHttpServer(final HttpParams params) throws IOException {
060: super ();
061: this .ioReactor = new DefaultListeningIOReactor(2, params);
062: this .params = params;
063: }
064:
065: public HttpParams getParams() {
066: return this .params;
067: }
068:
069: public void setExceptionHandler(
070: final IOReactorExceptionHandler exceptionHandler) {
071: this .ioReactor.setExceptionHandler(exceptionHandler);
072: }
073:
074: private void execute(final NHttpServiceHandler serviceHandler)
075: throws IOException {
076: IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
077: serviceHandler, this .params);
078:
079: this .ioReactor.execute(ioEventDispatch);
080: }
081:
082: public ListenerEndpoint getListenerEndpoint() {
083: return this .endpoint;
084: }
085:
086: public void setEndpoint(ListenerEndpoint endpoint) {
087: this .endpoint = endpoint;
088: }
089:
090: public void start(final NHttpServiceHandler serviceHandler) {
091: this .endpoint = this .ioReactor.listen(new InetSocketAddress(0));
092: this .thread = new IOReactorThread(serviceHandler);
093: this .thread.start();
094: }
095:
096: public IOReactorStatus getStatus() {
097: return this .ioReactor.getStatus();
098: }
099:
100: public void join(long timeout) throws InterruptedException {
101: if (this .thread != null) {
102: this .thread.join(timeout);
103: }
104: }
105:
106: public Exception getException() {
107: if (this .thread != null) {
108: return this .thread.getException();
109: } else {
110: return null;
111: }
112: }
113:
114: public void shutdown() throws IOException {
115: this .ioReactor.shutdown();
116: try {
117: join(500);
118: } catch (InterruptedException ignore) {
119: }
120: }
121:
122: private class IOReactorThread extends Thread {
123:
124: private final NHttpServiceHandler serviceHandler;
125:
126: private volatile Exception ex;
127:
128: public IOReactorThread(final NHttpServiceHandler serviceHandler) {
129: super ();
130: this .serviceHandler = serviceHandler;
131: }
132:
133: @Override
134: public void run() {
135: try {
136: execute(this .serviceHandler);
137: } catch (IOException ex) {
138: this .ex = ex;
139: } catch (RuntimeException ex) {
140: this .ex = ex;
141: }
142: }
143:
144: public Exception getException() {
145: return this.ex;
146: }
147:
148: }
149:
150: }
|