Search in sources :

Example 1 with NamingContextListener

use of org.apache.catalina.core.NamingContextListener in project tomee by apache.

the class TomcatWebAppBuilder method init.

/**
     * {@inheritDoc}
     */
@Override
public void init(final StandardContext standardContext) {
    if (isIgnored(standardContext)) {
        return;
    }
    // just adding a carriage return to get logs more readable
    logger.info("------------------------- " + Contexts.getHostname(standardContext).replace("_", hosts.getDefaultHost()) + " -> " + finalName(standardContext.getPath()));
    if (FORCE_RELOADABLE) {
        final ContextInfo ctxInfo = getContextInfo(standardContext);
        if (ctxInfo == null || (ctxInfo.appInfo != null && ctxInfo.appInfo.webAppAlone)) {
            // don't do it for ears
            standardContext.setReloadable(true);
        }
    }
    if (SKIP_TLD) {
        if (standardContext.getJarScanner() != null && standardContext.getJarScanner().getJarScanFilter() != null) {
            final JarScanFilter jarScanFilter = standardContext.getJarScanner().getJarScanFilter();
            if (StandardJarScanFilter.class.isInstance(jarScanFilter)) {
                StandardJarScanFilter.class.cast(jarScanFilter).setDefaultTldScan(false);
            }
        }
    }
    final String name = standardContext.getName();
    initJ2EEInfo(standardContext);
    File warFile = Contexts.warPath(standardContext);
    if (!warFile.exists()) {
        return;
    }
    if (!warFile.isDirectory()) {
        try {
            warFile = DeploymentLoader.unpack(warFile);
        } catch (final OpenEJBException e) {
            logger.error("can't unpack '" + warFile.getAbsolutePath() + "'");
        }
    }
    standardContext.setCrossContext(SystemInstance.get().getOptions().get(OPENEJB_CROSSCONTEXT_PROPERTY, false));
    standardContext.setNamingResources(new OpenEJBNamingResource(standardContext.getNamingResources()));
    String sessionManager = SystemInstance.get().getOptions().get(OPENEJB_SESSION_MANAGER_PROPERTY + "." + name, (String) null);
    if (sessionManager == null) {
        sessionManager = SystemInstance.get().getOptions().get(OPENEJB_SESSION_MANAGER_PROPERTY, (String) null);
    }
    if (sessionManager != null) {
        if (sessionManagerClass == null) {
            try {
                // the manager should be in standardclassloader
                sessionManagerClass = ParentClassLoaderFinder.Helper.get().loadClass(sessionManager);
            } catch (final ClassNotFoundException e) {
                logger.error("can't find '" + sessionManager + "', StandardManager will be used", e);
                sessionManagerClass = StandardManager.class;
            }
        }
        try {
            final Manager mgr = (Manager) sessionManagerClass.newInstance();
            standardContext.setManager(mgr);
        } catch (final Exception e) {
            logger.error("can't instantiate '" + sessionManager + "', StandardManager will be used", e);
        }
    }
    final LifecycleListener[] listeners = standardContext.findLifecycleListeners();
    for (final LifecycleListener l : listeners) {
        if (l instanceof ContextConfig) {
            standardContext.removeLifecycleListener(l);
        }
    }
    standardContext.addLifecycleListener(new OpenEJBContextConfig(new StandardContextInfo(standardContext)));
    // force manually the namingContextListener to merge jndi in an easier way
    final NamingContextListener ncl = new NamingContextListener();
    try {
        ncl.setName((String) getNamingContextName.invoke(standardContext));
    } catch (final Exception e) {
        ncl.setName(getId(standardContext));
    }
    ncl.setExceptionOnFailedWrite(standardContext.getJndiExceptionOnFailedWrite());
    standardContext.setNamingContextListener(ncl);
    standardContext.addLifecycleListener(ncl);
    standardContext.addLifecycleListener(new TomcatJavaJndiBinder());
    // listen some events
    standardContext.addContainerListener(new TomEEContainerListener());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) StandardManager(org.apache.catalina.session.StandardManager) JarScanFilter(org.apache.tomcat.JarScanFilter) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter) OpenEJBContextConfig(org.apache.catalina.startup.OpenEJBContextConfig) LifecycleListener(org.apache.catalina.LifecycleListener) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager) StandardManager(org.apache.catalina.session.StandardManager) DeploymentExceptionManager(org.apache.openejb.assembler.classic.DeploymentExceptionManager) TransactionManager(javax.transaction.TransactionManager) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ContextConfig(org.apache.catalina.startup.ContextConfig) OpenEJBContextConfig(org.apache.catalina.startup.OpenEJBContextConfig) NamingContextListener(org.apache.catalina.core.NamingContextListener) File(java.io.File) JarFile(java.util.jar.JarFile)

