001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.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.DefaultClientIOEventDispatch;
038: import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
039: import org.apache.http.nio.NHttpClientHandler;
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.params.HttpParams;
044:
045: public class TestHttpClient {
046:
047: private final DefaultConnectingIOReactor ioReactor;
048: private final HttpParams params;
049:
050: private volatile IOReactorThread thread;
051:
052: public TestHttpClient(final HttpParams params) throws IOException {
053: super ();
054: this .ioReactor = new DefaultConnectingIOReactor(2, params);
055: this .params = params;
056: }
057:
058: public HttpParams getParams() {
059: return this .params;
060: }
061:
062: public void setExceptionHandler(
063: final IOReactorExceptionHandler exceptionHandler) {
064: this .ioReactor.setExceptionHandler(exceptionHandler);
065: }
066:
067: private void execute(final NHttpClientHandler clientHandler)
068: throws IOException {
069: IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(
070: clientHandler, this .params);
071:
072: this .ioReactor.execute(ioEventDispatch);
073: }
074:
075: public void openConnection(final InetSocketAddress address,
076: final Object attachment) {
077: this .ioReactor.connect(address, null, attachment, null);
078: }
079:
080: public void start(final NHttpClientHandler clientHandler) {
081: this .thread = new IOReactorThread(clientHandler);
082: this .thread.start();
083: }
084:
085: public IOReactorStatus getStatus() {
086: return this .ioReactor.getStatus();
087: }
088:
089: public void join(long timeout) throws InterruptedException {
090: if (this .thread != null) {
091: this .thread.join(timeout);
092: }
093: }
094:
095: public Exception getException() {
096: if (this .thread != null) {
097: return this .thread.getException();
098: } else {
099: return null;
100: }
101: }
102:
103: public void shutdown() throws IOException {
104: this .ioReactor.shutdown();
105: try {
106: join(500);
107: } catch (InterruptedException ignore) {
108: }
109: }
110:
111: private class IOReactorThread extends Thread {
112:
113: private final NHttpClientHandler clientHandler;
114:
115: private volatile Exception ex;
116:
117: public IOReactorThread(final NHttpClientHandler clientHandler) {
118: super ();
119: this .clientHandler = clientHandler;
120: }
121:
122: @Override
123: public void run() {
124: try {
125: execute(this .clientHandler);
126: } catch (IOException ex) {
127: this .ex = ex;
128: } catch (RuntimeException ex) {
129: this .ex = ex;
130: }
131: }
132:
133: public Exception getException() {
134: return this.ex;
135: }
136:
137: }
138:
139: }
|