001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.presentation.portlet.session;
051:
052: import java.util.HashMap;
053: import java.util.Iterator;
054: import org.apache.log4j.Logger;
055: import org.jaffa.config.Config;
056: import java.util.MissingResourceException;
057:
058: /** This class holds information about all the UserSession objects in use currently. This may be used as an administration tool.
059: */
060: public class SessionManager {
061:
062: private static Logger log = Logger.getLogger(SessionManager.class);
063: private static IdleComponentGarbageCollector m_idleComponentGarbageCollector = null;
064: private static HashMap m_users = new HashMap();
065: private static long m_sessionId = 0;
066:
067: /** Adds a UserSession. It assigns a unique id to the UserSession before adding it to the internal cache.
068: * @param s The UserSession.
069: */
070: public static void addSession(UserSession s) {
071: s.setSessionId(getNextId());
072: m_users.put(s.getSessionId(), s);
073: }
074:
075: /** Removes a UserSession.
076: * @param s The UserSession.
077: */
078: public static void removeSession(UserSession s) {
079: m_users.remove(s.getSessionId());
080: }
081:
082: /** Returns an array of all the UserSession objects in use.
083: * @return an array of all the UserSession objects in use.
084: */
085: public static UserSession[] getSessions() {
086: return (UserSession[]) m_users.values().toArray(
087: new UserSession[0]);
088: }
089:
090: /** Returns the UserSession object for a sessionId.
091: * @param sessionId The id identifying a UserSession.
092: * @return the UserSession object for a sessionId.
093: */
094: public static UserSession getSession(String sessionId) {
095: return (UserSession) m_users.get(sessionId);
096:
097: }
098:
099: private static synchronized String getNextId() {
100: return Long.toString(++m_sessionId);
101: }
102:
103: /** This will perform garbage collection of idle components. Idle components are those components which have had no activity performed in the last 'timeOutMinutes' minutes.
104: * This will start a Thread which runs every 'timeOutMinutes' minutes
105: */
106: public static void startGarbageCollectionOfIdleComponents() {
107: if (m_idleComponentGarbageCollector == null) {
108: int frequency = getComponentGarbageCollectionFrequency();
109: int timeOut = getComponentGarbageCollectionTimeOut();
110: if (frequency <= 0 || timeOut <= 0) {
111: if (log.isInfoEnabled())
112: log
113: .info("Component garbage collection not started. Values for Component garbage collection Frequency and TimeOut should be positive numbers.");
114: } else {
115: Thread m_idleComponentGarbageCollector = new IdleComponentGarbageCollector(
116: frequency, timeOut);
117: m_idleComponentGarbageCollector.start();
118: }
119: }
120: }
121:
122: /** This stops the Thread that was started by the startGarbageCollectionOfIdleComponents() method.
123: */
124: public static void stopGarbageCollectionOfIdleComponents() {
125: if (m_idleComponentGarbageCollector != null) {
126: m_idleComponentGarbageCollector.destroy();
127: m_idleComponentGarbageCollector = null;
128: }
129: }
130:
131: private static int getComponentGarbageCollectionFrequency() {
132: int frequency = 0;
133: String prop = null;
134: try {
135: prop = (String) Config
136: .getProperty(Config.PROP_PRESENTATION_COMPONENT_GARBAGE_COLLECTION_FREQUENCY_IN_MINUTES);
137: } catch (MissingResourceException e) {
138: // do nothing
139: }
140: if (prop != null && prop.trim().length() > 0)
141: frequency = Integer.parseInt(prop);
142: return frequency;
143: }
144:
145: private static int getComponentGarbageCollectionTimeOut() {
146: int timeOut = 0;
147: String prop = null;
148: try {
149: prop = (String) Config
150: .getProperty(Config.PROP_PRESENTATION_COMPONENT_GARBAGE_COLLECTION_TIME_OUT_IN_MINUTES);
151: } catch (MissingResourceException e) {
152: // do nothing
153: }
154: if (prop != null && prop.trim().length() > 0)
155: timeOut = Integer.parseInt(prop);
156: return timeOut;
157: }
158:
159: /** This thread will be run as a Daemon. */
160: private static class IdleComponentGarbageCollector extends Thread {
161:
162: private int m_frequency;
163: private int m_timeOut;
164:
165: private IdleComponentGarbageCollector(int frequency, int timeOut) {
166: m_frequency = frequency;
167: m_timeOut = timeOut;
168: setDaemon(true);
169: }
170:
171: /** This will perform garbage collection of idle components every 'timeOutMinutes' minutes.
172: */
173: public void run() {
174: while (true) {
175: try {
176: sleep(m_frequency * 60 * 1000);
177: } catch (InterruptedException e) {
178: // do nothing
179: }
180:
181: try {
182: if (log.isDebugEnabled())
183: log
184: .debug("Starting the garbage collection of idle components");
185:
186: for (Iterator i = m_users.values().iterator(); i
187: .hasNext();) {
188: UserSession us = (UserSession) i.next();
189: if (log.isDebugEnabled())
190: log
191: .debug("Starting garbage collection of idle components for the user "
192: + us.getUserId()
193: + " having the sessionid "
194: + us.getSessionId());
195: us.garbageCollectIdleComponents(m_timeOut);
196: }
197:
198: if (log.isDebugEnabled())
199: log
200: .debug("Finished the garbage collection of idle components");
201: } catch (Exception e) {
202: // just log an info message and continue
203: if (log.isInfoEnabled())
204: log
205: .info(
206: "Exception thrown during garbage collection of idle components",
207: e);
208: }
209: }
210: }
211: }
212:
213: }
|