Search in sources :

Example 6 with LifeCycle

use of org.eclipse.jetty.util.component.LifeCycle in project jetty.project by eclipse.

the class ShutdownHandlerTest method testShutdownServerWithCorrectTokenAndIP.

@Test
public void testShutdownServerWithCorrectTokenAndIP() throws Exception {
    start(null);
    CountDownLatch stopLatch = new CountDownLatch(1);
    server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {

        @Override
        public void lifeCycleStopped(LifeCycle event) {
            stopLatch.countDown();
        }
    });
    HttpTester.Response response = shutdown(shutdownToken);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(stopLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(AbstractLifeCycle.STOPPED, server.getState());
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) AbstractLifeCycle(org.eclipse.jetty.util.component.AbstractLifeCycle) AbstractLifeCycle(org.eclipse.jetty.util.component.AbstractLifeCycle) CountDownLatch(java.util.concurrent.CountDownLatch) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 7 with LifeCycle

use of org.eclipse.jetty.util.component.LifeCycle in project jetty.project by eclipse.

the class ExtensionStack method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    // Wire up Extensions
    if ((extensions != null) && (extensions.size() > 0)) {
        ListIterator<Extension> exts = extensions.listIterator();
        // Connect outgoings
        while (exts.hasNext()) {
            Extension ext = exts.next();
            ext.setNextOutgoingFrames(nextOutgoing);
            nextOutgoing = ext;
            if (ext instanceof LifeCycle) {
                addBean(ext, true);
            }
        }
        // Connect incomings
        while (exts.hasPrevious()) {
            Extension ext = exts.previous();
            ext.setNextIncomingFrames(nextIncoming);
            nextIncoming = ext;
        }
    }
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension) LifeCycle(org.eclipse.jetty.util.component.LifeCycle) ContainerLifeCycle(org.eclipse.jetty.util.component.ContainerLifeCycle)

Example 8 with LifeCycle

use of org.eclipse.jetty.util.component.LifeCycle in project JMRI by JMRI.

the class WebAppManager method lifeCycleStarted.

private void lifeCycleStarted(LifeCycle lc, Profile profile) {
    // register watcher to watch web/app directories everywhere
    if (this.watcher.get(profile) != null) {
        FileUtil.findFiles("web", ".").stream().filter((file) -> (file.isDirectory())).forEachOrdered((file) -> {
            try {
                Path path = file.toPath();
                WebAppManager.this.watchPaths.put(path.register(this.watcher.get(profile), StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY), path);
            } catch (IOException ex) {
                log.error("Unable to watch {} for changes.", file);
            }
            (new Thread() {

                @Override
                public void run() {
                    while (WebAppManager.this.watcher.get(profile) != null) {
                        WatchKey key;
                        try {
                            key = WebAppManager.this.watcher.get(profile).take();
                        } catch (InterruptedException ex) {
                            return;
                        }
                        key.pollEvents().stream().filter((event) -> (event.kind() != OVERFLOW)).forEachOrdered((event) -> {
                            WebAppManager.this.savePreferences(profile);
                        });
                        if (!key.reset()) {
                            WebAppManager.this.watcher.remove(profile);
                        }
                    }
                }
            }).start();
        });
    }
}
Also used : OVERFLOW(java.nio.file.StandardWatchEventKinds.OVERFLOW) LifeCycle(org.eclipse.jetty.util.component.LifeCycle) URL(java.net.URL) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InitializationException(jmri.util.prefs.InitializationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) AngularRoute(jmri.server.web.spi.AngularRoute) WatchKey(java.nio.file.WatchKey) HashSet(java.util.HashSet) WebServer(jmri.web.server.WebServer) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) ProfileUtils(jmri.profile.ProfileUtils) Locale(java.util.Locale) Map(java.util.Map) Profile(jmri.profile.Profile) Path(java.nio.file.Path) PropertyChangeEvent(java.beans.PropertyChangeEvent) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) WebMenuItem(jmri.server.web.spi.WebMenuItem) File(java.io.File) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) WatchService(java.nio.file.WatchService) List(java.util.List) WebManifest(jmri.server.web.spi.WebManifest) WebServerPreferences(jmri.web.server.WebServerPreferences) FileUtil(jmri.util.FileUtil) StringJoiner(java.util.StringJoiner) FileSystems(java.nio.file.FileSystems) Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

Example 9 with LifeCycle

use of org.eclipse.jetty.util.component.LifeCycle in project lucene-solr by apache.

the class JettySolrRunner method init.

