Search in sources :

Example 41 with Manager

use of org.apache.catalina.Manager in project tomcat70 by apache.

the class HTMLManagerServlet method getSessionsForName.

protected List<Session> getSessionsForName(ContextName cn, StringManager smClient) {
    if ((cn == null) || !(cn.getPath().startsWith("/") || cn.getPath().equals(""))) {
        String path = null;
        if (cn != null) {
            path = cn.getPath();
        }
        throw new IllegalArgumentException(smClient.getString("managerServlet.invalidPath", RequestUtil.filter(path)));
    }
    Context ctxt = (Context) host.findChild(cn.getName());
    if (null == ctxt) {
        throw new IllegalArgumentException(smClient.getString("managerServlet.noContext", RequestUtil.filter(cn.getDisplayName())));
    }
    Manager manager = ctxt.getManager();
    List<Session> sessions = new ArrayList<Session>();
    sessions.addAll(Arrays.asList(manager.findSessions()));
    if (manager instanceof DistributedManager && showProxySessions) {
        // Add dummy proxy sessions
        Set<String> sessionIds = ((DistributedManager) manager).getSessionIdsFull();
        // Remove active (primary and backup) session IDs from full list
        for (Session session : sessions) {
            sessionIds.remove(session.getId());
        }
        // Left with just proxy sessions - add them
        for (String sessionId : sessionIds) {
            sessions.add(new DummyProxySession(sessionId));
        }
    }
    return sessions;
}
Also used : Context(org.apache.catalina.Context) ArrayList(java.util.ArrayList) DistributedManager(org.apache.catalina.DistributedManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.catalina.Session) DistributedManager(org.apache.catalina.DistributedManager)

Example 42 with Manager

use of org.apache.catalina.Manager in project tomcat70 by apache.

the class ManagerServlet method sessions.

/**
 * Session information for the web application at the specified context path.
 * Displays a profile of session thisAccessedTime listing number
 * of sessions for each 10 minute interval up to 10 hours.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to list session information for
 * @param idle Expire all sessions with idle time &gt; idle for this context
 */
protected void sessions(PrintWriter writer, ContextName cn, int idle, StringManager smClient) {
    if (debug >= 1) {
        log("sessions: Session information for web application '" + cn + "'");
        if (idle >= 0)
            log("sessions: Session expiration for " + idle + " minutes '" + cn + "'");
    }
    if (!validateContextName(cn, writer, smClient)) {
        return;
    }
    String displayPath = cn.getDisplayName();
    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext", RequestUtil.filter(displayPath)));
            return;
        }
        Manager manager = context.getManager();
        if (manager == null) {
            writer.println(smClient.getString("managerServlet.noManager", RequestUtil.filter(displayPath)));
            return;
        }
        int maxCount = 60;
        int histoInterval = 1;
        int maxInactiveInterval = context.getSessionTimeout();
        if (maxInactiveInterval > 0) {
            histoInterval = maxInactiveInterval / maxCount;
            if (histoInterval * maxCount < maxInactiveInterval)
                histoInterval++;
            if (0 == histoInterval)
                histoInterval = 1;
            maxCount = maxInactiveInterval / histoInterval;
            if (histoInterval * maxCount < maxInactiveInterval)
                maxCount++;
        }
        writer.println(smClient.getString("managerServlet.sessions", displayPath));
        writer.println(smClient.getString("managerServlet.sessiondefaultmax", "" + maxInactiveInterval));
        Session[] sessions = manager.findSessions();
        int[] timeout = new int[maxCount + 1];
        int notimeout = 0;
        int expired = 0;
        long now = System.currentTimeMillis();
        for (int i = 0; i < sessions.length; i++) {
            int time;
            if (LAST_ACCESS_AT_START) {
                time = (int) ((now - sessions[i].getLastAccessedTimeInternal()) / 1000L);
            } else {
                time = (int) ((now - sessions[i].getThisAccessedTimeInternal()) / 1000L);
            }
            if (idle >= 0 && time >= idle * 60) {
                sessions[i].expire();
                expired++;
            }
            time = time / 60 / histoInterval;
            if (time < 0)
                notimeout++;
            else if (time >= maxCount)
                timeout[maxCount]++;
            else
                timeout[time]++;
        }
        if (timeout[0] > 0)
            writer.println(smClient.getString("managerServlet.sessiontimeout", "<" + histoInterval, "" + timeout[0]));
        for (int i = 1; i < maxCount; i++) {
            if (timeout[i] > 0)
                writer.println(smClient.getString("managerServlet.sessiontimeout", "" + (i) * histoInterval + " - <" + (i + 1) * histoInterval, "" + timeout[i]));
        }
        if (timeout[maxCount] > 0) {
            writer.println(smClient.getString("managerServlet.sessiontimeout", ">=" + maxCount * histoInterval, "" + timeout[maxCount]));
        }
        if (notimeout > 0)
            writer.println(smClient.getString("managerServlet.sessiontimeout.unlimited", "" + notimeout));
        if (idle >= 0)
            writer.println(smClient.getString("managerServlet.sessiontimeout.expired", ">" + idle, "" + expired));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.sessions[" + displayPath + "]", t);
        writer.println(smClient.getString("managerServlet.exception", t.toString()));
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) Session(org.apache.catalina.Session)

