2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.engine.http.connector;
36
import java.io.IOException;
37
import java.net.Socket;
38
import java.net.SocketException;
39
import java.nio.channels.AsynchronousCloseException;
40
import java.nio.channels.ClosedByInterruptException;
41
import java.util.concurrent.CountDownLatch;
42
import java.util.logging.Level;
44
import org.restlet.Server;
47
* Listens on the given socket channel for incoming connections and dispatches
48
* them to the given handler pool
50
* @author Jerome Louvel
52
public class Acceptor extends BaseTask {
54
/** The parent server helper. */
55
private final BaseServerHelper helper;
58
* The latch to countdown when the socket is ready to accept connections.
60
private final CountDownLatch latch;
66
* The target server helper.
68
* The latch to countdown when the socket is ready to accept
71
public Acceptor(BaseServerHelper helper, CountDownLatch latch) {
77
* Returns the parent server helper.
79
* @return The parent server helper.
81
protected BaseServerHelper getHelper() {
86
* Listens on the given server socket for incoming connections.
89
this.latch.countDown();
94
Socket socket = getHelper().getServerSocket().accept();
95
int connectionsCount = getHelper().getConnections().size();
97
if ((getHelper().getMaxTotalConnections() == -1)
98
|| (connectionsCount <= getHelper()
99
.getMaxTotalConnections())) {
100
Connection<Server> connection = getHelper()
101
.createConnection(getHelper(), socket, null);
103
getHelper().getConnections().add(connection);
105
// Rejection connection
110
"Maximum number of concurrent connections reached. New connection rejected.");
112
} catch (ClosedByInterruptException ex) {
113
this.helper.getLogger().log(Level.FINE,
114
"ServerSocket channel was closed by interrupt", ex);
116
} catch (AsynchronousCloseException ace) {
117
this.helper.getLogger().log(Level.FINE,
118
"The server socket was closed", ace);
119
} catch (SocketException se) {
120
this.helper.getLogger().log(Level.FINE,
121
"The server socket was closed", se);
122
} catch (IOException ex) {
123
this.helper.getLogger().log(Level.WARNING,
124
"Unexpected error while accepting new connection", ex);