001: /**
002: * $Id: ContextCache.java,v 1.9 2005/10/18 22:28:22 js143762 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.base;
014:
015: import java.lang.*;
016: import java.util.*;
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019: import java.util.logging.LogRecord;
020:
021: import com.iplanet.sso.*;
022: import com.sun.portal.log.common.PortalLogger;
023:
024: //import com.sun.portal.profile.*;
025:
026: /**
027: * ContextCache - cache to hold Context
028: *
029: * @author John Saare
030: * @version 2.0
031: * @see Context
032: */
033: public class ContextCache {
034:
035: protected static HashMap contextCache = new HashMap(5);
036: protected static final int START_SIZE = 1023;
037: protected HashMap contexts = null;
038:
039: // Create a logger for this class
040: private static Logger debugLogger = PortalLogger
041: .getLogger(ContextCache.class);
042:
043: /**
044: * Private ContextCache constructor; use getInstance()
045: * instead to get a handle to an instance.
046: */
047: public ContextCache() {
048: contexts = new HashMap(START_SIZE);
049: }
050:
051: /**
052: * Return an instance; creating one if needed
053: *
054: * @return the ContextCache instance
055: */
056: public static synchronized ContextCache getInstance(String className) {
057: ContextCache c = (ContextCache) contextCache.get(className);
058:
059: if (c != null) {
060: return c;
061: }
062:
063: c = new ContextCache();
064: contextCache.put(className, c);
065:
066: return c;
067: }
068:
069: /**
070: * Put a context into the cache
071: *
072: * @param context the context
073: */
074: public void put(Context context, String configName) {
075: put(context, configName, true);
076: }
077:
078: /**
079: * Put a context into the cache
080: *
081: * @param context the context
082: * @param listener whether to add a session listener
083: */
084: public synchronized void put(Context context, String configName,
085: boolean listener) {
086: SSOTokenID sid = context.getSessionID();
087: String sidString = sid.toString();
088: if (configName != null) {
089: sidString = sidString + configName;
090: }
091: contexts.put(sidString, context);
092: if (listener) {
093: HashMapReaper reaper = new HashMapReaper(sidString,
094: contexts);
095: try {
096: context.getSession().addSSOTokenListener(reaper);
097: } catch (SSOException e) {
098: if (debugLogger.isLoggable(Level.INFO)) {
099: LogRecord logRecord = new LogRecord(Level.INFO,
100: "PSMA_CSPWTB0016");
101: logRecord.setParameters(new Object[] { sidString });
102: logRecord.setThrown(e);
103: logRecord.setLoggerName(debugLogger.getName());
104: debugLogger.log(logRecord);
105: }
106: }
107:
108: }
109: }
110:
111: /**
112: * Remove a context from the cache
113: *
114: * @param context the context
115: * @param configName the configName used
116: */
117: public synchronized void remove(Context context, String configName) {
118: SSOTokenID sid = context.getSessionID();
119: String sidString = sid.toString();
120: if (configName != null) {
121: sidString = sidString + configName;
122: }
123: Context lcontext = (Context) contexts.get(sidString);
124: contexts.remove(sidString);
125: // no way to remove listeners from ssotokens, so it'll keep on
126: // listening until the session expires. Memory hit? somewhat.
127: // Performance hit? small. Will exception be thrown if there
128: // continues to be new contexts created and stored? maybe.
129: }
130:
131: /**
132: * Get context from the specified session
133: *
134: * @param session the portal session
135: * @return the saved context
136: */
137: public synchronized Context get(SSOToken session, String configName) {
138: if (session == null) {
139: return null;
140: }
141:
142: String sidString = session.getTokenID().toString();
143: if (configName != null)
144: sidString = sidString + configName;
145: Context context = (Context) contexts.get(sidString);
146:
147: return context;
148: }
149:
150: /*
151: * This class is used to purge the caches of per-session entries.
152: */
153: private class HashMapReaper implements SSOTokenListener {
154:
155: private String key;
156: private HashMap hashMap;
157:
158: HashMapReaper(String key, HashMap hashMap) {
159: this .key = key;
160: this .hashMap = hashMap;
161: }
162:
163: /**
164: * Implements SSOTokenListener "ssoTokenChanged" method.
165: */
166: public void ssoTokenChanged(com.iplanet.sso.SSOTokenEvent evt) {
167: try {
168: SSOToken token = evt.getToken();
169: int evtType = evt.getType();
170:
171: if (evtType != evt.SSO_TOKEN_DESTROY
172: && evtType != evt.SSO_TOKEN_IDLE_TIMEOUT
173: && evtType != evt.SSO_TOKEN_MAX_TIMEOUT) {
174: return;
175: }
176:
177: clearCache();
178:
179: } catch (Exception e) {
180: debugLogger.log(Level.INFO, "PSMA_CSPWTB0001", e);
181: }
182: }
183:
184: public void clearCache() {
185: try {
186: Context tempContext = (Context) hashMap.get(key);
187: tempContext.closeConnection();
188: } catch (Exception ex) {
189: }
190: hashMap.remove(key);
191: }
192:
193: }
194: }
|