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.XmlRootElement;
020
021import org.eclipse.jetty.server.Connector;
022import org.eclipse.jetty.server.LocalConnector;
023import org.eclipse.jetty.server.Server;
024
025import com.google.inject.AbstractModule;
026import com.google.inject.Module;
027import com.google.inject.Provider;
028
029
030/**
031 * Jetty in-JVM internal connector.
032 * Sends data directly to Jetty without any network operations.
033 * <p>
034 * Use one of {@link LocalConnector#getResponses} methods to send/receive data.
035 * {@link LocalConnector} will be available in Guice injector.
036 * </p>
037 */
038@XmlRootElement(name = "local")
039public class LocalConnectorConfig extends ConnectorConfig {
040    /*
041     * Set the maximum Idle time for a connection in ms.
042     */
043    @XmlElement
044    private int maxIdleTime = 200000;
045
046    private LocalConnector connector;
047
048    @Override
049    public Module configure() {
050        return new AbstractModule() {
051            @Override
052            protected void configure() {
053                bind(LocalConnector.class).toProvider(new Provider<LocalConnector>() {
054                    @Override
055                    public LocalConnector get() {
056                        return connector;
057                    }
058                });
059            }
060        };
061    }
062
063    @Override
064    public Connector getConnector(Server server) {
065        connector = new LocalConnector(server);
066        connector.setIdleTimeout(maxIdleTime);
067        return connector;
068    }
069}