001: package com.sun.portal.netlet.eproxy;
002:
003: /*
004: * Manages the creation/deletion and modification of the NetletGroups
005: * Ensures that there will be only one NetletGroup Associated with a
006: * single user session at any instance of time
007: *
008: * @ see com.iplanet.sso.SSOToken
009: * @ see com.sun.portal.netlet.eproxy.NetletGroup
010: * @ see com.sun.portal.netlet.econnection.ReaderWriterLock
011: *
012: * @ Created 31-08-2001
013: * @ Author Rajesh T
014: */
015:
016: import java.util.Enumeration;
017: import java.util.Hashtable;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020:
021: import com.iplanet.sso.SSOException;
022: import com.iplanet.sso.SSOToken;
023: import com.iplanet.sso.SSOTokenID;
024: import com.iplanet.sso.SSOTokenManager;
025: import com.sun.portal.log.common.PortalLogger;
026: import com.sun.portal.netlet.econnection.ReaderWriterLock;
027: import com.sun.portal.util.GWThreadPool;
028:
029: public class NetletGroupManager {
030:
031: private static Hashtable userNetlets = new Hashtable();
032:
033: private static NetletGroupManager manager = null;
034:
035: private static SessionKeepAlive ska = null;
036:
037: // private static Logger logger =
038: // Logger.getLogger("com.sun.portal.sra.netlet");
039: private static Logger logger = PortalLogger
040: .getLogger(NetletGroupManager.class);
041:
042: // Singleton Class
043: private NetletGroupManager() {
044: }
045:
046: /*
047: * Provides access to the NetletGroupManager Creates a Manager it there
048: * exists none
049: */
050:
051: public static NetletGroupManager getNetletGroupManager() {
052: if (manager == null)
053: manager = new NetletGroupManager();
054: return manager;
055: }
056:
057: /*
058: * Checks the internal structure if there already exists a NetletGroup for
059: * this session if so reuses it. else creates a new NetletGroup and and adds
060: * to the interanal structure @ see
061: * com.sun.portal.netlet.eproxy.ReaderWriterLock @ see
062: * com.iplanet.sso.SSOToken
063: */
064:
065: public static void registerReaderWriter(ReaderWriterLock r,
066: SSOToken ssoToken) throws SSOException {
067: String ssoTokenID = ssoToken.getTokenID().toString();
068: init(ssoToken);
069: safeAddToUserNetlets(r, ssoToken, ssoTokenID);
070: }
071:
072: /*
073: * Checks the internal structure if there already exists a NetletGroup for
074: * this session if so reuses it. else creates a new NetletGroup and and adds
075: * to the interanal structure
076: * @ see com.sun.portal.netlet.eproxy.ReaderWriterLock @ see
077: * com.iplanet.sso.SSOTokenID
078: */
079:
080: /**
081: * @param r
082: * @param ssoToken
083: * @param ssoTokenID
084: * @throws SSOException
085: */
086: private static void safeAddToUserNetlets(ReaderWriterLock r,
087: SSOToken ssoToken, String ssoTokenID) throws SSOException {
088: synchronized (userNetlets) {
089: NetletGroup ng = (NetletGroup) userNetlets.get(ssoTokenID);
090: if (ng == null) {
091: ng = new NetletGroup(manager, ssoToken);
092: }
093: ng.add(r);
094: manager.userNetlets.put(ssoTokenID, ng);
095:
096: }
097: }
098:
099: /*
100: * Checks the internal structure if there already exists a NetletGroup for
101: * this session if so reuses it. else creates a new NetletGroup and and adds
102: * to the interanal structure @ see
103: * com.sun.portal.netlet.eproxy.ReaderWriterLock @ see
104: * com.iplanet.sso.SSOTokenID
105: */
106:
107: public static void registerReaderWriter(ReaderWriterLock r,
108: SSOTokenID ssoTokenId) throws SSOException {
109: String ssoTokenID = ssoTokenId.toString();
110: SSOTokenManager ssoTokenManager = SSOTokenManager.getInstance();
111: SSOToken ssoToken = ssoTokenManager.createSSOToken(ssoTokenID);
112: init(ssoToken);
113: safeAddToUserNetlets(r, ssoToken, ssoTokenID);
114:
115: }
116:
117: /*
118: * Checks the internal structure if there already exists a NetletGroup for
119: * this session if so reuses it. else creates a new NetletGroup and and adds
120: * to the interanal structure @ see
121: * com.sun.portal.netlet.eproxy.ReaderWriterLock @ see String
122: */
123:
124: public static void registerReaderWriter(ReaderWriterLock r,
125: String ssoTokenID) throws SSOException {
126: SSOTokenManager ssoTokenManager = SSOTokenManager.getInstance();
127: SSOToken ssoToken = ssoTokenManager.createSSOToken(ssoTokenID);
128: init(ssoToken);
129: safeAddToUserNetlets(r, ssoToken, ssoTokenID);
130:
131: }
132:
133: /*
134: * Removes the NetletGroup from the internal structure @ see
135: * com.iplanet.sso.SSOToken
136: */
137: public static void unregister(SSOToken ssoToken) {
138: String ssoTokenID = ssoToken.getTokenID().toString();
139: clean(ssoTokenID);
140: manager.userNetlets.remove(ssoTokenID);
141: }
142:
143: /*
144: * Removes the NetletGroup from the internal structure @ see
145: * com.iplanet.sso.SSOTokenID
146: */
147: public static void unregister(SSOTokenID ssoTokenId) {
148: String ssoTokenID = ssoTokenId.toString();
149: clean(ssoTokenID);
150: manager.userNetlets.remove(ssoTokenID);
151: }
152:
153: /*
154: * Removes the NetletGroup from the internal structure @ see
155: * com.iplanet.sso.SSOTokenID
156: */
157: public static void unregister(String ssoTokenID) {
158: clean(ssoTokenID);
159: manager.userNetlets.remove(ssoTokenID);
160: }
161:
162: /*
163: * Provides proper shutdown of the ReaderWriterLocks associated with the
164: * Session which is present inside the NetletGroup
165: */
166: public static void clean(String ssoTokenID) {
167: NetletGroup ng = (NetletGroup) manager.userNetlets
168: .get(ssoTokenID);
169: ng.clean();
170: }
171:
172: /*
173: * public static synchronized void clean(SSOToken ssoToken) { NetletGroup ng
174: * =(NetletGroup) manager.userNetlets.get(ssoToken.getTokenID().toString());
175: * ng.clean(); }
176: */
177:
178: /*
179: * Returns all the user sessions that are currently managed by the
180: * NetletGroupManager
181: *
182: * @see com.iplanet.sso.SSOToken
183: */
184: public static String[] getRegisteredSessions() {
185:
186: Enumeration enumeration = null;
187: String[] keys = null;
188:
189: synchronized (userNetlets) {
190: enumeration = manager.userNetlets.keys();
191: keys = new String[manager.userNetlets.size()];
192: }
193:
194: int i = 0;
195:
196: while (enumeration.hasMoreElements()) {
197: keys[i] = (String) enumeration.nextElement();
198: i++;
199: }
200: return keys;
201: }
202:
203: /*
204: * public synchronized static SSOToken[] getRegisteredSessions() { public
205: * synchronized static String[] getRegisteredSessions() { Enumeration enum =
206: * manager.userNetlets.keys(); int i = 0; String[] keys = new
207: * String[manager.userNetlets.size()]; while (enum.hasMoreElements()) {
208: * keys[i] = (String) enum.nextElement(); i++; } return keys; }
209: * /* public synchronized static SSOToken[] getRegisteredSessions() {
210: * Enumeration enum = manager.userNetlets.keys(); int i=0; SSOToken[] keys =
211: * new SSOToken[manager.userNetlets.size()]; while (enum.hasMoreElements()){
212: * keys[i] = (SSOToken) enum.nextElement(); i++; } return keys; }
213: */
214:
215: /*
216: * Returns the NetletGroup associated with the user Session @ param
217: * com.iplanet.sso.SSOToken @ see com.sun.portal.netlet.eproxy.NetletGroup
218: */
219:
220: public static NetletGroup getNetletGroup(String ssoTokenID) {
221: return ((NetletGroup) manager.userNetlets.get(ssoTokenID));
222: }
223:
224: /*
225: * public static synchronized NetletGroup getNetletGroup(SSOToken ssoToken) {
226: * return
227: * ((NetletGroup)manager.userNetlets.get(ssoToken.getTokenID().toString())); }
228: */
229:
230: /*
231: * Ensures that there is always a SessionKeepAlive thread to extend the
232: * maxIdleTime for the registered session @ see
233: * com.sun.portal.netlet.eproxy.SessionKeepAlive
234: */
235:
236: private static void init(SSOToken ssoToken) {
237: if (manager.ska == null) {
238: try {
239: manager.ska = new SessionKeepAlive(manager, ssoToken
240: .getMaxIdleTime());
241: GWThreadPool.run(ska);
242: } catch (InterruptedException ie) {
243: // logger.log(Level.SEVERE, "Unable to start Session Keep Alive
244: // Thread from Netlet Group manager", ie);
245: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX047");
246: } catch (SSOException se) {
247: // logger.log(Level.SEVERE, "Unable to start Session Keep Alive
248: // Thread from Netlet Group manager", se);
249: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX048");
250: }
251: }
252: }
253: }
|