001/*
002 * Copyright 2012 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.moonshine.jetty.connectors;
017
018import javax.xml.bind.annotation.XmlElement;
019import javax.xml.bind.annotation.XmlElementRef;
020import javax.xml.bind.annotation.XmlElementWrapper;
021import javax.xml.bind.annotation.XmlRootElement;
022
023import org.atteo.moonshine.webserver.WebServerAddress;
024import org.eclipse.jetty.server.AbstractNetworkConnector;
025import org.eclipse.jetty.server.ConnectionFactory;
026import org.eclipse.jetty.server.Server;
027import org.eclipse.jetty.server.ServerConnector;
028
029import com.google.inject.AbstractModule;
030import com.google.inject.Module;
031
032
033/**
034 * Jetty connector using NIO buffers and non blocking threading model.
035 * Direct NIO buffers are used and threads are only allocated to connections with requests.
036 * Synchronization is used to simulate blocking for the servlet API, and any unflushed content
037 * at the end of request handling is written asynchronously.
038 *
039 * This connector is best used when there are a many connections that have idle periods.
040 *
041 * @see ServerConnector
042 */
043@XmlRootElement(name = "serverconnector")
044public class ServerConnectorConfig extends AbstractNetworkConnectorConfig {
045    /**
046     * List of connection factories.
047     */
048    @XmlElementWrapper(name = "connections")
049    @XmlElementRef
050    private ConnectionFactoryConfig[] connections = new ConnectionFactoryConfig[] {
051        new HttpConnectionFactoryConfig()
052    };
053
054    /**
055     * Register {@link WebServerAddress} in Guice with port and hostname assigned to this connector.
056     */
057    @XmlElement
058    private boolean provideAddress = false;
059
060    @Override
061    public AbstractNetworkConnector createConnector(Server server) {
062        ConnectionFactory[] connectionFactories = new ConnectionFactory[connections.length];
063        int i = 0;
064        for (ConnectionFactoryConfig connectionConfig : connections) {
065            connectionFactories[i] = connectionConfig.getConnectionFactory();
066            i++;
067        }
068        return new ServerConnector(server, connectionFactories);
069    }
070
071    @Override
072    public Module configure() {
073        return new AbstractModule() {
074            @Override
075            protected void configure() {
076                if (provideAddress) {
077                    bind(WebServerAddress.class).toInstance(getWebServerAddress());
078                }
079            }
080        };
081    }
082
083    private WebServerAddress getWebServerAddress() {
084        return new WebServerAddress() {
085            @Override
086            public int getPort() {
087                return ServerConnectorConfig.this.getPort();
088            }
089
090            @Override
091            public String getHost() {
092                return ServerConnectorConfig.this.getHost();
093            }
094
095            @Override
096            public String getUrl() {
097                String host = ServerConnectorConfig.this.getHost();
098                if (host == null) {
099                    host = "localhost";
100                }
101                return connections[0].getProtocolString() + "://" + host + ":" + ServerConnectorConfig.this.getPort();
102            }
103        };
104    }
105}