Package org.apache.wiki.auth

## Class SessionMonitor

- java.lang.Object
- - org.apache.wiki.auth.SessionMonitor

All Implemented Interfaces:
  `java.util.EventListener`, `javax.servlet.http.HttpSessionListener`

    public class SessionMonitor
    extends java.lang.Object
    implements javax.servlet.http.HttpSessionListener

Manages Sessions for different Engines.

The Sessions are stored both in the remote user HttpSession and in the SessionMonitor for the Engine. This class must be configured as a session listener in the web.xml for the wiki web application.

### Constructor Summary

Constructors
`SessionMonitor()` Construct the SessionListener

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`void` `addWikiEventListener​(WikiEventListener listener)` Registers a WikiEventListener with this instance.
`Session` `find​(java.lang.String sessionId)` Looks up the wiki session associated with a user's Http session and adds it to the session cache.
`Session` `find​(javax.servlet.http.HttpSession session)` Looks up the wiki session associated with a user's Http session and adds it to the session cache.
`protected void` `fireEvent​(int type, java.security.Principal principal, Session session)` Fires a WikiSecurityEvent to all registered listeners.
`static SessionMonitor` `getInstance​(Engine engine)` Returns the instance of the SessionMonitor for this wiki.
`void` `remove​(javax.servlet.http.HttpServletRequest request)` Removes the wiki session associated with the user's HttpRequest from the session cache.
`void` `remove​(javax.servlet.http.HttpSession session)` Removes the wiki session associated with the user's HttpSession from the session cache.
`void` `removeWikiEventListener​(WikiEventListener listener)` Un-registers a WikiEventListener with this instance.
`void` `sessionCreated​(javax.servlet.http.HttpSessionEvent se)` Fires when the web container creates a new HTTP session.
`void` `sessionDestroyed​(javax.servlet.http.HttpSessionEvent se)` Removes the user's WikiSession from the internal session cache when the web container destroys an HTTP session.
`int` `sessions()` Returns the current number of active wiki sessions.
`java.security.Principal[]` `userPrincipals()` Returns the current wiki users as a sorted array of Principal objects.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### SessionMonitor

    public SessionMonitor()

Construct the SessionListener

### Method Detail

#### getInstance

    public static SessionMonitor getInstance​(Engine engine)

Returns the instance of the SessionMonitor for this wiki. Only one SessionMonitor exists per Engine.

Parameters:
  `engine` - the wiki engine
Returns:
  the session monitor

#### find

    public final Session find​(javax.servlet.http.HttpSession session)

Looks up the wiki session associated with a user's Http session and adds it to the session cache. This method will return the "guest session" as constructed by `SessionSPI.guest(Engine)` if the HttpSession is not currently associated with a WikiSession. This method is guaranteed to return a non-`null` WikiSession.

Internally, the session is stored in a HashMap; keys are the HttpSession objects, while the values are `WeakReference`-wrapped WikiSessions.

Parameters:
  `session` - the HTTP session
Returns:
  the wiki session

#### find

    public final Session find​(java.lang.String sessionId)

Looks up the wiki session associated with a user's Http session and adds it to the session cache. This method will return the "guest session" as constructed by `SessionSPI.guest(Engine)` if the HttpSession is not currently associated with a WikiSession. This method is guaranteed to return a non-`null` WikiSession.

Internally, the session is stored in a HashMap; keys are the HttpSession objects, while the values are `WeakReference`-wrapped WikiSessions.

Parameters:
  `sessionId` - the HTTP session
Returns:
  the wiki session

#### remove

    public final void remove​(javax.servlet.http.HttpServletRequest request)

Removes the wiki session associated with the user's HttpRequest from the session cache.

Parameters:
  `request` - the user's HTTP request

#### remove

    public final void remove​(javax.servlet.http.HttpSession session)

Removes the wiki session associated with the user's HttpSession from the session cache.

Parameters:
  `session` - the user's HTTP session

#### sessions

    public final int sessions()

Returns the current number of active wiki sessions.

Returns:
  the number of sessions

#### userPrincipals

    public final java.security.Principal[] userPrincipals()

Returns the current wiki users as a sorted array of Principal objects. The principals are those returned by each WikiSession's `Session.getUserPrincipal()`'s method.

To obtain the list of current WikiSessions, we iterate through our session Map and obtain the list of values, which are WikiSessions wrapped in `WeakReference` objects. Those `WeakReference`s whose `get()` method returns non-`null` values are valid sessions.

Returns:
  the array of user principals

#### addWikiEventListener

    public final void addWikiEventListener​(WikiEventListener listener)

Registers a WikiEventListener with this instance.

Parameters:
  `listener` - the event listener
Since:
  2.4.75

#### removeWikiEventListener

    public final void removeWikiEventListener​(WikiEventListener listener)

Un-registers a WikiEventListener with this instance.

Parameters:
  `listener` - the event listener
Since:
  2.4.75

#### fireEvent

    protected final void fireEvent​(int type,
                                   java.security.Principal principal,
                                   Session session)

Fires a WikiSecurityEvent to all registered listeners.

Parameters:
  `type` - the event type
  `principal` - the user principal associated with this session
  `session` - the wiki session
Since:
  2.4.75

#### sessionCreated

    public void sessionCreated​(javax.servlet.http.HttpSessionEvent se)

Fires when the web container creates a new HTTP session.

Specified by:
  `sessionCreated` in interface `javax.servlet.http.HttpSessionListener`
Parameters:
  `se` - the HTTP session event

#### sessionDestroyed

    public void sessionDestroyed​(javax.servlet.http.HttpSessionEvent se)

Removes the user's WikiSession from the internal session cache when the web container destroys an HTTP session.

Specified by:
  `sessionDestroyed` in interface `javax.servlet.http.HttpSessionListener`
Parameters:
  `se` - the HTTP session event