Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.catalina.session;
 
 
 import java.util.Date;
 import java.util.Map;
 
 
Minimal implementation of the Manager interface that supports no session persistence or distributable capabilities. This class may be subclassed to create more sophisticated Manager implementations.

Author(s):
Craig R. McClanahan
Version:
$Revision: 1856 $ $Date: 2011-10-27 18:25:57 +0200 (Thu, 27 Oct 2011) $
 
 
 public abstract class ManagerBase implements ManagerMBeanRegistration {
 
     private static final char[] SESSION_ID_ALPHABET = 
         System.getProperty("org.apache.catalina.session.ManagerBase.SESSION_ID_ALPHABET"
                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_").toCharArray();
  
      // ----------------------------------------------------- Instance Variables
 
    
The Container with which this Manager is associated.
 
     protected Container container;


    
The distributable flag for Sessions created by this Manager. If this flag is set to true, any user attributes added to a session controlled by this Manager must be Serializable.
 
     protected boolean distributable;


    
The descriptive information string for this implementation.
 
     private static final String info = "ManagerBase/2.0";


    
The default maximum inactive interval for Sessions created by this Manager.
 
     protected int maxInactiveInterval = 30 * 60;


    
The session id length of Sessions created by this Manager.
 
     protected int sessionIdLength = 18;


    
The descriptive name of this Manager implementation (for logging).
 
    protected static String name = "ManagerBase";


    
The longest time (in seconds) that an expired session had been alive.
    protected int sessionMaxAliveTime;


    
Average time (in seconds) that expired sessions had been alive.
    protected int sessionAverageAliveTime;


    
Number of sessions that have expired.
    protected int expiredSessions = 0;


    
The set of currently active Sessions for this Manager, keyed by session identifier.
    protected Map<StringSessionsessions = new ConcurrentHashMap<StringSession>();
    // Number of sessions created by this manager
    protected int sessionCounter=0;
    protected int maxActive=0;
    // number of duplicated session ids - anything >0 means we have problems
    protected int duplicates=0;
    protected boolean initialized=false;
    
    
Processing time during session expiration.
    protected long processingTime = 0;

    
Iteration count for background processing.
    private int count = 0;


    
Frequency of the session expiration, and related manager operations. Manager operations will be done once for the specified amount of backgrondProcess calls (ie, the lower the amount, the most often the checks will occur).
    protected int processExpiresFrequency = 6;

    
The property change support for this component.
    protected PropertyChangeSupport support = new PropertyChangeSupport(this);
    
    
    // ------------------------------------------------------------- Properties

    
Return the Container with which this Manager is associated.
    public Container getContainer() {
        return (this.);
    }


    
Set the Container with which this Manager is associated.

Parameters:
container The newly associated Container
    public void setContainer(Container container) {
        Container oldContainer = this.;
        this. = container;
        .firePropertyChange("container"oldContainerthis.);
    }


    
Returns the name of the implementation class.
    public String getClassName() {
        return this.getClass().getName();
    }


    
Return the distributable flag for the sessions supported by this Manager.
    public boolean getDistributable() {
        return (this.);
    }


    
Set the distributable flag for the sessions supported by this Manager. If this flag is set, all user data objects added to sessions associated with this manager must implement Serializable.

Parameters:
distributable The new distributable flag
    public void setDistributable(boolean distributable) {
        boolean oldDistributable = this.;
        this. = distributable;
        .firePropertyChange("distributable",
                                   new Boolean(oldDistributable),
                                   new Boolean(this.));
    }


    
Return descriptive information about this Manager implementation and the corresponding version number, in the format <description>/<version>.
    public String getInfo() {
        return ();
    }


    
Return the default maximum inactive interval (in seconds) for Sessions created by this Manager.
    public int getMaxInactiveInterval() {
        return (this.);
    }


    
Set the default maximum inactive interval (in seconds) for Sessions created by this Manager.

Parameters:
interval The new default value
    public void setMaxInactiveInterval(int interval) {
        int oldMaxInactiveInterval = this.;
        this. = interval;
        .firePropertyChange("maxInactiveInterval",
                                   new Integer(oldMaxInactiveInterval),
                                   new Integer(this.));
    }


    
Gets the session id length (in bytes) of Sessions created by this Manager.

Returns:
The session id length
    public int getSessionIdLength() {
        return (this.);
    }


    
Sets the session id length (in bytes) for Sessions created by this Manager.

Parameters:
idLength The session id length
    public void setSessionIdLength(int idLength) {
        int oldSessionIdLength = this.;
        this. = idLength;
        .firePropertyChange("sessionIdLength",
                                   new Integer(oldSessionIdLength),
                                   new Integer(this.));
    }


    
Return the descriptive short name of this Manager implementation.
    public String getName() {
        return ();
    }

    
Gets the number of sessions that have expired.

Returns:
Number of sessions that have expired
    public int getExpiredSessions() {
        return ;
    }


    
Sets the number of sessions that have expired.

Parameters:
expiredSessions Number of sessions that have expired
    public void setExpiredSessions(int expiredSessions) {
        this. = expiredSessions;
    }
    public long getProcessingTime() {
        return ;
    }
    public void setProcessingTime(long processingTime) {
        this. = processingTime;
    }
    
    
Return the frequency of manager checks.
    public int getProcessExpiresFrequency() {
        return (this.);
    }

    
Set the manager checks frequency.

Parameters:
processExpiresFrequency the new manager checks frequency
    public void setProcessExpiresFrequency(int processExpiresFrequency) {
        if (processExpiresFrequency <= 0) {
            return;
        }
        int oldProcessExpiresFrequency = this.;
        this. = processExpiresFrequency;
        .firePropertyChange("processExpiresFrequency",
                                   new Integer(oldProcessExpiresFrequency),
                                   new Integer(this.));
    }
    // --------------------------------------------------------- Public Methods


    
Implements the Manager interface, direct call to processExpires
    public void backgroundProcess() {
         = ( + 1) % ;
        if ( == 0)
            processExpires();
    }

    
Invalidate all sessions that have expired.
    public void processExpires() {
        long timeNow = System.currentTimeMillis();
        Session sessions[] = findSessions();
        int expireHere = 0 ;
        
            ..debug("Start expire sessions " + getName() + " at " + timeNow + " sessioncount " + sessions.length);
        for (int i = 0; i < sessions.lengthi++) {
            if (sessions[i] != null && !sessions[i].isValid()) {
                expireHere++;
            }
        }
        long timeEnd = System.currentTimeMillis();
            ..debug("End expire sessions " + getName() + " processingTime " + (timeEnd - timeNow) + " expired sessions: " + expireHere);
         += ( timeEnd - timeNow );
    }
    public void destroy() {
        if (.....) {
            if != null )
                Registry.getRegistry(nullnull).unregisterComponent();
        }
        =false;
         = null;
    }
    
    public void init() {
        if ) return;
        =true;        
        
        StandardContext ctx=(StandardContext)this.getContainer();
         = ctx.getDistributable();
        if (.....) {
            if==null ) {
                try {
                    Engine eng=(Engine)ctx.getParent().getParent();
                    =ctx.getEngineName();
                    StandardHost hst=(StandardHost)ctx.getParent();
                    String path = ctx.getPath();
                    if (path.equals("")) {
                        path = "/";
                    }   
                    =new ObjectName( + ":type=Manager,path="
                            + path + ",host=" + hst.getName());
                    Registry.getRegistry(nullnull).registerComponent(thisnull );
                } catch (Exception e) {
                    ..failedSessionManagerJmxRegistration(e);
                }
            }
        }
        
            ..debug("Registering " +  );
               
    }

    
