Search in sources :

Example 81 with Container

use of org.apache.catalina.Container in project Payara by payara.

the class ReplicationStore method getSession.

public Session getSession(byte[] state, long version) throws IOException {
    Session _session = null;
    InputStream is;
    BufferedInputStream bis;
    ByteArrayInputStream bais;
    ObjectInputStream ois = null;
    Loader loader = null;
    ClassLoader classLoader = null;
    Container container = manager.getContainer();
    // MERGE chg added
    java.security.Principal pal = null;
    try {
        bais = new ByteArrayInputStream(state);
        bis = new BufferedInputStream(bais);
        if (isReplicationCompressionEnabled()) {
            is = new GZIPInputStream(bis);
        } else {
            is = bis;
        }
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.finest("loaded session from replicationstore, length = " + state.length);
        }
        loader = container.getLoader();
        if (loader != null) {
            classLoader = loader.getClassLoader();
        }
        if (classLoader != null) {
            try {
                ois = ioUtils.createObjectInputStream(is, true, classLoader, getUniqueId());
            } catch (Exception ex) {
                _logger.log(Level.WARNING, LogFacade.ERROR_CREATING_INPUT_STREAM, ex);
            }
        }
        if (ois == null) {
            ois = new ObjectInputStream(is);
        }
        try {
            _session = readSession(manager, ois);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                    bis = null;
                } catch (IOException e) {
                }
            }
        }
    } catch (ClassNotFoundException e) {
        IOException ex1 = (IOException) new IOException(_logger.getResourceBundle().getString(LogFacade.EXCEPTION_DESERIALIZING_SESSION) + e.getMessage()).initCause(e);
        _logger.log(Level.WARNING, LogFacade.EXCEPTION_DESERIALIZING_SESSION, ex1);
        throw ex1;
    } catch (IOException e) {
        if (_logger.isLoggable(Level.WARNING)) {
            _logger.log(Level.WARNING, LogFacade.EXCEPTION_GET_SESSION, e);
        }
        throw e;
    }
    String username = ((HASession) _session).getUserName();
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("ReplicationStore>>getSession: username=" + username + " principal=" + _session.getPrincipal());
    }
    if ((username != null) && (!username.equals("")) && _session.getPrincipal() == null) {
        if (_debug > 0) {
            debug("Username retrieved is " + username);
        }
        pal = ((com.sun.web.security.RealmAdapter) container.getRealm()).createFailOveredPrincipal(username);
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine("ReplicationStore>>getSession:created pal=" + pal);
        }
        if (_debug > 0) {
            debug("principal created using username  " + pal);
        }
        if (pal != null) {
            _session.setPrincipal(pal);
            if (_debug > 0) {
                debug("getSession principal=" + pal + " was added to session=" + _session);
            }
        }
    }
    // --SRI
    _session.setNew(false);
    ((HASession) _session).setVersion(version);
    ((HASession) _session).setDirty(false);
    ((HASession) _session).setPersistent(false);
    return _session;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) Loader(org.apache.catalina.Loader) LifecycleException(org.apache.catalina.LifecycleException) BackingStoreException(org.glassfish.ha.store.api.BackingStoreException) GZIPInputStream(java.util.zip.GZIPInputStream) Container(org.apache.catalina.Container) Session(org.apache.catalina.Session)

Example 82 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class TomcatWebAppBuilder method afterStart.

/**
 * {@inheritDoc}
 */
