Search in sources :

Example 36 with StandardContext

use of org.apache.catalina.core.StandardContext in project Payara by payara.

the class HASessionStoreValve method doPostInvoke.

/**
 * A post-request processing implementation that does the valveSave.
 * @param request
 * @param response
 */
private void doPostInvoke(Request request, Response response) throws IOException, ServletException {
    if (_logger.isLoggable(Level.FINEST)) {
        _logger.finest("IN HASessionStoreValve>>postInvoke()");
    }
    String sessionId = null;
    Session session;
    StandardContext context;
    Manager manager;
    HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
    HttpSession hsess = hreq.getSession(false);
    if (hsess != null) {
        sessionId = hsess.getId();
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.finest("IN HASessionStoreValve:postInvoke:sessionId=" + sessionId);
        }
    }
    if (sessionId != null) {
        context = (StandardContext) request.getContext();
        manager = context.getManager();
        session = manager.findSession(sessionId);
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.finest("IN HASessionStoreValve:postInvoke:session=" + session);
        }
        if (session != null) {
            WebEventPersistentManager pMgr = (WebEventPersistentManager) manager;
            pMgr.doValveSave(session);
        }
    }
    HACookieManager.reset();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) StandardContext(org.apache.catalina.core.StandardContext) HACookieManager(org.glassfish.ha.common.HACookieManager) Manager(org.apache.catalina.Manager) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session)

Example 37 with StandardContext

use of org.apache.catalina.core.StandardContext in project Payara by payara.

the class HASessionStoreValve method invoke.

/**
 * invoke call-back; nothing to do on the way in
 * @param request
 * @param response
 */
public int invoke(org.apache.catalina.Request request, org.apache.catalina.Response response) throws java.io.IOException, javax.servlet.ServletException {
    // FIXME this is for 7.0PE style valves
    // left here if the same optimization is done to the valve architecture
    String sessionId = null;
    ReplicationWebEventPersistentManager manager;
    StandardContext context;
    HttpServletRequest httpServletrequest = (HttpServletRequest) request.getRequest();
    HttpSession session = httpServletrequest.getSession(false);
    if (session != null) {
        sessionId = session.getId();
        if (sessionId != null) {
            context = (StandardContext) request.getContext();
            manager = (ReplicationWebEventPersistentManager) context.getManager();
            String oldJreplicaValue = null;
            Cookie[] cookies = httpServletrequest.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equalsIgnoreCase(Globals.JREPLICA_COOKIE_NAME)) {
                        oldJreplicaValue = cookie.getValue();
                    }
                }
                String replica = manager.getReplicaFromPredictor(sessionId, oldJreplicaValue);
                if (replica != null) {
                    Session sess = request.getSessionInternal(false);
                    if (sess != null) {
                        sess.setNote(Globals.JREPLICA_SESSION_NOTE, replica);
                    }
                }
            }
        }
    }
    return INVOKE_NEXT;
// return 0;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) HttpSession(javax.servlet.http.HttpSession) StandardContext(org.apache.catalina.core.StandardContext) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session)

Example 38 with StandardContext

use of org.apache.catalina.core.StandardContext in project Payara by payara.

the class ReplicationStore method getUniqueId.

protected long getUniqueId() {
    long uniqueId = 0L;
    Container container = this.manager.getContainer();
    if (container instanceof StandardContext) {
        StandardContext ctx = (StandardContext) container;
        uniqueId = ctx.getUniqueId();
    }
    return uniqueId;
}
Also used : Container(org.apache.catalina.Container) StandardContext(org.apache.catalina.core.StandardContext)

Example 39 with StandardContext

use of org.apache.catalina.core.StandardContext in project Payara by payara.

the class ReplicatedWebMethodSessionStrategyBuilder method initializePersistenceStrategy.

public void initializePersistenceStrategy(Context ctx, SessionManager smBean, ServerConfigLookup serverConfigLookup) {
    super.initializePersistenceStrategy(ctx, smBean, serverConfigLookup);
    // super.setPassedInPersistenceType("replicated");
    if (this.getPersistenceScope().equals("session")) {
        setupReplicationWebEventPersistentManager(SimpleMetadata.class, new FullSessionFactory(), new ReplicationStore(ioUtils), ctx, serverConfigLookup);
    } else if (this.getPersistenceScope().equals("modified-session")) {
        setupReplicationWebEventPersistentManager(SimpleMetadata.class, new ModifiedSessionFactory(), new ReplicationStore(ioUtils), ctx, serverConfigLookup);
    } else if (this.getPersistenceScope().equals("modified-attribute")) {
        setupReplicationWebEventPersistentManager(CompositeMetadata.class, new ModifiedAttributeSessionFactory(), new ReplicationAttributeStore(ioUtils), ctx, serverConfigLookup);
    } else {
        throw new IllegalArgumentException(this.getPersistenceScope());
    }
    HASessionStoreValve haValve = new HASessionStoreValve();
    StandardContext stdCtx = (StandardContext) ctx;
    stdCtx.addValve((GlassFishValve) haValve);
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) SimpleMetadata(org.glassfish.ha.store.util.SimpleMetadata)

Example 40 with StandardContext

use of org.apache.catalina.core.StandardContext 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)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)91 File (java.io.File)36 Tomcat (org.apache.catalina.startup.Tomcat)24 Context (org.apache.catalina.Context)19 Test (org.junit.Test)19 StandardHost (org.apache.catalina.core.StandardHost)16 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)12 Container (org.apache.catalina.Container)11 Host (org.apache.catalina.Host)11 JarFile (java.util.jar.JarFile)10 IOException (java.io.IOException)9 URL (java.net.URL)9 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)9 HashMap (java.util.HashMap)8 StandardRoot (org.apache.catalina.webresources.StandardRoot)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 MalformedURLException (java.net.MalformedURLException)5 ServletContext (javax.servlet.ServletContext)5