Add this Session to the set of active Sessions for this Manager.

Parameters:
session Session to be added
    public void add(Session session) {
        .put(session.getIdInternal(), session);
        int size = .size();
        ifsize >  ) {
             = size;
        }
    }


    
Add a property change listener to this component.

Parameters:
listener The listener to add
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        .addPropertyChangeListener(listener);
    }


    
Change the session ID of the current session to a new randomly generated session ID.

Parameters:
session The session to change the session ID for
    public void changeSessionId(Session sessionRandom random) {
        session.setId(generateSessionId(random));
    }


    
Construct and return a new session object, based on the default settings specified by this Manager's properties. The session id specified will be used as the session id. If a new session cannot be created for any reason, return null.

Parameters:
sessionId The session id which should be used to create the new session; if null, a new session id will be generated
Throws:
java.lang.IllegalStateException if a new session cannot be instantiated for any reason
    public Session createSession(String sessionIdRandom random) {
        
        // Recycle or create a Session instance
        Session session = createEmptySession();
        // Initialize the properties of the new session and return it
        session.setNew(true);
        session.setValid(true);
        session.setCreationTime(System.currentTimeMillis());
        session.setMaxInactiveInterval(this.);
        if (sessionId == null) {
            sessionId = generateSessionId(random);
        }
        session.setId(sessionId);
        ++;
        return (session);
    }
    
    
    
