Search in sources :

Example 1 with Connector

use of org.mortbay.jetty.Connector in project sonatype-aether by sonatype.

the class HttpServer method waitForConnectors.

protected void waitForConnectors() throws Exception {
    // for unknown reasons, the connectors occasionally don't start properly, this tries hard to ensure they are up
    List<Connector> badConnectors = new ArrayList<Connector>(2);
    for (int r = 10; r > 0; r--) {
        // wait some seconds for the connectors to come up
        for (int i = 200; i > 0; i--) {
            badConnectors.clear();
            for (Connector connector : server.getConnectors()) {
                if (connector.getLocalPort() < 0) {
                    badConnectors.add(connector);
                }
            }
            if (badConnectors.isEmpty()) {
                return;
            }
            try {
                Thread.sleep(15);
            } catch (InterruptedException e) {
                return;
            }
        }
        // restart the broken connectors and hope they make it this time
        System.err.println("WARNING: " + badConnectors + " did not start properly, restarting");
        for (Connector connector : badConnectors) {
            connector.stop();
            connector.start();
        }
    }
}
Also used : Connector(org.mortbay.jetty.Connector) SslSocketConnector(org.mortbay.jetty.security.SslSocketConnector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) ArrayList(java.util.ArrayList) Constraint(org.mortbay.jetty.security.Constraint)

Example 2 with Connector

use of org.mortbay.jetty.Connector in project sonatype-aether by sonatype.

the class HttpServer method start.

/**
     * Starts the server. Trying to start an already running server has no effect.
     *
     * @return This server, never {@code null}.
     * @throws Exception If the server could not be started.
     */
public HttpServer start() throws Exception {
    if (server != null) {
        return this;
    }
    recordedRequests.clear();
    List<Connector> connectors = new ArrayList<Connector>();
    if (httpPort >= 0) {
        connectors.add(newHttpConnector());
    }
    if (httpsPort >= 0 && keyStoreLocation != null) {
        connectors.add(newHttpsConnector());
    }
    HandlerList handlerList = new HandlerList();
    if (!recordedPatterns.isEmpty()) {
        handlerList.addHandler(new RecordingHandler());
    }
    if (latency != 0) {
        handlerList.addHandler(newSleepHandler(latency));
    }
    if (redirectToHttps) {
        handlerList.addHandler(newSslRedirectHandler());
    }
    if (proxyUsername != null && proxyPassword != null) {
        handlerList.addHandler(newProxyHandler());
    }
    if (!securedRealms.isEmpty()) {
        handlerList.addHandler(newSecurityHandler());
    }
    if (!resourceDirs.isEmpty()) {
        handlerList.addHandler(newResourceHandler());
    }
    handlerList.addHandler(new DefaultHandler());
    server = new Server(0);
    server.setHandler(handlerList);
    server.setConnectors(connectors.toArray(new Connector[connectors.size()]));
    server.start();
    waitForConnectors();
    addDefaultFilterTokens();
    return this;
}
Also used : HandlerList(org.mortbay.jetty.handler.HandlerList) Connector(org.mortbay.jetty.Connector) SslSocketConnector(org.mortbay.jetty.security.SslSocketConnector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) Server(org.mortbay.jetty.Server) ArrayList(java.util.ArrayList) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler)

Example 3 with Connector

use of org.mortbay.jetty.Connector in project commons by twitter.

the class JettyHttpServerDispatch method listen.

@Override
public synchronized boolean listen(int minPort, int maxPort) {
    boolean state = !isStarted();
    Preconditions.checkState(state, "HttpServerDispatch has already been started on port: %d", port);
    Connector connector = openConnector(minPort, maxPort);
    // Couldn't open a server port.
    if (connector == null)
        return false;
    port = connector.getLocalPort();
    server = new Server();
    server.addConnector(connector);
    context = new Context(server, "/", Context.NO_SESSIONS);
    if (requestLog.isPresent()) {
        RequestLogHandler logHandler = new RequestLogHandler();
        logHandler.setRequestLog(requestLog.get());
        context.addHandler(logHandler);
    }
    context.addServlet(new ServletHolder(new RootHandler()), "/");
    try {
        server.start();
        LOG.info("HTTP server is listening on port " + port);
        return true;
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "HTTP server failed to start on port " + connector.getLocalPort(), e);
        return false;
    }
}
Also used : Context(org.mortbay.jetty.servlet.Context) Connector(org.mortbay.jetty.Connector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) AbstractConnector(org.mortbay.jetty.AbstractConnector) Server(org.mortbay.jetty.Server) RequestLogHandler(org.mortbay.jetty.handler.RequestLogHandler) ServletHolder(org.mortbay.jetty.servlet.ServletHolder) IOException(java.io.IOException)

