001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.console.base;
009:
010: //base classes
011: import java.util.HashMap;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpSession;
014:
015: //project specific classes
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.platforms.stores.base.SystemStoreSession;
018: import org.jfolder.platforms.stores.base.SystemStoreSessionManager;
019: import org.jfolder.security.model.UserHolder;
020: import org.jfolder.security.model.UserIdentity;
021:
022: //other classes
023:
024: public class ConsolePageSessionFactory {
025:
026: private final static HashMap userHolders = new HashMap();
027: private final static HashMap userCounts = new HashMap();
028:
029: //private final static String JFOLDER_CONSOLE_SESSION =
030: // "JFOLDER_CONSOLE_SESSION";
031:
032: private static int totalSessions = 0;
033:
034: public final static ConsolePageSession getConsoleSession(
035: HttpServletRequest inRequest, UserHolder inUser) {
036:
037: ConsolePageSession outValue = null;
038:
039: UserIdentity userId = inUser.getUserIdentity();
040:
041: HttpSession hs = inRequest.getSession(true);
042:
043: String handle = userId.getSecurityType() + "-"
044: + userId.getName();
045:
046: SystemStoreSession requestSss = SystemStoreSessionManager
047: .getSystemStoreSession(inRequest);
048:
049: if (requestSss.isConsoleTemplateSessionPresent()) {
050: Object o = requestSss.getConsoleTemplateSession();
051: outValue = (ConsolePageSession) o;
052: } else {
053: outValue = createConsoleSession(inRequest, handle, inUser);
054: }
055:
056: return outValue;
057: }
058:
059: private synchronized final static ConsolePageSession createConsoleSession(
060: HttpServletRequest inRequest, String inHandle,
061: UserHolder inUser) {
062:
063: ConsolePageSession outValue = null;
064:
065: HttpSession hs = inRequest.getSession(true);
066:
067: SystemStoreSession requestSss = SystemStoreSessionManager
068: .getSystemStoreSession(inRequest);
069:
070: if (userHolders.containsKey(inHandle)
071: && userCounts.containsKey(inHandle)) {
072: outValue = (ConsolePageSession) userHolders.get(inHandle);
073: Integer count = (Integer) userCounts.get(inHandle);
074: userCounts.put(inHandle, new Integer(count.intValue() + 1));
075: } else {
076: outValue = new ConsolePageSession(inHandle, inUser);
077: userHolders.put(inHandle, outValue);
078: userCounts.put(inHandle, new Integer(1));
079: ConsolePageSessionFactory.totalSessions++;
080: }
081:
082: requestSss.setConsoleTemplateSession(outValue);
083:
084: return outValue;
085: }
086:
087: protected synchronized final static void removeConsoleSession(
088: String inHandle) {
089:
090: int count = ((Integer) userCounts.get(inHandle)).intValue();
091:
092: if (count > 1) {
093: userCounts.put(inHandle, new Integer(count - 1));
094: } else if (count == 1) {
095: userHolders.remove(inHandle);
096: userCounts.remove(inHandle);
097: ConsolePageSessionFactory.totalSessions--;
098: } else {
099: throw UnexpectedSystemException.unknownState();
100: }
101: }
102:
103: public synchronized final static int getTotalSessionsCount() {
104: return ConsolePageSessionFactory.totalSessions;
105: }
106: }
|