View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.localserver;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.util.List;
33  
34  import org.apache.http.impl.nio.DefaultHttpServerIODispatch;
35  import org.apache.http.impl.nio.DefaultNHttpServerConnection;
36  import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
37  import org.apache.http.impl.nio.reactor.ExceptionEvent;
38  import org.apache.http.impl.nio.reactor.IOReactorConfig;
39  import org.apache.http.nio.NHttpConnectionFactory;
40  import org.apache.http.nio.NHttpServerEventHandler;
41  import org.apache.http.nio.reactor.IOEventDispatch;
42  import org.apache.http.nio.reactor.IOReactorExceptionHandler;
43  import org.apache.http.nio.reactor.IOReactorStatus;
44  import org.apache.http.nio.reactor.ListenerEndpoint;
45  import org.apache.http.nio.reactor.ListeningIOReactor;
46  
47  public class HttpServerNio {
48  
49      private final DefaultListeningIOReactor ioReactor;
50      private final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
51  
52      private volatile IOReactorThread thread;
53      private ListenerEndpoint endpoint;
54  
55      public HttpServerNio(
56              final IOReactorConfig ioReactorConfig,
57              final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory) throws IOException {
58          super();
59          this.ioReactor = new DefaultListeningIOReactor(ioReactorConfig);
60          this.connFactory = connFactory;
61      }
62  
63      public void setExceptionHandler(final IOReactorExceptionHandler exceptionHandler) {
64          this.ioReactor.setExceptionHandler(exceptionHandler);
65      }
66  
67      private void execute(final NHttpServerEventHandler serviceHandler) throws IOException {
68          final IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(serviceHandler,
69                  this.connFactory);
70          this.ioReactor.execute(ioEventDispatch);
71      }
72  
73      public ListenerEndpoint getListenerEndpoint() {
74          return this.endpoint;
75      }
76  
77      public void setEndpoint(final ListenerEndpoint endpoint) {
78          this.endpoint = endpoint;
79      }
80  
81      public void start(final NHttpServerEventHandler serviceHandler) {
82          this.endpoint = this.ioReactor.listen(new InetSocketAddress(0));
83          this.thread = new IOReactorThread(serviceHandler);
84          this.thread.start();
85      }
86  
87      public ListeningIOReactor getIoReactor() {
88          return this.ioReactor;
89      }
90  
91      public IOReactorStatus getStatus() {
92          return this.ioReactor.getStatus();
93      }
94  
95      public List<ExceptionEvent> getAuditLog() {
96          return this.ioReactor.getAuditLog();
97      }
98  
99      public void join(final long timeout) throws InterruptedException {
100         if (this.thread != null) {
101             this.thread.join(timeout);
102         }
103     }
104 
105     public Exception getException() {
106         if (this.thread != null) {
107             return this.thread.getException();
108         } else {
109             return null;
110         }
111     }
112 
113     public void shutdown() throws IOException {
114         this.ioReactor.shutdown();
115         try {
116             join(500);
117         } catch (final InterruptedException ignore) {
118         }
119     }
120 
121     private class IOReactorThread extends Thread {
122 
123         private final NHttpServerEventHandler serviceHandler;
124 
125         private volatile Exception ex;
126 
127         public IOReactorThread(final NHttpServerEventHandler serviceHandler) {
128             super();
129             this.serviceHandler = serviceHandler;
130         }
131 
132         @Override
133         public void run() {
134             try {
135                 execute(this.serviceHandler);
136             } catch (final Exception ex) {
137                 this.ex = ex;
138             }
139         }
140 
141         public Exception getException() {
142             return this.ex;
143         }
144 
145     }
146 
147 }