Example 4 with Connector

use of org.mortbay.jetty.Connector in project guice by google.

the class Main method main.

public static void main(String[] args) throws Exception {
    Server server = new Server();
    Connector connector = new SelectChannelConnector();
    connector.setPort(8080);
    server.setConnectors(new Connector[] { connector });
    WebAppContext webapp = new WebAppContext("./root", "/example");
    server.addHandler(webapp);
    server.start();
    server.join();
}
Also used : Connector(org.mortbay.jetty.Connector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) Server(org.mortbay.jetty.Server)

Example 5 with Connector

use of org.mortbay.jetty.Connector in project OpenRefine by OpenRefine.

the class ShutdownSignalHandler method init.

public void init(String host, int port) throws Exception {
    logger.info("Starting Server bound to '" + host + ":" + port + "'");
    String memory = Configurations.get("refine.memory");
    if (memory != null) {
        logger.info("refine.memory size: " + memory + " JVM Max heap: " + Runtime.getRuntime().maxMemory());
    }
    int maxThreads = Configurations.getInteger("refine.queue.size", 30);
    int maxQueue = Configurations.getInteger("refine.queue.max_size", 300);
    long keepAliveTime = Configurations.getInteger("refine.queue.idle_time", 60);
    LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(maxQueue);
    threadPool = new ThreadPoolExecutor(maxThreads, maxQueue, keepAliveTime, TimeUnit.SECONDS, queue);
    this.setThreadPool(new ThreadPoolExecutorAdapter(threadPool));
    Connector connector = new SocketConnector();
    connector.setPort(port);
    connector.setHost(host);
    connector.setMaxIdleTime(Configurations.getInteger("refine.connection.max_idle_time", 60000));
    connector.setStatsOn(false);
    this.addConnector(connector);
    File webapp = new File(Configurations.get("refine.webapp", "main/webapp"));
    if (!isWebapp(webapp)) {
        webapp = new File("main/webapp");
        if (!isWebapp(webapp)) {
            webapp = new File("webapp");
            if (!isWebapp(webapp)) {
                logger.warn("Warning: Failed to find web application at '" + webapp.getAbsolutePath() + "'");
                System.exit(-1);
            }
        }
    }
    final String contextPath = Configurations.get("refine.context_path", "/");
    final int maxFormContentSize = Configurations.getInteger("refine.max_form_content_size", 1048576);
    logger.info("Initializing context: '" + contextPath + "' from '" + webapp.getAbsolutePath() + "'");
    WebAppContext context = new WebAppContext(webapp.getAbsolutePath(), contextPath);
    context.setMaxFormContentSize(maxFormContentSize);
    this.setHandler(context);
    this.setStopAtShutdown(true);
    this.setSendServerVersion(true);
    // Enable context autoreloading
    if (Configurations.getBoolean("refine.autoreload", false)) {
        scanForUpdates(webapp, context);
    }
    // start the server
    try {
        this.start();
    } catch (BindException e) {
        logger.error("Failed to start server - is there another copy running already on this port/address?");
        throw e;
    }
    configure(context);
}
Also used : Connector(org.mortbay.jetty.Connector) SocketConnector(org.mortbay.jetty.bio.SocketConnector) ThreadPoolExecutorAdapter(com.google.util.threads.ThreadPoolExecutorAdapter) BindException(java.net.BindException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SocketConnector(org.mortbay.jetty.bio.SocketConnector) File(java.io.File)

Aggregations

Connector (org.mortbay.jetty.Connector)14 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)12 Server (org.mortbay.jetty.Server)8 WebAppContext (org.mortbay.jetty.webapp.WebAppContext)5 IOException (java.io.IOException)4 DefaultHandler (org.mortbay.jetty.handler.DefaultHandler)4 SslSocketConnector (org.mortbay.jetty.security.SslSocketConnector)4 Handler (org.mortbay.jetty.Handler)3 ArrayList (java.util.ArrayList)2 SocketConnector (org.mortbay.jetty.bio.SocketConnector)2 HandlerList (org.mortbay.jetty.handler.HandlerList)2 Constraint (org.mortbay.jetty.security.Constraint)2 ThreadPoolExecutorAdapter (com.google.util.threads.ThreadPoolExecutorAdapter)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 BindException (java.net.BindException)1 ServerSocket (java.net.ServerSocket)1 URL (java.net.URL)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1