org.apache.catalina.Lifecycle Java Examples

The following examples show how to use org.apache.catalina.Lifecycle. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JniLifecycleListener.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {

    if (Lifecycle.BEFORE_START_EVENT.equals(event.getType())) {

        if (!libraryName.isEmpty()) {
            System.loadLibrary(libraryName);
            log.info("Loaded native library " + libraryName);
        } else if (!libraryPath.isEmpty()) {
            System.load(libraryPath);
            log.info("Loaded native library from " + libraryPath);
        } else {
            throw new IllegalArgumentException("Either libraryName or libraryPath must be set");
        }
    }
}
 
Example #2
Source File: EngineConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Process the START event for an associated Engine.
 *
 * @param event The lifecycle event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    // Identify the engine we are associated with
    try {
        engine = (Engine) event.getLifecycle();
    } catch (ClassCastException e) {
        log.error(sm.getString("engineConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    if (event.getType().equals(Lifecycle.START_EVENT))
        start();
    else if (event.getType().equals(Lifecycle.STOP_EVENT))
        stop();

}
 
Example #3
Source File: UserConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Process the START event for an associated Host.
 *
 * @param event The lifecycle event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    // Identify the host we are associated with
    try {
        host = (Host) event.getLifecycle();
    } catch (ClassCastException e) {
        log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    if (event.getType().equals(Lifecycle.START_EVENT))
        start();
    else if (event.getType().equals(Lifecycle.STOP_EVENT))
        stop();

}
 
Example #4
Source File: StoreConfigLifecycleListener.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Register StoreRegistry after Start the complete Server.
 *
 * @see org.apache.catalina.LifecycleListener#lifecycleEvent(org.apache.catalina.LifecycleEvent)
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (Lifecycle.AFTER_START_EVENT.equals(event.getType())) {
        if (event.getSource() instanceof Server) {
            createMBean((Server) event.getSource());
        } else {
            log.warn(sm.getString("storeConfigListener.notServer"));
        }
    } else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
        if (oname != null) {
            registry.unregisterComponent(oname);
            oname = null;
        }
    }
 }
 
Example #5
Source File: Tomcat.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {
    try {
        Context context = (Context) event.getLifecycle();
        if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
            context.setConfigured(true);
        }
        // LoginConfig is required to process @ServletSecurity
        // annotations
        if (context.getLoginConfig() == null) {
            context.setLoginConfig(
                    new LoginConfig("NONE", null, null, null));
            context.getPipeline().addValve(new NonLoginAuthenticator());
        }
    } catch (ClassCastException e) {
        return;
    }
}
 
Example #6
Source File: JMXServerListener.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void lifecycleEvent(final LifecycleEvent event) {
    try {
        if (server == null && Lifecycle.START_EVENT.equals(event.getType())) {
            serviceURL = new JMXServiceURL(protocol, host, port, urlPath);
            server = JMXConnectorServerFactory.newJMXConnectorServer(serviceURL, null,
                                                ManagementFactory.getPlatformMBeanServer());
            server.start();
            LOGGER.info("Started JMX server: " + serviceURL.toString());
        } else if (server != null && Lifecycle.STOP_EVENT.equals(event.getType())) {
            server.stop();
            server = null;
            LOGGER.info("Stopped JMX server: " + serviceURL.toString());
        }
    } catch (final Exception e) {
        throw new JMXException(e);
    }
}
 
Example #7
Source File: TomEEUndeployTest.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Test
public void justAContextStop() throws Exception {
    container.start();
    assertEquals(0, webapps().length);
    final StandardHost standardHost = StandardHost.class.cast(TomcatHelper.getServer().findService("Tomcat").getContainer().findChild("localhost"));
    final HostConfig listener = new HostConfig(); // not done in embedded but that's the way autodeploy works in normal tomcat
    standardHost.addLifecycleListener(listener);
    createWebapp(new File(WORK_DIR, "tomee/webapps/my-webapp"));
    listener.lifecycleEvent(new LifecycleEvent(standardHost, Lifecycle.START_EVENT, standardHost));
    assertEquals(1, webapps().length);
    webapps()[0].stop();
    assertEquals(1, webapps().length);
    webapps()[0].start();
    assertEquals(1, webapps().length);
    assertEquals("test", IO.slurp(new URL("http://localhost:" + http + "/my-webapp/")));
}
 
