Search in sources :

Example 1 with HttpSessionListener

use of jakarta.servlet.http.HttpSessionListener in project tomcat by apache.

the class StandardContext method listenerStart.

/**
 * Configure the set of instantiated application event listeners
 * for this Context.
 * @return <code>true</code> if all listeners wre
 * initialized successfully, or <code>false</code> otherwise.
 */
public boolean listenerStart() {
    if (log.isDebugEnabled()) {
        log.debug("Configuring application event listeners");
    }
    // Instantiate the required listeners
    String[] listeners = findApplicationListeners();
    Object[] results = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(" Configuring event listener class '" + listeners[i] + "'");
        }
        try {
            String listener = listeners[i];
            results[i] = getInstanceManager().newInstance(listener);
        } catch (Throwable t) {
            t = ExceptionUtils.unwrapInvocationTargetException(t);
            ExceptionUtils.handleThrowable(t);
            getLogger().error(sm.getString("standardContext.applicationListener", listeners[i]), t);
            ok = false;
        }
    }
    if (!ok) {
        getLogger().error(sm.getString("standardContext.applicationSkipped"));
        return false;
    }
    // Sort listeners in two arrays
    List<Object> eventListeners = new ArrayList<>();
    List<Object> lifecycleListeners = new ArrayList<>();
    for (Object result : results) {
        if ((result instanceof ServletContextAttributeListener) || (result instanceof ServletRequestAttributeListener) || (result instanceof ServletRequestListener) || (result instanceof HttpSessionIdListener) || (result instanceof HttpSessionAttributeListener)) {
            eventListeners.add(result);
        }
        if ((result instanceof ServletContextListener) || (result instanceof HttpSessionListener)) {
            lifecycleListeners.add(result);
        }
    }
    // Listener instances may have been added directly to this Context by
    // ServletContextInitializers and other code via the pluggability APIs.
    // Put them these listeners after the ones defined in web.xml and/or
    // annotations then overwrite the list of instances with the new, full
    // list.
    eventListeners.addAll(Arrays.asList(getApplicationEventListeners()));
    setApplicationEventListeners(eventListeners.toArray());
    for (Object lifecycleListener : getApplicationLifecycleListeners()) {
        lifecycleListeners.add(lifecycleListener);
        if (lifecycleListener instanceof ServletContextListener) {
            noPluggabilityListeners.add(lifecycleListener);
        }
    }
    setApplicationLifecycleListeners(lifecycleListeners.toArray());
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Sending application start events");
    }
    // Ensure context is not null
    getServletContext();
    context.setNewServletContextListenerAllowed(false);
    Object[] instances = getApplicationLifecycleListeners();
    if (instances == null || instances.length == 0) {
        return ok;
    }
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    ServletContextEvent tldEvent = null;
    if (noPluggabilityListeners.size() > 0) {
        noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
        tldEvent = new ServletContextEvent(noPluggabilityServletContext);
    }
    for (Object instance : instances) {
        if (!(instance instanceof ServletContextListener)) {
            continue;
        }
        ServletContextListener listener = (ServletContextListener) instance;
        try {
            fireContainerEvent("beforeContextInitialized", listener);
            if (noPluggabilityListeners.contains(listener)) {
                listener.contextInitialized(tldEvent);
            } else {
                listener.contextInitialized(event);
            }
            fireContainerEvent("afterContextInitialized", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            fireContainerEvent("afterContextInitialized", listener);
            getLogger().error(sm.getString("standardContext.listenerStart", instance.getClass().getName()), t);
            ok = false;
        }
    }
    return ok;
}
Also used : ServletContextAttributeListener(jakarta.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(jakarta.servlet.ServletRequestAttributeListener) HttpSessionListener(jakarta.servlet.http.HttpSessionListener) ServletContextListener(jakarta.servlet.ServletContextListener) ServletRequestListener(jakarta.servlet.ServletRequestListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) HttpSessionAttributeListener(jakarta.servlet.http.HttpSessionAttributeListener) HttpSessionIdListener(jakarta.servlet.http.HttpSessionIdListener) ServletContextEvent(jakarta.servlet.ServletContextEvent)

Example 2 with HttpSessionListener

use of jakarta.servlet.http.HttpSessionListener in project tomcat by apache.

the class StandardSession method tellNew.

/**
 * Inform the listeners about the new session.
 */
public void tellNew() {
    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_CREATED_EVENT, null);
    // Notify interested application event listeners
    Context context = manager.getContext();
    Object[] listeners = context.getApplicationLifecycleListeners();
    if (listeners != null && listeners.length > 0) {
        HttpSessionEvent event = new HttpSessionEvent(getSession());
        for (Object o : listeners) {
            if (!(o instanceof HttpSessionListener)) {
                continue;
            }
            HttpSessionListener listener = (HttpSessionListener) o;
            try {
                context.fireContainerEvent("beforeSessionCreated", listener);
                listener.sessionCreated(event);
                context.fireContainerEvent("afterSessionCreated", listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                try {
                    context.fireContainerEvent("afterSessionCreated", listener);
                } catch (Exception e) {
                // Ignore
                }
                manager.getContext().getLogger().error(sm.getString("standardSession.sessionEvent"), t);
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(jakarta.servlet.ServletContext) HttpSessionListener(jakarta.servlet.http.HttpSessionListener) HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ObjectStreamException(java.io.ObjectStreamException) WriteAbortedException(java.io.WriteAbortedException)

Example 3 with HttpSessionListener

use of jakarta.servlet.http.HttpSessionListener in project tomcat by apache.

the class StandardSession method expire.

/**
 * Perform the internal processing required to invalidate this session,
 * without triggering an exception if the session has already expired.
 *
 * @param notify Should we notify listeners about the demise of
 *  this session?
 */
public void expire(boolean notify) {
    // isValid is false
    if (!isValid) {
        return;
    }
    synchronized (this) {
        // entered as per bug 56339
        if (expiring || !isValid) {
            return;
        }
        if (manager == null) {
            return;
        }
        // Mark this session as "being expired"
        expiring = true;
        // Notify interested application event listeners
        // FIXME - Assumes we call listeners in reverse order
        Context context = manager.getContext();
        // listeners
        if (notify) {
            ClassLoader oldContextClassLoader = null;
            try {
                oldContextClassLoader = context.bind(Globals.IS_SECURITY_ENABLED, null);
                Object[] listeners = context.getApplicationLifecycleListeners();
                if (listeners != null && listeners.length > 0) {
                    HttpSessionEvent event = new HttpSessionEvent(getSession());
                    for (int i = 0; i < listeners.length; i++) {
                        int j = (listeners.length - 1) - i;
                        if (!(listeners[j] instanceof HttpSessionListener)) {
                            continue;
                        }
                        HttpSessionListener listener = (HttpSessionListener) listeners[j];
                        try {
                            context.fireContainerEvent("beforeSessionDestroyed", listener);
                            listener.sessionDestroyed(event);
                            context.fireContainerEvent("afterSessionDestroyed", listener);
                        } catch (Throwable t) {
                            ExceptionUtils.handleThrowable(t);
                            try {
                                context.fireContainerEvent("afterSessionDestroyed", listener);
                            } catch (Exception e) {
                            // Ignore
                            }
                            manager.getContext().getLogger().error(sm.getString("standardSession.sessionEvent"), t);
                        }
                    }
                }
            } finally {
                context.unbind(Globals.IS_SECURITY_ENABLED, oldContextClassLoader);
            }
        }
        if (activityCheck) {
            accessCount.set(0);
        }
        // Remove this session from our manager's active sessions
        manager.remove(this, true);
        // Notify interested session event listeners
        if (notify) {
            fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);
        }
        // Call the logout method
        if (principal instanceof TomcatPrincipal) {
            TomcatPrincipal gp = (TomcatPrincipal) principal;
            try {
                gp.logout();
            } catch (Exception e) {
                manager.getContext().getLogger().error(sm.getString("standardSession.logoutfail"), e);
            }
        }
        // We have completed expire of this session
        setValid(false);
        expiring = false;
        // Unbind any objects associated with this session
        String[] keys = keys();
        ClassLoader oldContextClassLoader = null;
        try {
            oldContextClassLoader = context.bind(Globals.IS_SECURITY_ENABLED, null);
            for (String key : keys) {
                removeAttributeInternal(key, notify);
            }
        } finally {
            context.unbind(Globals.IS_SECURITY_ENABLED, oldContextClassLoader);
        }
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(jakarta.servlet.ServletContext) HttpSessionListener(jakarta.servlet.http.HttpSessionListener) HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ObjectStreamException(java.io.ObjectStreamException) WriteAbortedException(java.io.WriteAbortedException) TomcatPrincipal(org.apache.catalina.TomcatPrincipal)

Aggregations

HttpSessionListener (jakarta.servlet.http.HttpSessionListener)3 ServletContext (jakarta.servlet.ServletContext)2 HttpSessionEvent (jakarta.servlet.http.HttpSessionEvent)2 IOException (java.io.IOException)2 NotSerializableException (java.io.NotSerializableException)2 ObjectStreamException (java.io.ObjectStreamException)2 WriteAbortedException (java.io.WriteAbortedException)2 Context (org.apache.catalina.Context)2 ServletContextAttributeListener (jakarta.servlet.ServletContextAttributeListener)1 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 ServletContextListener (jakarta.servlet.ServletContextListener)1 ServletRequestAttributeListener (jakarta.servlet.ServletRequestAttributeListener)1 ServletRequestListener (jakarta.servlet.ServletRequestListener)1 HttpSessionAttributeListener (jakarta.servlet.http.HttpSessionAttributeListener)1 HttpSessionIdListener (jakarta.servlet.http.HttpSessionIdListener)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 TomcatPrincipal (org.apache.catalina.TomcatPrincipal)1 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)1