예제 #1
0
  protected void expandSession(AbstractSession session) throws IOException {
    if (session != null) {
      String id = session.getId();
      HttpSession httpSession = lookupHttpSessionById.get(id);

      // Set 'timeLastAccess' upon session:
      {
        if (httpSession != null) {
          Date timeLastAccess = new Date(httpSession.getLastAccessedTime());
          session.setTimeLastAccess(timeLastAccess);
        }
      }

      expandSessionPrincipal(session);

      // Set 'requestURI' upon session:
      {
        if (httpSession != null) {
          List<String> requestURIs = RequestURISessionDecorator.getRequestURIs(httpSession);
          if (requestURIs != null) {
            Collections.reverse(requestURIs); // reverse the order!
            session.setRequestURIs(requestURIs);
          }
        }
      }

      // Set 'properties' upon session:
      {
        if (httpSession != null) {
          Map<String, Object> m = PropertiesSessionDecorator.getProperties(httpSession);
          if (m != null) {
            Properties properties = convertProperties(m);
            session.setProperties(properties);
          }
        }
      }
    }
  }
예제 #2
0
/**
 * @author <a href="mailto:[email protected]" >Morten Sabroe Mortensen</a>
 * @version $Id: HttpSessionAccessor.java,v 1.8 2007/05/21 17:56:06 momor Exp $
 */
public class HttpSessionAccessor extends AbstractSessionAccessor {
  /** Constructor. */
  public HttpSessionAccessor() {
    super();
  }

  /** */
  private static final Map<String, AbstractSession> lookupSessionById =
      new HashMap<String, AbstractSession>();

  /** */
  private static final Map<String, HttpSession> lookupHttpSessionById =
      Collections.synchronizedMap(new HashMap<String, HttpSession>());

  /** */
  private static int sessionCountMax;

  /** */
  private static Long sessionCountMaxTime;

  /** */
  public static synchronized void sessionCreated(HttpSessionEvent ev) {
    HttpSession httpSession = ev.getSession();
    String id = httpSession.getId();

    // Remember HTTP-session:
    {
      lookupHttpSessionById.put(id, httpSession);
    }

    AbstractSession session = null;

    synchronized (lookupSessionById) {
      session = lookupSessionById.get(id);
    }

    if (session == null) {
      Principal userPrincipal = null;
      Date timeCreation = new Date(httpSession.getCreationTime());
      Date timeLastAccess = new Date(httpSession.getLastAccessedTime());
      List<String> urisForLastRequests = null;
      Properties properties = null;

      session =
          new DefaultSession(
              id, userPrincipal, timeCreation, timeLastAccess, urisForLastRequests, properties);

      synchronized (lookupSessionById) {
        lookupSessionById.put(id, session);

        // Update 'sessionCountMax':
        {
          int sessionCount = lookupSessionById.size();
          if (sessionCount > sessionCountMax) {
            sessionCountMax = sessionCount;
            sessionCountMaxTime = System.currentTimeMillis();
          }
        }
      }
    }
  }

  /** */
  public static synchronized void sessionDestroyed(HttpSessionEvent ev) {
    HttpSession httpSession = ev.getSession();
    String id = httpSession.getId();

    synchronized (lookupSessionById) {
      lookupSessionById.remove(id);
    }

    // Forget HTTP-session:
    {
      lookupHttpSessionById.remove(id);
    }
  }

  /** */
  public Integer getSessionCount() throws IOException {
    Integer res = null;

    {
      synchronized (lookupSessionById) {
        res = lookupSessionById.size();
      }
    }

    return res;
  }

  /** */
  public Integer getSessionCountMax() throws IOException {
    Integer res = null;

    {
      synchronized (lookupSessionById) {
        res = sessionCountMax;
      }
    }

    return res;
  }

  /** */
  public void resetSessionCountMax() throws IOException {
    synchronized (lookupSessionById) {
      sessionCountMax = 0;
    }
  }

  /** */
  public List<String> getSessionIds() throws IOException {
    List<String> res = null;

    {
      synchronized (lookupSessionById) {
        Set<String> keySet = lookupSessionById.keySet();
        if (keySet != null) {
          res = new ArrayList<String>();
          res.addAll(keySet);
        }
      }
    }

    return res;
  }

  /** */
  public List<Session> getSessions() throws IOException {
    List<Session> res = null;

    {
      synchronized (lookupSessionById) {
        Collection<AbstractSession> values = lookupSessionById.values();
        if (values != null) {
          res = new ArrayList<Session>();

          for (AbstractSession session : values) {
            expandSession(session);
          }

          res.addAll(values);
        }
      }
    }

    return res;
  }