Example #8
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Start {@link Valve}s) in this pipeline and implement the requirements
 * of {@link LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {

    // Start the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).start();
        current = current.getNext();
    }

    setState(LifecycleState.STARTING);
}
 
Example #9
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Stop {@link Valve}s) in this pipeline and implement the requirements
 * of {@link LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void stopInternal() throws LifecycleException {

    setState(LifecycleState.STOPPING);

    // Stop the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).stop();
        current = current.getNext();
    }
}
 
Example #10
Source File: UserConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Process the START event for an associated Host.
 *
 * @param event The lifecycle event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    // Identify the host we are associated with
    try {
        host = (Host) event.getLifecycle();
    } catch (ClassCastException e) {
        log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    if (event.getType().equals(Lifecycle.START_EVENT))
        start();
    else if (event.getType().equals(Lifecycle.STOP_EVENT))
        stop();

}
 
Example #11
Source File: Embedded.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Stop nested components ({@link Connector}s and {@link Engine}s) and
 * implement the requirements of
 * {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that needs to be reported
 */
@Override
protected void stopInternal() throws LifecycleException {

    if( log.isDebugEnabled() )
        log.debug("Stopping embedded server");

    setState(LifecycleState.STOPPING);

    // Stop our defined Connectors first
    for (int i = 0; i < connectors.length; i++) {
        ((Lifecycle) connectors[i]).stop();
    }

    // Stop our defined Engines second
    for (int i = 0; i < engines.length; i++) {
        engines[i].stop();
    }

}
 
Example #12
Source File: JasperListener.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Primary entry point for startup and shutdown events.
 *
 * @param event The event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    if (Lifecycle.BEFORE_INIT_EVENT.equals(event.getType())) {
        try {
            // Set JSP factory
            Class.forName("org.apache.jasper.compiler.JspRuntimeContext",
                          true,
                          this.getClass().getClassLoader());
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            // Should not occur, obviously
            log.warn("Couldn't initialize Jasper", t);
        }
        // Another possibility is to do directly:
        // JspFactory.setDefaultFactory(new JspFactoryImpl());
    }

}
 
Example #13
Source File: LifecycleBase.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public final synchronized void init() throws LifecycleException {

    //当前状态与要处理的方法是否匹配
    if (!state.equals(LifecycleState.NEW)) {
        // 抛异常处理
        invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
    }

    try {
        setStateInternal(LifecycleState.INITIALIZING, null, false);
        //  模板方法
        initInternal();

        //初始化结束设置 state
        setStateInternal(LifecycleState.INITIALIZED, null, false);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        setStateInternal(LifecycleState.FAILED, null, false);
        throw new LifecycleException(
                sm.getString("lifecycleBase.initFail",toString()), t);
    }
}
 
Example #14
Source File: EngineConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Process the START event for an associated Engine.
 *
 * @param event The lifecycle event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    // Identify the engine we are associated with
    try {
        engine = (Engine) event.getLifecycle();
    } catch (ClassCastException e) {
        log.error(sm.getString("engineConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    if (event.getType().equals(Lifecycle.START_EVENT))
        start();
    else if (event.getType().equals(Lifecycle.STOP_EVENT))
        stop();

}
 
Example #15
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {
    try {
        Context context = (Context) event.getLifecycle();
        if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
            context.setConfigured(true);

            // Process annotations
            WebAnnotationSet.loadApplicationAnnotations(context);

            // LoginConfig is required to process @ServletSecurity
            // annotations
            if (context.getLoginConfig() == null) {
                context.setLoginConfig(new LoginConfig("NONE", null, null, null));
                context.getPipeline().addValve(new NonLoginAuthenticator());
            }
        }
    } catch (ClassCastException e) {
    }
}
 
Example #16
Source File: StandardEngine.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (disabled) return;

    String type = event.getType();
    if (Lifecycle.AFTER_START_EVENT.equals(type) ||
            Lifecycle.BEFORE_STOP_EVENT.equals(type) ||
            Lifecycle.BEFORE_DESTROY_EVENT.equals(type)) {
        // Container is being started/stopped/removed
        // Force re-calculation and disable listener since it won't
        // be re-used
        engine.defaultAccessLog.set(null);
        uninstall();
    }
}
 
