Search in sources :

Example 16 with InstanceManager

use of org.apache.tomcat.InstanceManager in project tomcat by apache.

the class OpenWebBeansContextLifecycleListener method lifecycleEvent.

@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (event.getSource() instanceof Context) {
        Context context = (Context) event.getSource();
        if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
            if (getStartWithoutBeansXml() || context.getResources().getResource("/WEB-INF/beans.xml").exists() || context.getResources().getResource("/WEB-INF/classes/META-INF/beans.xml").exists()) {
                // Registering ELResolver with JSP container
                System.setProperty("org.apache.webbeans.application.jsp", "true");
                // Add Listeners
                String[] oldListeners = context.findApplicationListeners();
                LinkedList<String> listeners = new LinkedList<>();
                listeners.addFirst(WebBeansConfigurationListener.class.getName());
                for (String listener : oldListeners) {
                    listeners.add(listener);
                    context.removeApplicationListener(listener);
                }
                for (String listener : listeners) {
                    context.addApplicationListener(listener);
                }
                Pipeline pipeline = context.getPipeline();
                // Add to the corresponding pipeline to get a notification once configure is done
                if (pipeline instanceof Lifecycle) {
                    boolean contextLifecycleListenerFound = false;
                    for (LifecycleListener listener : ((Lifecycle) pipeline).findLifecycleListeners()) {
                        if (listener instanceof OpenWebBeansContextLifecycleListener) {
                            contextLifecycleListenerFound = true;
                        }
                    }
                    if (!contextLifecycleListenerFound) {
                        ((Lifecycle) pipeline).addLifecycleListener(this);
                    }
                }
                if (getAddSecurityValve()) {
                    // Add security valve
                    boolean securityValveFound = false;
                    for (Valve valve : pipeline.getValves()) {
                        if (valve instanceof OpenWebBeansSecurityValve) {
                            securityValveFound = true;
                        }
                    }
                    if (!securityValveFound) {
                        pipeline.addValve(new OpenWebBeansSecurityValve());
                    }
                }
            }
        }
    } else if (event.getSource() instanceof Pipeline && event.getType().equals(Lifecycle.START_EVENT)) {
        // This notification occurs once the configuration is fully done, including naming resources setup
        // Otherwise, the instance manager is not ready for creation
        Pipeline pipeline = (Pipeline) event.getSource();
        if (pipeline.getContainer() instanceof Context) {
            Context context = (Context) pipeline.getContainer();
            if (!(context.getInstanceManager() instanceof OpenWebBeansInstanceManager)) {
                InstanceManager processor = context.getInstanceManager();
                if (processor == null) {
                    processor = context.createInstanceManager();
                }
                InstanceManager custom = new OpenWebBeansInstanceManager(context.getLoader().getClassLoader(), processor);
                context.setInstanceManager(custom);
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) InstanceManager(org.apache.tomcat.InstanceManager) Lifecycle(org.apache.catalina.Lifecycle) WebBeansConfigurationListener(org.apache.webbeans.servlet.WebBeansConfigurationListener) LifecycleListener(org.apache.catalina.LifecycleListener) LinkedList(java.util.LinkedList) Pipeline(org.apache.catalina.Pipeline) Valve(org.apache.catalina.Valve)

Aggregations

InstanceManager (org.apache.tomcat.InstanceManager)16 IOException (java.io.IOException)5 NamingException (javax.naming.NamingException)5 ServletException (jakarta.servlet.ServletException)4 ServletException (javax.servlet.ServletException)3 Context (org.apache.catalina.Context)3 LifecycleException (org.apache.catalina.LifecycleException)3 Servlet (jakarta.servlet.Servlet)2 FileNotFoundException (java.io.FileNotFoundException)2 PrintStream (java.io.PrintStream)2 MalformedURLException (java.net.MalformedURLException)2 ListenerNotFoundException (javax.management.ListenerNotFoundException)2 Servlet (javax.servlet.Servlet)2 ContainerServlet (org.apache.catalina.ContainerServlet)2 JasperException (org.apache.jasper.JasperException)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)1 UnavailableException (jakarta.servlet.UnavailableException)1 MultipartConfig (jakarta.servlet.annotation.MultipartConfig)1 DeploymentException (jakarta.websocket.DeploymentException)1 Encoder (jakarta.websocket.Encoder)1