Java Code Examples for org.apache.catalina.Session#access()

The following examples show how to use org.apache.catalina.Session#access() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentManagerBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * This method checks the persistence store if persistence is enabled,
 * otherwise just uses the functionality from ManagerBase.
 */
@Override
public Session findSession(String id) throws IOException {

    Session session = super.findSession(id);
    // OK, at this point, we're not sure if another thread is trying to
    // remove the session or not so the only way around this is to lock it
    // (or attempt to) and then try to get it by this session id again. If
    // the other code ran swapOut, then we should get a null back during
    // this run, and if not, we lock it out so we can access the session
    // safely.
    if(session != null) {
        synchronized(session){
            session = super.findSession(session.getIdInternal());
            if(session != null){
               // To keep any external calling code from messing up the
               // concurrency.
               session.access();
               session.endAccess();
            }
        }
    }
    if (session != null)
        return session;

    // See if the Session is in the Store
    session = swapIn(id);
    return session;
}
 
Example 2
Source File: PersistentManagerBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void reactivateLoadedSession(String id, Session session) {
    if(log.isDebugEnabled())
        log.debug(sm.getString("persistentManager.swapIn", id));

    session.setManager(this);
    // make sure the listeners know about it.
    ((StandardSession)session).tellNew();
    add(session);
    ((StandardSession)session).activate();
    // endAccess() to ensure timeouts happen correctly.
    // access() to keep access count correct or it will end up
    // negative
    session.access();
    session.endAccess();
}
 
Example 3
Source File: PersistentManagerBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Return the active Session, associated with this Manager, with the
 * specified session id (if any); otherwise return <code>null</code>.
 * This method checks the persistence store if persistence is enabled,
 * otherwise just uses the functionality from ManagerBase.
 *
 * @param id The session id for the session to be returned
 *
 * @exception IllegalStateException if a new session cannot be
 *  instantiated for any reason
 * @exception IOException if an input/output error occurs while
 *  processing this request
 */
@Override
public Session findSession(String id) throws IOException {

    Session session = super.findSession(id);
    // OK, at this point, we're not sure if another thread is trying to
    // remove the session or not so the only way around this is to lock it
    // (or attempt to) and then try to get it by this session id again. If
    // the other code ran swapOut, then we should get a null back during
    // this run, and if not, we lock it out so we can access the session
    // safely.
    if(session != null) {
        synchronized(session){
            session = super.findSession(session.getIdInternal());
            if(session != null){
               // To keep any external calling code from messing up the
               // concurrency.
               session.access();
               session.endAccess();
            }
        }
    }
    if (session != null)
        return session;

    // See if the Session is in the Store
    session = swapIn(id);
    return session;

}
 
Example 4
Source File: PersistentManagerBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * This method checks the persistence store if persistence is enabled,
 * otherwise just uses the functionality from ManagerBase.
 */
@Override
public Session findSession(String id) throws IOException {

    Session session = super.findSession(id);
    // OK, at this point, we're not sure if another thread is trying to
    // remove the session or not so the only way around this is to lock it
    // (or attempt to) and then try to get it by this session id again. If
    // the other code ran swapOut, then we should get a null back during
    // this run, and if not, we lock it out so we can access the session
    // safely.
    if(session != null) {
        synchronized(session){
            session = super.findSession(session.getIdInternal());
            if(session != null){
               // To keep any external calling code from messing up the
               // concurrency.
               session.access();
               session.endAccess();
            }
        }
    }
    if (session != null)
        return session;

    // See if the Session is in the Store
    session = swapIn(id);
    return session;
}
 
Example 5
Source File: DatastoreValve.java    From tomcat-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>If the manager contain a store, use it to persist the session at the end of the request.</p>
 */
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {

  log.debug("Processing request with session:" + request.getRequestedSessionId());

  getNext().invoke(request, response);

  Context context = request.getContext();
  Manager manager = context.getManager();

  Session session = request.getSessionInternal(false);
  if (session != null && !isUriExcluded(request.getRequestURI())) {
    log.debug("Persisting session with id: " + session.getId());
    session.access();
    session.endAccess();

    if (manager instanceof StoreManager) {
      StoreManager storeManager = (StoreManager) manager;
      storeManager.getStore().save(session);
      storeManager.removeSuper(session);
    } else {
      log.error("In order to persist the session the manager must implement StoreManager");
    }
  } else {
    log.debug("Session not persisted (Non existent or the URI is ignored)");
  }

}
 
Example 6
Source File: RedissonSessionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Session findSession(String id, boolean notify) throws IOException {
    Session result = super.findSession(id);
    if (result == null) {
        if (id != null) {
            Map<String, Object> attrs = new HashMap<String, Object>();
            try {
                attrs = getMap(id).getAll(RedissonSession.ATTRS);
            } catch (Exception e) {
                log.error("Can't read session object by id: " + id, e);
            }

            if (attrs.isEmpty() || (broadcastSessionEvents && getNotifiedNodes(id).contains(nodeId))) {
                log.info("Session " + id + " can't be found");
                return null;    
            }
            
            RedissonSession session = (RedissonSession) createEmptySession();
            session.load(attrs);
            session.setId(id, notify);
            
            session.access();
            session.endAccess();
            return session;
        }
        return null;
    }

    result.access();
    result.endAccess();
    
    return result;
}
 