@Override
public void afterStart(final StandardContext standardContext) {
    if (isIgnored(standardContext)) {
        return;
    }
    if (shouldNotDeploy(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)

Example 83 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class OpenEJBListener method findOpenEjbWar.

private static File findOpenEjbWar(final StandardHost standardHost) {
    // look for openejb war in a Tomcat context
    for (final Container container : standardHost.findChildren()) {
        if (container instanceof StandardContext) {
            final StandardContext standardContext = (StandardContext) container;
            File contextDocBase = new File(standardContext.getDocBase());
            if (!contextDocBase.isDirectory() && standardContext.getOriginalDocBase() != null) {
                contextDocBase = new File(standardContext.getOriginalDocBase());
            }
            if (contextDocBase.isDirectory()) {
                final File openEjbWar = findOpenEjbWarInContext(contextDocBase);
                if (openEjbWar != null) {
                    return openEjbWar;
                }
            }
        }
    }
    return null;
}
Also used : Container(org.apache.catalina.Container) StandardContext(org.apache.catalina.core.StandardContext) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 84 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class OpenEJBContextConfig method cleanUpRestServlets.

private void cleanUpRestServlets() {
    final WebAppInfo webAppInfo = info.get();
    final AppInfo appInfo = info.app();
    if (webAppInfo == null || appInfo == null || "false".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
        return;
    }
    final Container[] children = context.findChildren();
    final Map<String, Container> mappedChildren = new HashMap<String, Container>();
    if (children != null) {
        // index potential rest containers by class to cleanup applications defined as servlet
        for (final Container c : children) {
            if (!(c instanceof StandardWrapper)) {
                continue;
            }
            final StandardWrapper wrapper = (StandardWrapper) c;
            final String appSpec = wrapper.getInitParameter("javax.ws.rs.Application");
            if (appSpec != null) {
                mappedChildren.put(appSpec, c);
            } else {
                final String app = wrapper.getInitParameter(Application.class.getName());
                if (app != null) {
                    mappedChildren.put(app, c);
                } else if (wrapper.getServletClass() == null) {
                    try {
                        if (Application.class.isAssignableFrom(context.getLoader().getClassLoader().loadClass(wrapper.getServletName()))) {
                            // remove directly since it is not in restApplications
                            context.removeChild(c);
                        }
                    } catch (final Exception e) {
                    // no-op
                    }
                }
            }
        }
        // cleanup
        for (final String clazz : webAppInfo.restApplications) {
            final Container child = mappedChildren.get(clazz);
            try {
                // remove only "fake" servlets to let users use their own stuff
                if (child != null) {
                    final String servletClass = StandardWrapper.class.cast(child).getServletClass();
                    if (servletClass == null || "org.apache.openejb.server.rest.OpenEJBRestServlet".equals(servletClass) || !HttpServlet.class.isAssignableFrom(info.loader().loadClass(servletClass))) {
                        context.removeChild(child);
                    }
                }
            } catch (final NoClassDefFoundError | ClassNotFoundException e) {
                context.removeChild(child);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) Container(org.apache.catalina.Container) Application(javax.ws.rs.core.Application) StandardWrapper(org.apache.catalina.core.StandardWrapper)

Example 85 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class GlobalListenerSupport method hostAdded.

/**
 * Host is added.
 *
 * @param host tomcat host.
 */
private void hostAdded(final StandardHost host) {
    addContextListener(host);
    host.addLifecycleListener(this);
    for (final Container child : host.findChildren()) {
        if (child instanceof StandardContext) {
            final StandardContext context = (StandardContext) child;
            contextAdded(context);
        }
    }
}
Also used : Container(org.apache.catalina.Container) StandardContext(org.apache.catalina.core.StandardContext)

Aggregations

Container (org.apache.catalina.Container)163 Context (org.apache.catalina.Context)28 IOException (java.io.IOException)24 Host (org.apache.catalina.Host)22 StandardContext (org.apache.catalina.core.StandardContext)21 Engine (org.apache.catalina.Engine)18 LifecycleException (org.apache.catalina.LifecycleException)17 File (java.io.File)15 ObjectName (javax.management.ObjectName)15 Wrapper (org.apache.catalina.Wrapper)13 StandardHost (org.apache.catalina.core.StandardHost)13 ArrayList (java.util.ArrayList)12 ServletException (javax.servlet.ServletException)11 MalformedURLException (java.net.MalformedURLException)10 Valve (org.apache.catalina.Valve)10 StandardWrapper (org.apache.catalina.core.StandardWrapper)10 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)10 NamingException (javax.naming.NamingException)9 Lifecycle (org.apache.catalina.Lifecycle)9 Realm (org.apache.catalina.Realm)9