Example #17
Source File: TomcatJavaJndiBinder.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void lifecycleEvent(final LifecycleEvent event) {
    final Object source = event.getSource();
    if (source instanceof StandardContext) {
        final StandardContext context = (StandardContext) source;
        if (Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {
            TomcatJndiBuilder.mergeJava(context);
        }
    }
}
 
Example #18
Source File: WebappLoader.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Stop associated {@link ClassLoader} and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void stopInternal() throws LifecycleException {

    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.stopping"));

    setState(LifecycleState.STOPPING);

    // Remove context attributes as appropriate
    if (container instanceof Context) {
        ServletContext servletContext =
            ((Context) container).getServletContext();
        servletContext.removeAttribute(Globals.CLASS_PATH_ATTR);
    }

    // Throw away our current class loader
    if (classLoader != null) {
        ((Lifecycle) classLoader).stop();
        DirContextURLStreamHandler.unbind(classLoader);
    }

    try {
        StandardContext ctx=(StandardContext)container;
        String contextName = ctx.getName();
        if (!contextName.startsWith("/")) {
            contextName = "/" + contextName;
        }
        ObjectName cloname = new ObjectName
            (MBeanUtils.getDomain(ctx) + ":type=WebappClassLoader,context="
             + contextName + ",host=" + ctx.getParent().getName());
        Registry.getRegistry(null, null).unregisterComponent(cloname);
    } catch (Exception e) {
        log.error("LifecycleException ", e);
    }

    classLoader = null;
}
 
Example #19
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Add a new Valve to the end of the pipeline associated with this
 * Container.  Prior to adding the Valve, the Valve's
 * <code>setContainer()</code> method will be called, if it implements
 * <code>Contained</code>, with the owning Container as an argument.
 * The method may throw an
 * <code>IllegalArgumentException</code> if this Valve chooses not to
 * be associated with this Container, or <code>IllegalStateException</code>
 * if it is already associated with a different Container.</p>
 *
 * @param valve Valve to be added
 *
 * @exception IllegalArgumentException if this Container refused to
 *  accept the specified Valve
 * @exception IllegalArgumentException if the specified Valve refuses to be
 *  associated with this Container
 * @exception IllegalStateException if the specified Valve is already
 *  associated with a different Container
 */
@Override
public void addValve(Valve valve) {

    // Validate that we can add this Valve
    if (valve instanceof Contained)
        ((Contained) valve).setContainer(this.container);

    // Start the new component if necessary
    if (getState().isAvailable()) {
        if (valve instanceof Lifecycle) {
            try {
                ((Lifecycle) valve).start();
            } catch (LifecycleException e) {
                log.error("StandardPipeline.addValve: start: ", e);
            }
        }
    }

    // Add this Valve to the set associated with this Pipeline
    if (first == null) {
        first = valve;
        valve.setNext(basic);
    } else {
        Valve current = first;
        while (current != null) {
            if (current.getNext() == basic) {
                current.setNext(valve);
                valve.setNext(basic);
                break;
            }
            current = current.getNext();
        }
    }
    
    container.fireContainerEvent(Container.ADD_VALVE_EVENT, valve);
}
 
Example #20
Source File: ContainerBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void destroyInternal() throws LifecycleException {

    if ((manager != null) && (manager instanceof Lifecycle)) {
        ((Lifecycle) manager).destroy();
    }
    Realm realm = getRealmInternal();
    if ((realm != null) && (realm instanceof Lifecycle)) {
        ((Lifecycle) realm).destroy();
    }
    if ((cluster != null) && (cluster instanceof Lifecycle)) {
        ((Lifecycle) cluster).destroy();
    }
    if ((loader != null) && (loader instanceof Lifecycle)) {
        ((Lifecycle) loader).destroy();
    }

    // Stop the Valves in our pipeline (including the basic), if any
    if (pipeline instanceof Lifecycle) {
        ((Lifecycle) pipeline).destroy();
    }

    // Remove children now this container is being destroyed
    for (Container child : findChildren()) {
        removeChild(child);
    }

    // Required if the child is destroyed directly.
    if (parent != null) {
        parent.removeChild(this);
    }

    // If init fails, this may be null
    if (startStopExecutor != null) {
        startStopExecutor.shutdownNow();
    }

    super.destroyInternal();
}
 
Example #21
Source File: GlobalResourcesLifecycleListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Primary entry point for startup and shutdown events.
 *
 * @param event The event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    if (Lifecycle.START_EVENT.equals(event.getType())) {
        component = event.getLifecycle();
        createMBeans();
    } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
        destroyMBeans();
        component = null;
    }

}
 