Example 7
Source File: RedissonSessionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Session findSession(String id, boolean notify) throws IOException {
    Session result = super.findSession(id);
    if (result == null) {
        if (id != null) {
            Map<String, Object> attrs = new HashMap<String, Object>();
            try {
                attrs = getMap(id).getAll(RedissonSession.ATTRS);
            } catch (Exception e) {
                log.error("Can't read session object by id: " + id, e);
            }

            if (attrs.isEmpty() || (broadcastSessionEvents && getNotifiedNodes(id).contains(nodeId))) {
                log.info("Session " + id + " can't be found");
                return null;    
            }
            
            RedissonSession session = (RedissonSession) createEmptySession();
            session.load(attrs);
            session.setId(id, notify);
            
            session.access();
            session.endAccess();
            return session;
        }
        return null;
    }

    result.access();
    result.endAccess();
    
    return result;
}
 
Example 8
Source File: RedissonSessionManager.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Session findSession(String id, boolean notify) throws IOException {
    Session result = super.findSession(id);
    if (result == null) {
        if (id != null) {
            Map<String, Object> attrs = new HashMap<String, Object>();
            try {
                attrs = getMap(id).getAll(RedissonSession.ATTRS);
            } catch (Exception e) {
                log.error("Can't read session object by id: " + id, e);
            }

            if (attrs.isEmpty() || (broadcastSessionEvents && getNotifiedNodes(id).contains(nodeId))) {  
                log.info("Session " + id + " can't be found");
                return null;    
            }
            
            RedissonSession session = (RedissonSession) createEmptySession();
            session.load(attrs);
            session.setId(id, notify);
            
            session.access();
            session.endAccess();
            return session;
        }
        return null;
    }

    result.access();
    result.endAccess();
    
    return result;
}
 
Example 9
Source File: ApplicationHttpRequest.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Return the session associated with this Request, creating one
 * if necessary and requested.
 *
 * @param create Create a new session if one does not exist
 */
@Override
public HttpSession getSession(boolean create) {

    if (crossContext) {

        // There cannot be a session if no context has been assigned yet
        if (context == null)
            return null;

        // Return the current session if it exists and is valid
        if (session != null && session.isValid()) {
            return session.getSession();
        }

        HttpSession other = super.getSession(false);
        if (create && (other == null)) {
            // First create a session in the first context: the problem is
            // that the top level request is the only one which can
            // create the cookie safely
            other = super.getSession(true);
        }
        if (other != null) {
            Session localSession = null;
            try {
                localSession =
                    context.getManager().findSession(other.getId());
                if (localSession != null && !localSession.isValid()) {
                    localSession = null;
                }
            } catch (IOException e) {
                // Ignore
            }
            if (localSession == null && create) {
                localSession =
                    context.getManager().createSession(other.getId());
            }
            if (localSession != null) {
                localSession.access();
                session = localSession;
                return session.getSession();
            }
        }
        return null;

    } else {
        return super.getSession(create);
    }

}
 
Example 10
Source File: TestPersistentManagerIntegration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void backsUpOnce_56698() throws IOException, LifecycleException,
        InterruptedException {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setDistributable(true);

    Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
    ctx.addServletMappingDecoded("/dummy", "DummyServlet");

    PersistentManager manager = new PersistentManager();
    TesterStore store = new TesterStore();

    manager.setStore(store);
    manager.setMaxIdleBackup(0);
    ctx.setManager(manager);
    tomcat.start();
    String sessionId = getUrl("http://localhost:" + getPort() + "/dummy")
            .toString();

    // Note: PersistenceManager.findSession() silently updates
    // session.lastAccessedTime, so call it only once before other work.
    Session session = manager.findSession(sessionId);

    // Wait until request processing ends, as Request.recycle() updates
    // session.lastAccessedTime via session.endAccess().
    waitWhileSessionIsActive((StandardSession) session);

    long lastAccessedTime = session.getLastAccessedTimeInternal();

    // Session should be idle at least for 0 second (maxIdleBackup)
    // to be eligible for persistence, thus no need to wait.

    // Waiting a bit, to catch changes in last accessed time of a session
    waitForClockUpdate();

    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());

    // session was not accessed, so no save will be performed
    waitForClockUpdate();
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());

    // access session
    session.access();
    session.endAccess();

    // session was accessed, so it will be saved once again
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId),
            store.getSavedIds());

    // session was not accessed, so once again no save will happen
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId),
            store.getSavedIds());
}
 
Example 11
Source File: ApplicationHttpRequest.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Return the session associated with this Request, creating one
 * if necessary and requested.
 *
 * @param create Create a new session if one does not exist
 */