  /** */
  protected void expandSessionPrincipal(AbstractSession session) throws IOException {
    if (session != null) {
      String id = session.getId();
      HttpSession httpSession = lookupHttpSessionById.get(id);

      // Set 'userPrincipal' upon session:
      {
        if (httpSession != null) {
          Principal userPrincipal = PrincipalSessionDecorator.getPrincipal(httpSession);
          if (userPrincipal != null) {
            session.setUserPrincipal(userPrincipal);
          }
        }
      }
    }
  }

  /** */
  protected void expandSession(AbstractSession session) throws IOException {
    if (session != null) {
      String id = session.getId();
      HttpSession httpSession = lookupHttpSessionById.get(id);

      // Set 'timeLastAccess' upon session:
      {
        if (httpSession != null) {
          Date timeLastAccess = new Date(httpSession.getLastAccessedTime());
          session.setTimeLastAccess(timeLastAccess);
        }
      }

      expandSessionPrincipal(session);

      // Set 'requestURI' upon session:
      {
        if (httpSession != null) {
          List<String> requestURIs = RequestURISessionDecorator.getRequestURIs(httpSession);
          if (requestURIs != null) {
            Collections.reverse(requestURIs); // reverse the order!
            session.setRequestURIs(requestURIs);
          }
        }
      }

      // Set 'properties' upon session:
      {
        if (httpSession != null) {
          Map<String, Object> m = PropertiesSessionDecorator.getProperties(httpSession);
          if (m != null) {
            Properties properties = convertProperties(m);
            session.setProperties(properties);
          }
        }
      }
    }
  }

  /** */
  protected Properties convertProperties(Map<String, Object> m) {
    Properties res = null;

    {
      if (m != null) {
        res = new Properties();

        Set<String> keys = m.keySet();
        for (String key : keys) {
          String value = null;

          // Set 'value':
          {
            Object o = m.get(key);
            if (o != null) {
              value = o.toString();
            }
          }

          res.setProperty(key, value);
        }
      }
    }

    return res;
  }

  /** */
  public Session getSessionFromId(String id) throws IOException {
    Session res = null;

    {
      synchronized (lookupSessionById) {
        AbstractSession session = lookupSessionById.get(id);
        expandSession(session);
        res = session;
      }
    }

    return res;
  }

  /** */
  protected List<AbstractSession> filterByUserPrincipal(
      Collection<AbstractSession> values, Principal userPrincipal) throws IOException {
    List<AbstractSession> res = null;

    {
      if (values != null) {
        res = new ArrayList<AbstractSession>();

        for (AbstractSession session : values) {
          expandSessionPrincipal(session);
        }

        if (userPrincipal == null) {
          for (AbstractSession session : values) {
            Principal p = session.getUserPrincipal();
            if (p == null) {
              res.add(session);
            }
          }
        } else {
          for (AbstractSession session : values) {
            Principal p = session.getUserPrincipal();
            if (PrincipalUtil.equalsIgnoreRealm(userPrincipal, p)) {
              res.add(session);
            }
          }
        }
      }
    }

    return res;
  }

  /** */
  public List<Session> getSessionsFromUserPrincipal(Principal userPrincipal) throws IOException {
    List<Session> res = null;

    {
      synchronized (lookupSessionById) {
        Collection<AbstractSession> values = lookupSessionById.values();
        values = filterByUserPrincipal(values, userPrincipal);

        if (values != null) {
          res = new ArrayList<Session>();

          for (AbstractSession session : values) {
            expandSession(session);
          }

          res.addAll(values);
        }
      }
    }

    return res;
  }

  /** */
  public Properties getProperties() throws IOException {
    Properties res = null;

    {
      res = super.getProperties();

      if (res == null) {
        res = new Properties();
      }

      Integer sessionCount = null;
      Integer sessionCountMax = null;
      Long sessionCountMaxTime = null;

      synchronized (lookupSessionById) {
        sessionCount = lookupSessionById.size();
        sessionCountMax = this.sessionCountMax;
        sessionCountMaxTime = this.sessionCountMaxTime;
      }

      if (sessionCount != null) {
        res.setProperty("session.count", Integer.toString(sessionCount));
      }

      if (sessionCountMax != null) {
        res.setProperty("session.count-max", Integer.toString(sessionCountMax));
      }

      if (sessionCountMaxTime != null) {
        String sessionCountMaxTimeText =
            ApplicationConstants.FORMAT_DATE.format(sessionCountMaxTime);
        res.setProperty("session.count-max.timestamp", sessionCountMaxTimeText);
      }
    }

    return res;
  }

  /** */
  public void dispose() {
    super.dispose();
  }
}