Example 2 with NamingContextListener

use of org.apache.catalina.core.NamingContextListener in project tomcat by apache.

the class Tomcat method enableNaming.

/**
     * Enables JNDI naming which is disabled by default. Server must implement
     * {@link Lifecycle} in order for the {@link NamingContextListener} to be
     * used.
     *
     */
public void enableNaming() {
    // Make sure getServer() has been called as that is where naming is
    // disabled
    getServer();
    server.addLifecycleListener(new NamingContextListener());
    System.setProperty("catalina.useNaming", "true");
    String value = "org.apache.naming";
    String oldValue = System.getProperty(javax.naming.Context.URL_PKG_PREFIXES);
    if (oldValue != null) {
        if (oldValue.contains(value)) {
            value = oldValue;
        } else {
            value = value + ":" + oldValue;
        }
    }
    System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);
    value = System.getProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY);
    if (value == null) {
        System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    }
}
Also used : NamingContextListener(org.apache.catalina.core.NamingContextListener)

Example 3 with NamingContextListener

use of org.apache.catalina.core.NamingContextListener in project tomee by apache.

the class TomcatWebAppBuilder method afterStart.

/**
     * {@inheritDoc}
     */
@Override
public void afterStart(final StandardContext standardContext) {
    if (isIgnored(standardContext)) {
        return;
    }
    final Realm realm = standardContext.getRealm();
    final ClassLoader classLoader = standardContext.getLoader().getClassLoader();
    final Thread thread = Thread.currentThread();
    final ClassLoader originalLoader = thread.getContextClassLoader();
    if (realm != null && !(realm instanceof TomEERealm) && (standardContext.getParent() == null || (!realm.equals(standardContext.getParent().getRealm())))) {
        thread.setContextClassLoader(classLoader);
        try {
            standardContext.setRealm(tomeeRealm(realm));
        } finally {
            thread.setContextClassLoader(originalLoader);
        }
    }
    // if appInfo is null this is a failed deployment... just ignore
    final ContextInfo contextInfo = getContextInfo(standardContext);
    // shouldnt be there after startup (actually we shouldnt need it from info tree but our scanning does)
    contextInfo.module = null;
    if (contextInfo != null && contextInfo.appInfo == null) {
        return;
    } else if (contextInfo == null) {
        // openejb webapp loaded from the LoaderServlet
        return;
    }
    final String id = getId(standardContext);
    WebAppInfo currentWebAppInfo = null;
    for (final WebAppInfo webAppInfo : contextInfo.appInfo.webApps) {
        final String wId = getId(webAppInfo.host, webAppInfo.contextRoot, contextInfo.version);
        if (id.equals(wId)) {
            currentWebAppInfo = webAppInfo;
            break;
        }
    }
    // bind extra stuff at the java:comp level which can only be
    // bound after the context is created
    thread.setContextClassLoader(classLoader);
    final NamingContextListener ncl = standardContext.getNamingContextListener();
    final String listenerName = ncl.getName();
    ContextAccessController.setWritable(listenerName, standardContext.getNamingToken());
    try {
        final Context openejbContext = (Context) getContainerSystem().getJNDIContext().lookup("openejb");
        final Context root = (Context) ContextBindings.getClassLoader().lookup("");
        // usually fails
        final Context comp = (Context) ContextBindings.getClassLoader().lookup("comp");
        // Context root = ncl.getNamingContext();
        // Context comp = (Context) root.lookup("comp");
        safeBind(root, "openejb", openejbContext);
        // add context to WebDeploymentInfo
        if (currentWebAppInfo != null) {
            final WebContext webContext = getContainerSystem().getWebContext(currentWebAppInfo.moduleId);
            if (webContext != null) {
                webContext.setJndiEnc(root);
            }
            try {
                // Bean Validation
                standardContext.getServletContext().setAttribute("javax.faces.validator.beanValidator.ValidatorFactory", openejbContext.lookup(Assembler.VALIDATOR_FACTORY_NAMING_CONTEXT.replaceFirst("openejb", "") + currentWebAppInfo.uniqueId));
            } catch (final NamingException ne) {
                logger.warning("no validator factory found for webapp " + currentWebAppInfo.moduleId);
            }
        }
        try {
            final Class<?> orb = TomcatWebAppBuilder.class.getClassLoader().loadClass("org.omg.CORBA.ORB");
            if (SystemInstance.get().getComponent(orb) != null) {
                safeBind(comp, "ORB", new SystemComponentReference(orb));
            }
        } catch (final NoClassDefFoundError | ClassNotFoundException cnfe) {
        // no-op
        }
        if (SystemInstance.get().getComponent(HandleDelegate.class) != null) {
            safeBind(comp, "HandleDelegate", new SystemComponentReference(HandleDelegate.class));
        }
    } catch (final NamingException e) {
    // no-op
    } finally {
        // see also the start method getContainerSystem().addWebDeployment(webContext);
        for (final WebAppInfo webApp : contextInfo.appInfo.webApps) {
            SystemInstance.get().fireEvent(new AfterApplicationCreated(contextInfo.appInfo, webApp));
        }
        thread.setContextClassLoader(originalLoader);
        ContextAccessController.setReadOnly(listenerName);
    }
    thread.setContextClassLoader(classLoader);
    try {
        // owb integration filters
        final WebBeansContext webBeansContext = getWebBeansContext(contextInfo);
        if (webBeansContext != null) {
            // it is important to have a begin and a end listener
            // to be sure to create contexts before other listeners
            // and destroy contexts after other listeners
            final BeginWebBeansListener beginWebBeansListener = new BeginWebBeansListener(webBeansContext);
            final EndWebBeansListener endWebBeansListener = new EndWebBeansListener(webBeansContext);
            {
                final Object[] appEventListeners = standardContext.getApplicationEventListeners();
                final Object[] newEventListeners = new Object[appEventListeners.length + 2];
                newEventListeners[0] = beginWebBeansListener;
                System.arraycopy(appEventListeners, 0, newEventListeners, 1, appEventListeners.length);
                newEventListeners[newEventListeners.length - 1] = endWebBeansListener;
                standardContext.setApplicationEventListeners(newEventListeners);
            }
            {
                final Object[] lifecycleListeners = standardContext.getApplicationLifecycleListeners();
                final Object[] newLifecycleListeners = new Object[lifecycleListeners.length + 2];
                newLifecycleListeners[0] = beginWebBeansListener;
                System.arraycopy(lifecycleListeners, 0, newLifecycleListeners, 1, lifecycleListeners.length);
                newLifecycleListeners[newLifecycleListeners.length - 1] = endWebBeansListener;
                standardContext.setApplicationLifecycleListeners(newLifecycleListeners);
            }
            // also add the ThreadBindingListener to clean up async thread executions
            {
                WebBeansThreadBindingListener webBeansThreadBindingListener = new WebBeansThreadBindingListener(webBeansContext, standardContext.getThreadBindingListener());
                standardContext.setThreadBindingListener(webBeansThreadBindingListener);
            }
            final ContextsService contextsService = webBeansContext.getContextsService();
            if (CdiAppContextsService.class.isInstance(contextsService)) {
                // here ServletContext is usable
                CdiAppContextsService.class.cast(contextsService).applicationStarted(standardContext.getServletContext());
            }
        } else {
            // just add the end listener to be able to stack tasks to execute at the request end
            final EndWebBeansListener endWebBeansListener = new EndWebBeansListener(webBeansContext);
            {
                final Object[] appEventListeners = standardContext.getApplicationEventListeners();
                final Object[] newEventListeners = new Object[appEventListeners.length + 1];
                System.arraycopy(appEventListeners, 0, newEventListeners, 0, appEventListeners.length);
                newEventListeners[newEventListeners.length - 1] = endWebBeansListener;
                standardContext.setApplicationEventListeners(newEventListeners);
            }
            {
                final Object[] lifecycleListeners = standardContext.getApplicationLifecycleListeners();
                final Object[] newLifecycleListeners = new Object[lifecycleListeners.length + 1];
                System.arraycopy(lifecycleListeners, 0, newLifecycleListeners, 0, lifecycleListeners.length);
                newLifecycleListeners[newLifecycleListeners.length - 1] = endWebBeansListener;
                standardContext.setApplicationLifecycleListeners(newLifecycleListeners);
            }
        }
    } finally {
        thread.setContextClassLoader(originalLoader);
    }
    LinkageErrorProtection.preload(standardContext);
    final Pipeline pipeline = standardContext.getPipeline();
    pipeline.addValve(new OpenEJBValve());
    final String[] valves = SystemInstance.get().getOptions().get("tomee.valves", "").split(" *, *");
    for (final String className : valves) {
        if ("".equals(className)) {
            continue;
        }
        try {
            final Class<?> clazz = classLoader.loadClass(className);
            if (Valve.class.isAssignableFrom(clazz)) {
                final Valve valve = (Valve) clazz.newInstance();
                pipeline.addValve(valve);
            }
        } catch (final Exception e) {
            logger.error("can't add the valve " + className, e);
        }
    }
    // add servlets to webappinfo
    if (currentWebAppInfo != null) {
        for (final String mapping : standardContext.findServletMappings()) {
            final ServletInfo info = new ServletInfo();
            info.servletName = standardContext.findServletMapping(mapping);
            info.mappings.add(mapping);
            final Container container = standardContext.findChild(info.servletName);
            if (container instanceof StandardWrapper) {
                info.servletClass = ((StandardWrapper) container).getServletClass();
            } else {
                info.servletClass = mapping;
            }
            currentWebAppInfo.servlets.add(info);
        }
    }
    addConfiguredDocBases(standardContext, contextInfo);
    ensureMyFacesDontLooseFacesContext(standardContext);
}
Also used : WebContext(org.apache.openejb.core.WebContext) CdiAppContextsService(org.apache.openejb.cdi.CdiAppContextsService) ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) BeginWebBeansListener(org.apache.openejb.server.httpd.BeginWebBeansListener) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) Container(org.apache.catalina.Container) WebBeansContext(org.apache.webbeans.config.WebBeansContext) EndWebBeansListener(org.apache.openejb.server.httpd.EndWebBeansListener) NewEjbAvailableAfterApplicationCreated(org.apache.openejb.assembler.classic.event.NewEjbAvailableAfterApplicationCreated) AfterApplicationCreated(org.apache.tomee.catalina.event.AfterApplicationCreated) RouterValve(org.apache.tomee.catalina.routing.RouterValve) Valve(org.apache.catalina.Valve) NamingException(javax.naming.NamingException) NamingContextListener(org.apache.catalina.core.NamingContextListener) Realm(org.apache.catalina.Realm) WebContext(org.apache.openejb.core.WebContext) InitialContext(javax.naming.InitialContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ServletContext(javax.servlet.ServletContext) AppContext(org.apache.openejb.AppContext) StandardContext(org.apache.catalina.core.StandardContext) CdiAppContextsService(org.apache.openejb.cdi.CdiAppContextsService) ContextsService(org.apache.webbeans.spi.ContextsService) WebBeansThreadBindingListener(org.apache.tomee.catalina.cdi.WebBeansThreadBindingListener) SystemComponentReference(org.apache.openejb.core.ivm.naming.SystemComponentReference) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) Pipeline(org.apache.catalina.Pipeline) HandleDelegate(javax.ejb.spi.HandleDelegate) StandardWrapper(org.apache.catalina.core.StandardWrapper)

Aggregations

NamingContextListener (org.apache.catalina.core.NamingContextListener)3 IOException (java.io.IOException)2 NameNotFoundException (javax.naming.NameNotFoundException)2 NamingException (javax.naming.NamingException)2 LifecycleException (org.apache.catalina.LifecycleException)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 File (java.io.File)1 JarFile (java.util.jar.JarFile)1 HandleDelegate (javax.ejb.spi.HandleDelegate)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 ServletContext (javax.servlet.ServletContext)1 TransactionManager (javax.transaction.TransactionManager)1 Container (org.apache.catalina.Container)1 LifecycleListener (org.apache.catalina.LifecycleListener)1 Manager (org.apache.catalina.Manager)1 Pipeline (org.apache.catalina.Pipeline)1 Realm (org.apache.catalina.Realm)1 Valve (org.apache.catalina.Valve)1