Get a session from the recycled ones or create a new empty one. The PersistentManager manager does not need to create session data because it reads it from the Store.
    public Session createEmptySession() {
        return (getNewSession());
    }


    
Return the active Session, associated with this Manager, with the specified session id (if any); otherwise return null.

Parameters:
id The session id for the session to be returned
Throws:
java.lang.IllegalStateException if a new session cannot be instantiated for any reason
java.io.IOException if an input/output error occurs while processing this request
    public Session findSession(String idthrows IOException {
        if (id == null)
            return (null);
        return (Session.get(id);
    }


    
Return the set of active Sessions associated with this Manager. If this Manager has no active Sessions, a zero-length array is returned.
    public Session[] findSessions() {
        return .values().toArray(new Session[0]);
    }


    
Remove this Session from the active Sessions for this Manager.

Parameters:
session Session to be removed
    public void remove(Session session) {
        .remove(session.getIdInternal());
    }


    
Remove a property change listener from this component.

Parameters:
listener The listener to remove
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        .removePropertyChangeListener(listener);
    }
    // ------------------------------------------------------ Protected Methods


    
Get new session class to be used in the doLoad() method.
    protected StandardSession getNewSession() {
        return new StandardSession(this);
    }


    
Generate and return a new session identifier.
    protected String generateSessionId(Random random) {
        byte[] bytes = new byte[];
        random.nextBytes(bytes);
        // Encode the result
        char[] id = encode(bytes);
        String jvmRoute = getJvmRoute();
        if (appendJVMRoute() && jvmRoute != null) {
            StringBuilder buffer = new StringBuilder();
            buffer.append(id).append('.').append(jvmRoute);
            return buffer.toString();
        } else {
            return String.valueOf(id);
        }
    }
    protected boolean appendJVMRoute() {
        return true;
    }
    
    
Encode the bytes into a String with a slightly modified Base64-algorithm This code was written by Kevin Kelley <kelley@ruralnet.net> and adapted by Thomas Peuss <jboss@peuss.de>

Parameters:
data The bytes you want to encode
Returns:
the encoded String
    public static char[] encode(byte[] data) {
       char[] out = new char[((data.length + 2) / 3) * 4];
       char[] alphabet = ;
       //
       // 3 bytes encode to 4 chars.  Output is always an even
       // multiple of 4 characters.
       //
       for (int i = 0, index = 0; i < data.lengthi += 3, index += 4) {
          boolean quad = false;
          boolean trip = false;
          int val = (0xFF & (intdata[i]);
          val <<= 8;
          if ((i + 1) < data.length) {
             val |= (0xFF & (intdata[i + 1]);
             trip = true;
          }
          val <<= 8;
          if ((i + 2) < data.length) {
             val |= (0xFF & (intdata[i + 2]);
             quad = true;
          }
          out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
          val >>= 6;
          out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
          val >>= 6;
          out[index + 1] = alphabet[val & 0x3F];
          val >>= 6;
          out[index + 0] = alphabet[val & 0x3F];
       }
       return out;
    }
    
    // ------------------------------------------------------ Protected Methods


    
Retrieve the enclosing Engine for this Manager.

Returns:
an Engine object (or null).
    public Engine getEngine() {
        Engine e = null;
        for (Container c = getContainer(); e == null && c != null ; c = c.getParent()) {
            if (c != null && c instanceof Engine) {
                e = (Engine)c;
            }
        }
        return e;
    }


    
Retrieve the JvmRoute for the enclosing Engine.

Returns:
the JvmRoute or null.
    public String getJvmRoute() {
        Engine e = getEngine();
        return e == null ? null : e.getJvmRoute();
    }
    // -------------------------------------------------------- Package Methods
    public void setSessionCounter(int sessionCounter) {
        this. = sessionCounter;
    }


    
Total sessions created by this manager.

Returns:
sessions created
    public int getSessionCounter() {
        return ;
    }


    
Number of duplicated session IDs generated by the random source. Anything bigger than 0 means problems.

Returns:
The count of duplicates
    public int getDuplicates() {
        return ;
    }
    public void setDuplicates(int duplicates) {
        this. = duplicates;
    }


    
Returns the number of active sessions

Returns:
number of sessions active
    public int getActiveSessions() {
        return .size();
    }


    
Max number of concurrent active sessions

Returns:
The highest number of concurrent active sessions
    public int getMaxActive() {
        return ;
    }
    public void setMaxActive(int maxActive) {
        this. = maxActive;
    }


    
Gets the longest time (in seconds) that an expired session had been alive.

Returns:
Longest time (in seconds) that an expired session had been alive.
    public int getSessionMaxAliveTime() {
        return ;
    }


    
Sets the longest time (in seconds) that an expired session had been alive.

Parameters:
sessionMaxAliveTime Longest time (in seconds) that an expired session had been alive.
    public void setSessionMaxAliveTime(int sessionMaxAliveTime) {
        this. = sessionMaxAliveTime;
    }


    
Gets the average time (in seconds) that expired sessions had been alive.

Returns:
Average time (in seconds) that expired sessions had been alive.
    public int getSessionAverageAliveTime() {
        return ;
    }


    
Sets the average time (in seconds) that expired sessions had been alive.

Parameters:
sessionAverageAliveTime Average time (in seconds) that expired sessions had been alive.
    public void setSessionAverageAliveTime(int sessionAverageAliveTime) {
        this. = sessionAverageAliveTime;
    }


    
For debugging: return a list of all session ids currently active
    public String listSessionIds() {
        StringBuilder sb=new StringBuilder();
        Iterator keys = .keySet().iterator();
        while (keys.hasNext()) {
            sb.append(keys.next()).append(" ");
        }
        return sb.toString();
    }


    
For debugging: get a session attribute

Parameters:
sessionId
key
Returns:
The attribute value, if found, null otherwise
    public String getSessionAttributeString sessionIdString key ) {
        Session s = (Session.get(sessionId);
        ifs==null ) {
            return null;
        }
        Object o=s.getSession().getAttribute(key);
        ifo==null ) return null;
        return o.toString();
    }


    
Returns information about the session with the given session id.

The session information is organized as a HashMap, mapping session attribute names to the String representation of their values.

Parameters:
sessionId Session id
Returns:
HashMap mapping session attribute names to the String representation of their values, or null if no session with the specified id exists, or if the session does not have any attributes
    public HashMap getSession(String sessionId) {
        Session s = (Session.get(sessionId);
        if (s == null) {
            return null;
        }
        Enumeration ee = s.getSession().getAttributeNames();
        if (ee == null || !ee.hasMoreElements()) {
            return null;
        }
        HashMap map = new HashMap();
        while (ee.hasMoreElements()) {
            String attrName = (Stringee.nextElement();
            map.put(attrNamegetSessionAttribute(sessionIdattrName));
        }
        return map;
    }
    public void expireSessionString sessionId ) {
        Session s=(Session).get(sessionId);
        ifs==null ) {
            return;
        }
        s.expire();
    }
    public String getLastAccessedTimeString sessionId ) {
        Session s=(Session).get(sessionId);
        ifs==null ) {
            return "";
        }
        return new Date(s.getLastAccessedTime()).toString();
    }
    public String getCreationTimeString sessionId ) {
        Session s=(Session).get(sessionId);
        ifs==null ) {
            return "";
        }
        return new Date(s.getCreationTime()).toString();
    }
    // -------------------- JMX and Registration  --------------------
    protected String domain;
    protected ObjectName oname;
    protected MBeanServer mserver;
    public ObjectName getObjectName() {
        return ;
    }
    public String getDomain() {
        return ;
    }
    public ObjectName preRegister(MBeanServer server,
                                  ObjectName namethrows Exception {
        =name;
        =server;
        =name.getDomain();
        return name;
    }
    public void postRegister(Boolean registrationDone) {
    }
    public void preDeregister() throws Exception {
    }
    public void postDeregister() {
    }
New to GrepCode? Check out our FAQ X