@Override
public HttpSession getSession(boolean create) {

    if (crossContext) {
        
        // There cannot be a session if no context has been assigned yet
        if (context == null)
            return (null);

        // Return the current session if it exists and is valid
        if (session != null && session.isValid()) {
            return (session.getSession());
        }

        HttpSession other = super.getSession(false);
        if (create && (other == null)) {
            // First create a session in the first context: the problem is
            // that the top level request is the only one which can 
            // create the cookie safely
            other = super.getSession(true);
        }
        if (other != null) {
            Session localSession = null;
            try {
                localSession =
                    context.getManager().findSession(other.getId());
                if (localSession != null && !localSession.isValid()) {
                    localSession = null;
                }
            } catch (IOException e) {
                // Ignore
            }
            if (localSession == null && create) {
                localSession = 
                    context.getManager().createSession(other.getId());
            }
            if (localSession != null) {
                localSession.access();
                session = localSession;
                return session.getSession();
            }
        }
        return null;

    } else {
        return super.getSession(create);
    }

}
 
Example 12
Source File: TestPersistentManager.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void backsUpOnce_56698() throws IOException, LifecycleException,
        InterruptedException {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
    ctx.addServletMapping("/dummy", "DummyServlet");

    PersistentManager manager = new PersistentManager();
    DummyStore store = new DummyStore();

    manager.setStore(store);
    manager.setMaxIdleBackup(0);
    manager.setDistributable(true);
    ctx.setManager(manager);
    tomcat.start();
    String sessionId = getUrl("http://localhost:" + getPort() + "/dummy")
            .toString();

    // Note: PersistenceManager.findSession() silently updates
    // session.lastAccessedTime, so call it only once before other work.
    Session session = manager.findSession(sessionId);

    // Wait until request processing ends, as Request.recycle() updates
    // session.lastAccessedTime via session.endAccess().
    waitWhileSessionIsActive((StandardSession) session);

    long lastAccessedTime = session.getLastAccessedTimeInternal();

    // Session should be idle at least for 0 second (maxIdleBackup)
    // to be eligible for persistence, thus no need to wait.

    // Waiting a bit, to catch changes in last accessed time of a session
    waitForClockUpdate();

    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());

    // session was not accessed, so no save will be performed
    waitForClockUpdate();
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());

    // access session
    session.access();
    session.endAccess();

    // session was accessed, so it will be saved once again
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId),
            store.getSavedIds());

    // session was not accessed, so once again no save will happen
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId),
            store.getSavedIds());
}
 
Example 13
Source File: ApplicationHttpRequest.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Return the session associated with this Request, creating one
 * if necessary and requested.
 *
 * @param create Create a new session if one does not exist
 */
@Override
public HttpSession getSession(boolean create) {

    if (crossContext) {
        
        // There cannot be a session if no context has been assigned yet
        if (context == null)
            return (null);

        // Return the current session if it exists and is valid
        if (session != null && session.isValid()) {
            return (session.getSession());
        }

        HttpSession other = super.getSession(false);
        if (create && (other == null)) {
            // First create a session in the first context: the problem is
            // that the top level request is the only one which can 
            // create the cookie safely
            other = super.getSession(true);
        }
        if (other != null) {
            Session localSession = null;
            try {
                localSession =
                    context.getManager().findSession(other.getId());
                if (localSession != null && !localSession.isValid()) {
                    localSession = null;
                }
            } catch (IOException e) {
                // Ignore
            }
            if (localSession == null && create) {
                localSession = 
                    context.getManager().createSession(other.getId());
            }
            if (localSession != null) {
                localSession.access();
                session = localSession;
                return session.getSession();
            }
        }
        return null;

    } else {
        return super.getSession(create);
    }

}
 
Example 14
Source File: TestPersistentManagerIntegration.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void backsUpOnce_56698() throws IOException, LifecycleException,
        InterruptedException {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setDistributable(true);

    Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
    ctx.addServletMapping("/dummy", "DummyServlet");

    PersistentManager manager = new PersistentManager();
    TesterStore store = new TesterStore();

    manager.setStore(store);
    manager.setMaxIdleBackup(0);
    ctx.setManager(manager);
    tomcat.start();
    String sessionId = getUrl("http://localhost:" + getPort() + "/dummy")
            .toString();

    // Note: PersistenceManager.findSession() silently updates
    // session.lastAccessedTime, so call it only once before other work.
    Session session = manager.findSession(sessionId);

    // Wait until request processing ends, as Request.recycle() updates
    // session.lastAccessedTime via session.endAccess().
    waitWhileSessionIsActive((StandardSession) session);

    long lastAccessedTime = session.getLastAccessedTimeInternal();

    // Session should be idle at least for 0 second (maxIdleBackup)
    // to be eligible for persistence, thus no need to wait.

    // Waiting a bit, to catch changes in last accessed time of a session
    waitForClockUpdate();

    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());

    // session was not accessed, so no save will be performed
    waitForClockUpdate();
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
    Assert.assertEquals(lastAccessedTime, session.getLastAccessedTimeInternal());

    // access session
    session.access();
    session.endAccess();

    // session was accessed, so it will be saved once again
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId),
            store.getSavedIds());

    // session was not accessed, so once again no save will happen
    manager.processPersistenceChecks();
    Assert.assertEquals(Arrays.asList(sessionId, sessionId),
            store.getSavedIds());
}