Example 43 with Manager

use of org.apache.catalina.Manager in project tomcat70 by apache.

the class PersistentValve method invoke.

// --------------------------------------------------------- Public Methods
/**
 * Select the appropriate child Context to process this request,
 * based on the specified request URI.  If no matching Context can
 * be found, return an appropriate HTTP error.
 *
 * @param request Request to be processed
 * @param response Response to be produced
 *
 * @exception IOException if an input/output error occurred
 * @exception ServletException if a servlet error occurred
 */
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
    // Select the Context to be used for this Request
    Context context = request.getContext();
    if (context == null) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, sm.getString("standardHost.noContext"));
        return;
    }
    // Update the session last access time for our session (if any)
    String sessionId = request.getRequestedSessionId();
    Manager manager = context.getManager();
    if (sessionId != null && manager instanceof PersistentManager) {
        Store store = ((PersistentManager) manager).getStore();
        if (store != null) {
            Session session = null;
            try {
                session = store.load(sessionId);
            } catch (Exception e) {
                container.getLogger().error("deserializeError");
            }
            if (session != null) {
                if (!session.isValid() || isSessionStale(session, System.currentTimeMillis())) {
                    if (container.getLogger().isDebugEnabled()) {
                        container.getLogger().debug("session swapped in is invalid or expired");
                    }
                    session.expire();
                    store.remove(sessionId);
                } else {
                    session.setManager(manager);
                    // session.setId(sessionId); Only if new ???
                    manager.add(session);
                    // ((StandardSession)session).activate();
                    session.access();
                    session.endAccess();
                }
            }
        }
    }
    if (container.getLogger().isDebugEnabled()) {
        container.getLogger().debug("sessionId: " + sessionId);
    }
    // Ask the next valve to process the request.
    getNext().invoke(request, response);
    // If still processing async, don't try to store the session
    if (!request.isAsync()) {
        // Read the sessionid after the response.
        // HttpSession hsess = hreq.getSession(false);
        Session hsess;
        try {
            hsess = request.getSessionInternal(false);
        } catch (Exception ex) {
            hsess = null;
        }
        String newsessionId = null;
        if (hsess != null) {
            newsessionId = hsess.getIdInternal();
        }
        if (container.getLogger().isDebugEnabled()) {
            container.getLogger().debug("newsessionId: " + newsessionId);
        }
        if (newsessionId != null) {
            try {
                bind(context);
                /* store the session and remove it from the manager */
                if (manager instanceof PersistentManager) {
                    Session session = manager.findSession(newsessionId);
                    Store store = ((PersistentManager) manager).getStore();
                    if (store != null && session != null && session.isValid() && !isSessionStale(session, System.currentTimeMillis())) {
                        store.save(session);
                        ((PersistentManager) manager).removeSuper(session);
                        session.recycle();
                    } else {
                        if (container.getLogger().isDebugEnabled()) {
                            container.getLogger().debug("newsessionId store: " + store + " session: " + session + " valid: " + (session == null ? "N/A" : Boolean.toString(session.isValid())) + " stale: " + isSessionStale(session, System.currentTimeMillis()));
                        }
                    }
                } else {
                    if (container.getLogger().isDebugEnabled()) {
                        container.getLogger().debug("newsessionId Manager: " + manager);
                    }
                }
            } finally {
                unbind();
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) PersistentManager(org.apache.catalina.session.PersistentManager) Store(org.apache.catalina.Store) PersistentManager(org.apache.catalina.session.PersistentManager) Manager(org.apache.catalina.Manager) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Session(org.apache.catalina.Session)

Example 44 with Manager

use of org.apache.catalina.Manager in project tomcat70 by apache.

the class HostConfig method checkUndeploy.

/**
 * Check for old versions of applications using parallel deployment that are
 * now unused (have no active sessions) and undeploy any that are found.
 */
public synchronized void checkUndeploy() {
    // Need ordered set of names
    SortedSet<String> sortedAppNames = new TreeSet<String>();
    sortedAppNames.addAll(deployed.keySet());
    if (sortedAppNames.size() < 2) {
        return;
    }
    Iterator<String> iter = sortedAppNames.iterator();
    ContextName previous = new ContextName(iter.next(), false);
    do {
        ContextName current = new ContextName(iter.next(), false);
        if (current.getPath().equals(previous.getPath())) {
            // Current and previous are same path - current will always
            // be a later version
            Context previousContext = (Context) host.findChild(previous.getName());
            Context currentContext = (Context) host.findChild(current.getName());
            if (previousContext != null && currentContext != null && currentContext.getState().isAvailable() && !isServiced(previous.getName())) {
                Manager manager = previousContext.getManager();
                if (manager != null) {
                    int sessionCount;
                    if (manager instanceof DistributedManager) {
                        sessionCount = ((DistributedManager) manager).getActiveSessionsFull();
                    } else {
                        sessionCount = manager.getActiveSessions();
                    }
                    if (sessionCount == 0) {
                        if (log.isInfoEnabled()) {
                            log.info(sm.getString("hostConfig.undeployVersion", previous.getName()));
                        }
                        DeployedApplication app = deployed.get(previous.getName());
                        String[] resources = app.redeployResources.keySet().toArray(new String[0]);
                        // Version is unused - undeploy it completely
                        // The -1 is a 'trick' to ensure all redeploy
                        // resources are removed
                        undeploy(app);
                        deleteRedeployResources(app, resources, -1, true);
                    }
                }
            }
        }
        previous = current;
    } while (iter.hasNext());
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) Context(org.apache.catalina.Context) TreeSet(java.util.TreeSet) DistributedManager(org.apache.catalina.DistributedManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) ContextName(org.apache.catalina.util.ContextName) DistributedManager(org.apache.catalina.DistributedManager)

Example 45 with Manager

use of org.apache.catalina.Manager in project spring-boot by spring-projects.

the class TomcatServletWebServerFactory method configureSession.

private void configureSession(Context context) {
    long sessionTimeout = getSessionTimeoutInMinutes();
    context.setSessionTimeout((int) sessionTimeout);
    Boolean httpOnly = getSession().getCookie().getHttpOnly();
    if (httpOnly != null) {
        context.setUseHttpOnly(httpOnly);
    }
    if (getSession().isPersistent()) {
        Manager manager = context.getManager();
        if (manager == null) {
            manager = new StandardManager();
            context.setManager(manager);
        }
        configurePersistSession(manager);
    } else {
        context.addLifecycleListener(new DisablePersistSessionListener());
    }
}
Also used : StandardManager(org.apache.catalina.session.StandardManager) Manager(org.apache.catalina.Manager) StandardManager(org.apache.catalina.session.StandardManager)

Aggregations

Manager (org.apache.catalina.Manager)54 StringManager (org.apache.tomcat.util.res.StringManager)26 IOException (java.io.IOException)22 Session (org.apache.catalina.Session)22 Context (org.apache.catalina.Context)19 Container (org.apache.catalina.Container)13 LifecycleException (org.apache.catalina.LifecycleException)12 Lifecycle (org.apache.catalina.Lifecycle)11 Loader (org.apache.catalina.Loader)11 InstanceManager (org.apache.tomcat.InstanceManager)11 StandardManager (org.apache.catalina.session.StandardManager)10 Realm (org.apache.catalina.Realm)9 ServletException (javax.servlet.ServletException)7 ArrayList (java.util.ArrayList)6 NamingException (javax.naming.NamingException)6 Cluster (org.apache.catalina.Cluster)6 DistributedManager (org.apache.catalina.DistributedManager)6 StandardContext (org.apache.catalina.core.StandardContext)6 WebappLoader (org.apache.catalina.loader.WebappLoader)6 ServletException (jakarta.servlet.ServletException)5