001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.taskadmin.context;
014:
015: import java.util.Collections;
016: import java.util.Collection;
017: import java.util.List;
018: import java.util.Arrays;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.HashMap;
023: import java.util.Set;
024: import java.util.HashSet;
025: import java.util.TreeSet;
026:
027: import java.util.Locale;
028: import java.util.ResourceBundle;
029: import java.util.logging.Logger;
030: import java.util.logging.Level;
031: import java.util.logging.LogRecord;
032:
033: import com.sun.portal.log.common.PortalLogger;
034:
035: /**
036: * This class manages the DPRoot cache.
037: */
038: public class DPRootCacheManager {
039: private static Logger logger = PortalLogger
040: .getLogger(DPRootCacheManager.class);
041:
042: //dproot cache
043: private static Map dpRootsPerPortal = Collections
044: .synchronizedMap(new HashMap());
045: private static Map dnSessionIDPerPortal = new HashMap();
046:
047: public static void updateDPRootCache(String portalId, Map dpRoots) {
048: dpRootsPerPortal.put(portalId, dpRoots);
049: }
050:
051: public static Map getDPRootMapFromCache(String portalId) {
052: Map dpRoots = null;
053: if (dpRootsPerPortal.containsKey(portalId)) {
054: dpRoots = (Map) dpRootsPerPortal.get(portalId);
055: } else {
056: dpRoots = Collections.synchronizedMap(new HashMap());
057: dpRootsPerPortal.put(portalId, dpRoots);
058: }
059: return dpRoots;
060: }
061:
062: public static void markDNAsRecentlyAccessed(String portalId,
063: Set DNs, String sid) {
064: Map dnMap = null;
065: Set sids = null;
066: if (dnSessionIDPerPortal.containsKey(portalId)) {
067: dnMap = (Map) dnSessionIDPerPortal.get(portalId);
068: } else {
069: dnMap = new HashMap();
070: }
071: for (Iterator i = DNs.iterator(); i.hasNext();) {
072: String dn = (String) i.next();
073: if (dnMap.containsKey(dn)) {
074: sids = (Set) dnMap.get(dn);
075: } else {
076: sids = new HashSet();
077: }
078: sids.add(sid);
079: dnMap.put(dn, sids);
080: }
081: dnSessionIDPerPortal.put(portalId, dnMap);
082: logger.log(Level.FINEST, "DNSessionIDMap: "
083: + dnSessionIDPerPortal);
084: }
085:
086: public static synchronized void clearCache(String sid) {
087: //go through each DN and remove the current sid
088: //if there are no more sids attached to this dn
089: //remove the dn from dproot cache
090: Set keys = dnSessionIDPerPortal.keySet();
091: for (Iterator i = keys.iterator(); i.hasNext();) {
092: String portalId = (String) i.next();
093: Map dnMap = (Map) dnSessionIDPerPortal.get(portalId);
094: Set dns = dnMap.keySet();
095: for (Iterator j = dns.iterator(); j.hasNext();) {
096: String dn = (String) j.next();
097: Set sids = (Set) dnMap.get(dn);
098: sids.remove(sid);
099: if (sids.isEmpty()) {
100: j.remove();
101: if (dnMap.isEmpty()) {
102: i.remove();
103: }
104: //also remove the dproot
105: Map dpRoots = (Map) dpRootsPerPortal.get(portalId);
106: dpRoots.remove(dn);
107: if (dpRoots.isEmpty()) {
108: dpRootsPerPortal.remove(portalId);
109: } else {
110: dpRootsPerPortal.put(portalId, dpRoots);
111: }
112: }
113:
114: }
115: }
116: logger.log(Level.FINEST, "DPRootCache after clear: "
117: + dpRootsPerPortal);
118: }
119:
120: }
|