Example #22
Source File: SecurityListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {
    // This is the earliest event in Lifecycle
    if (event.getType().equals(Lifecycle.BEFORE_INIT_EVENT)) {
        doChecks();
    }
}
 
Example #23
Source File: ContextConfig.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Process events for an associated Context.
 *
 * @param event The lifecycle event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    // Identify the context we are associated with
    try {
        context = (Context) event.getLifecycle();
    } catch (ClassCastException e) {
        log.error(sm.getString("contextConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
        configureStart();
    } else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
        beforeStart();
    } else if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
        // Restore docBase for management tools
        if (originalDocBase != null) {
            context.setDocBase(originalDocBase);
        }
    } else if (event.getType().equals(Lifecycle.CONFIGURE_STOP_EVENT)) {
        configureStop();
    } else if (event.getType().equals(Lifecycle.AFTER_INIT_EVENT)) {
        init();
    } else if (event.getType().equals(Lifecycle.AFTER_DESTROY_EVENT)) {
        destroy();
    }

}
 
Example #24
Source File: LazyRealm.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
protected void initInternal() throws LifecycleException {
    final Class<?> r = loadClass();
    if (r != null && Lifecycle.class.isAssignableFrom(r) && instance() != null) {
        Lifecycle.class.cast(delegate).init();
    } else {
        init = true;
    }
}
 
Example #25
Source File: GlobalResourcesLifecycleListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Primary entry point for startup and shutdown events.
 *
 * @param event The event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    if (Lifecycle.START_EVENT.equals(event.getType())) {
        component = event.getLifecycle();
        createMBeans();
    } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
        destroyMBeans();
        component = null;
    }

}
 
Example #26
Source File: HostConfig.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Process the START event for an associated Host.
 *
 * @param event The lifecycle event that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {

    // Identify the host we are associated with
    try {
        host = (Host) event.getLifecycle();
        if (host instanceof StandardHost) {
            setCopyXML(((StandardHost) host).isCopyXML());
            setDeployXML(((StandardHost) host).isDeployXML());
            setUnpackWARs(((StandardHost) host).isUnpackWARs());
            setContextClass(((StandardHost) host).getContextClass());
        }
    } catch (ClassCastException e) {
        log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    if (event.getType().equals(Lifecycle.PERIODIC_EVENT)) {
        check();
    } else if (event.getType().equals(Lifecycle.START_EVENT)) {
        start();
    } else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
        stop();
    }
}
 
Example #27
Source File: SecurityListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {
    // This is the earliest event in Lifecycle
    if (event.getType().equals(Lifecycle.BEFORE_INIT_EVENT)) {
        doChecks();
    }
}
 
Example #28
Source File: AbstractSamlAuthenticatorValve.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (Lifecycle.START_EVENT.equals(event.getType())) {
        cache = false;
    } else if (Lifecycle.AFTER_START_EVENT.equals(event.getType())) {
    	keycloakInit();
    } else if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType())) {
        beforeStop();
    }
}
 
Example #29
Source File: LazyStopLoader.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public LifecycleListener[] findLifecycleListeners() {
    if (delegate instanceof Lifecycle) {
        return ((Lifecycle) delegate).findLifecycleListeners();
    }
    return new LifecycleListener[0];
}
 
Example #30
Source File: LazyRealm.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
protected void startInternal() throws LifecycleException {
    final Class<?> r = loadClass();
    if (r != null && Lifecycle.class.isAssignableFrom(r) && instance() != null) {
        Lifecycle.class.cast(instance()).start();
    } else {
        start = true;
    }
    setState(LifecycleState.STARTING);
}