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  package org.apache.http.impl.bootstrap;
28  
29  import java.io.IOException;
30  import java.net.ServerSocket;
31  import java.net.Socket;
32  import java.util.concurrent.ExecutorService;
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  import org.apache.http.ExceptionLogger;
36  import org.apache.http.HttpConnectionFactory;
37  import org.apache.http.HttpServerConnection;
38  import org.apache.http.config.SocketConfig;
39  import org.apache.http.protocol.HttpService;
40  
41  /**
42   * @since 4.4
43   */
44  class RequestListener implements Runnable {
45  
46      private final SocketConfig socketConfig;
47      private final ServerSocket serversocket;
48      private final HttpService httpService;
49      private final HttpConnectionFactory<? extends HttpServerConnection> connectionFactory;
50      private final ExceptionLogger exceptionLogger;
51      private final ExecutorService executorService;
52      private final AtomicBoolean terminated;
53  
54      public RequestListener(
55              final SocketConfig socketConfig,
56              final ServerSocket serversocket,
57              final HttpService httpService,
58              final HttpConnectionFactory<? extends HttpServerConnection> connectionFactory,
59              final ExceptionLogger exceptionLogger,
60              final ExecutorService executorService) {
61          this.socketConfig = socketConfig;
62          this.serversocket = serversocket;
63          this.connectionFactory = connectionFactory;
64          this.httpService = httpService;
65          this.exceptionLogger = exceptionLogger;
66          this.executorService = executorService;
67          this.terminated = new AtomicBoolean(false);
68      }
69  
70      @Override
71      public void run() {
72          try {
73              while (!isTerminated() && !Thread.interrupted()) {
74                  final Socket socket = this.serversocket.accept();
75                  socket.setSoTimeout(this.socketConfig.getSoTimeout());
76                  socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
77                  socket.setTcpNoDelay(this.socketConfig.isTcpNoDelay());
78                  if (this.socketConfig.getRcvBufSize() > 0) {
79                      socket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
80                  }
81                  if (this.socketConfig.getSndBufSize() > 0) {
82                      socket.setSendBufferSize(this.socketConfig.getSndBufSize());
83                  }
84                  if (this.socketConfig.getSoLinger() >= 0) {
85                      socket.setSoLinger(true, this.socketConfig.getSoLinger());
86                  }
87                  final HttpServerConnection conn = this.connectionFactory.createConnection(socket);
88                  final Workerstrap/Worker.html#Worker">Worker worker = new Worker(this.httpService, conn, this.exceptionLogger);
89                  this.executorService.execute(worker);
90              }
91          } catch (final Exception ex) {
92              this.exceptionLogger.log(ex);
93          }
94      }
95  
96      public boolean isTerminated() {
97          return this.terminated.get();
98      }
99  
100     public void terminate() throws IOException {
101         if (this.terminated.compareAndSet(false, true)) {
102             this.serversocket.close();
103         }
104     }
105 
106 }