private void init(int port) {
    QueuedThreadPool qtp = new QueuedThreadPool();
    qtp.setMaxThreads(THREAD_POOL_MAX_THREADS);
    qtp.setIdleTimeout(THREAD_POOL_MAX_IDLE_TIME_MS);
    qtp.setStopTimeout((int) TimeUnit.MINUTES.toMillis(1));
    server = new Server(qtp);
    server.manage(qtp);
    server.setStopAtShutdown(config.stopAtShutdown);
    if (System.getProperty("jetty.testMode") != null) {
        // if this property is true, then jetty will be configured to use SSL
        // leveraging the same system properties as java to specify
        // the keystore/truststore if they are set unless specific config
        // is passed via the constructor.
        //
        // This means we will use the same truststore, keystore (and keys) for
        // the server as well as any client actions taken by this JVM in
        // talking to that server, but for the purposes of testing that should 
        // be good enough
        final SslContextFactory sslcontext = SSLConfig.createContextFactory(config.sslConfig);
        ServerConnector connector;
        if (sslcontext != null) {
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.setSecureScheme("https");
            configuration.addCustomizer(new SecureRequestCustomizer());
            connector = new ServerConnector(server, new SslConnectionFactory(sslcontext, "http/1.1"), new HttpConnectionFactory(configuration));
        } else {
            connector = new ServerConnector(server, new HttpConnectionFactory());
        }
        connector.setReuseAddress(true);
        connector.setSoLingerTime(-1);
        connector.setPort(port);
        connector.setHost("127.0.0.1");
        connector.setIdleTimeout(THREAD_POOL_MAX_IDLE_TIME_MS);
        server.setConnectors(new Connector[] { connector });
        server.setSessionIdManager(new HashSessionIdManager(new Random()));
    } else {
        ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory());
        connector.setPort(port);
        connector.setSoLingerTime(-1);
        connector.setIdleTimeout(THREAD_POOL_MAX_IDLE_TIME_MS);
        server.setConnectors(new Connector[] { connector });
    }
    // Initialize the servlets
    final ServletContextHandler root = new ServletContextHandler(server, config.context, ServletContextHandler.SESSIONS);
    server.addLifeCycleListener(new LifeCycle.Listener() {

        @Override
        public void lifeCycleStopping(LifeCycle arg0) {
        }

        @Override
        public void lifeCycleStopped(LifeCycle arg0) {
        }

        @Override
        public void lifeCycleStarting(LifeCycle arg0) {
            synchronized (JettySolrRunner.this) {
                waitOnSolr = true;
                JettySolrRunner.this.notify();
            }
        }

        @Override
        public void lifeCycleStarted(LifeCycle arg0) {
            lastPort = getFirstConnectorPort();
            nodeProperties.setProperty("hostPort", Integer.toString(lastPort));
            nodeProperties.setProperty("hostContext", config.context);
            root.getServletContext().setAttribute(SolrDispatchFilter.PROPERTIES_ATTRIBUTE, nodeProperties);
            root.getServletContext().setAttribute(SolrDispatchFilter.SOLRHOME_ATTRIBUTE, solrHome);
            logger.info("Jetty properties: {}", nodeProperties);
            debugFilter = root.addFilter(DebugFilter.class, "*", EnumSet.of(DispatcherType.REQUEST));
            extraFilters = new LinkedList<>();
            for (Class<? extends Filter> filterClass : config.extraFilters.keySet()) {
                extraFilters.add(root.addFilter(filterClass, config.extraFilters.get(filterClass), EnumSet.of(DispatcherType.REQUEST)));
            }
            for (ServletHolder servletHolder : config.extraServlets.keySet()) {
                String pathSpec = config.extraServlets.get(servletHolder);
                root.addServlet(servletHolder, pathSpec);
            }
            dispatchFilter = root.getServletHandler().newFilterHolder(BaseHolder.Source.EMBEDDED);
            dispatchFilter.setHeldClass(SolrDispatchFilter.class);
            dispatchFilter.setInitParameter("excludePatterns", excludePatterns);
            root.addFilter(dispatchFilter, "*", EnumSet.of(DispatcherType.REQUEST));
        }

        @Override
        public void lifeCycleFailure(LifeCycle arg0, Throwable arg1) {
            System.clearProperty("hostPort");
        }
    });
    // for some reason, there must be a servlet for this to get applied
    root.addServlet(Servlet404.class, "/*");
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setHandler(root);
    gzipHandler.setMinGzipSize(0);
    gzipHandler.setCheckGzExists(false);
    gzipHandler.setCompressionLevel(-1);
    gzipHandler.setExcludedAgentPatterns(".*MSIE.6\\.0.*");
    gzipHandler.setIncludedMethods("GET");
    server.setHandler(gzipHandler);
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) HashSessionIdManager(org.eclipse.jetty.server.session.HashSessionIdManager) LinkedList(java.util.LinkedList) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Random(java.util.Random) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) Filter(javax.servlet.Filter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

LifeCycle (org.eclipse.jetty.util.component.LifeCycle)9 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Environment (io.dropwizard.setup.Environment)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DataSourceFactory (io.dropwizard.db.DataSourceFactory)1 DBIFactory (io.dropwizard.jdbi.DBIFactory)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 FileSystems (java.nio.file.FileSystems)1 Path (java.nio.file.Path)1 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)1 OVERFLOW (java.nio.file.StandardWatchEventKinds.OVERFLOW)1 WatchKey (java.nio.file.WatchKey)1 WatchService (java.nio.file.WatchService)1