001: /**
002: * Copyright 2003 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: */package com.sun.portal.wsrp.consumer.markup.impl;
013:
014: import java.util.Map;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Collections;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020:
021: import com.sun.portal.container.ContainerRequest;
022: import com.sun.portal.wsrp.consumer.markup.MarkupConfig;
023: import com.sun.portal.wsrp.common.stubs.MarkupParams;
024:
025: import com.sun.portal.providers.context.ProviderContext;
026: import com.sun.portal.providers.context.ProviderContextException;
027: import com.sun.portal.wsrp.consumer.markup.CacheEntry;
028: import com.sun.portal.wsrp.consumer.markup.CacheManager;
029: import com.sun.portal.log.common.PortalLogger;
030:
031: public class CacheManagerImpl implements CacheManager {
032:
033: public static final String CACHED_PORTLETS = "wsrp.cacheEntryMap";
034:
035: private static Logger logger = PortalLogger
036: .getLogger(CacheManagerImpl.class);
037:
038: /**
039: * Returns the cached content for a container request.
040: *
041: * @param request
042: */
043:
044: public CacheEntry getCacheEntry(MarkupConfig markupConfig,
045: ContainerRequest request, MarkupParams markupParams) {
046:
047: CacheEntry cacheEntry = null;
048: if (!MarkupUtil.isAuthless(request)) {
049: String entityId = request.getEntityID();
050: Map cachedPortlets = getCachedPortletsMap(request);
051:
052: cacheEntry = (CacheEntry) cachedPortlets.get(entityId);
053:
054: if (cacheEntry != null) {
055: if ((!sameMarkupParams(markupParams, cacheEntry
056: .getMarkupParams()))) {
057: if (logger.isLoggable(Level.FINEST))
058: logger.log(Level.FINEST, "PSWS_CSPWCMI0001",
059: request.getEntityID());
060: cachedPortlets.remove(entityId);
061: cacheEntry = null;
062: } else {
063: if (logger.isLoggable(Level.FINEST))
064: logger.log(Level.FINEST, "PSWS_CSPWCMI0002",
065: request.getEntityID());
066: }
067: } else {
068: if (logger.isLoggable(Level.FINEST))
069: logger.log(Level.FINEST, "PSWS_CSPWCMI0003",
070: request.getEntityID());
071: }
072:
073: }
074:
075: return cacheEntry;
076:
077: }
078:
079: /**
080: *
081: *
082: * @param request
083: * @param cacheControl
084: * @param entry
085: * Anonymous user -- look again for forAll case.
086: */
087: public void putCacheEntry(MarkupConfig markupConfig,
088: ContainerRequest request, CacheEntry cacheEntry) {
089:
090: if (!MarkupUtil.isAuthless(request)) {
091: if (logger.isLoggable(Level.FINEST))
092: logger.log(Level.FINEST, "PSWS_CSPWCMI0004", request
093: .getEntityID());
094:
095: Map cachedPortlets = getCachedPortletsMap(request);
096: cachedPortlets.put(request.getEntityID(), cacheEntry);
097: }
098:
099: }
100:
101: /**
102: * Invalidte the cache
103: *
104: * @param request
105: */
106: public void invalidateCacheEntry(MarkupConfig markupConfig,
107: ContainerRequest request) {
108:
109: if (!MarkupUtil.isAuthless(request)) {
110: Map cachedPortlets = getCachedPortletsMap(request);
111:
112: if (cachedPortlets.containsKey(request.getEntityID())) {
113: if (logger.isLoggable(Level.FINEST))
114: logger.log(Level.FINEST, "PSWS_CSPWCMI0005",
115: request.getEntityID());
116: cachedPortlets.remove(request.getEntityID());
117: }
118: }
119:
120: }
121:
122: /**
123: * convenient method to retrieve the cache Map from the
124: * encapsulated <code>PorviderContext</code> in the <code>
125: * ContainerRequest</code> object.
126: * <p>
127: * @param request The <code>ContainerRequest</code> object that
128: * encapsulates the <code>PorviderContext</code> object
129: * <p>
130: * @return a Map that contains all the cache entries for
131: * the user session
132: */
133: private Map getCachedPortletsMap(ContainerRequest request) {
134:
135: ProviderContext pc = MarkupUtil.getProviderContext(request);
136: // Use DCL mechanism to improve the performance. Although
137: // DCL is known not to be always safe, it should not be a
138: // problem here since the object is retrieved from a
139: // synchronized HashMap in desktop context and will be
140: // instantiated, if not already exists, before being set
141: // in the map.
142:
143: Map cachedPortlets = (Map) pc
144: .getSessionProperty(CACHED_PORTLETS);
145: if (cachedPortlets == null) {
146: synchronized (pc) {
147: if ((cachedPortlets = (Map) pc
148: .getSessionProperty(CACHED_PORTLETS)) == null) {
149: cachedPortlets = Collections
150: .synchronizedMap(new HashMap());
151: pc.setSessionProperty(CACHED_PORTLETS,
152: cachedPortlets);
153: if (logger.isLoggable(Level.FINEST))
154: logger.log(Level.FINEST, "PSWS_CSPWCMI0006");
155: }
156: }
157: }
158: return cachedPortlets;
159: }
160:
161: private boolean sameMarkupParams(MarkupParams first,
162: MarkupParams second) {
163:
164: boolean same = false;
165: if (!strSame(first.getWindowState(), second.getWindowState())) {
166: return false;
167: }
168: if (!strSame(first.getMode(), second.getMode())) {
169: return false;
170: }
171: if (!strSame(first.getNavigationalState(), second
172: .getNavigationalState())) {
173: return false;
174: }
175: if (first.isSecureClientCommunication() != second
176: .isSecureClientCommunication()) {
177:
178: return false;
179: }
180: if (!listSame(first.getValidNewWindowStates(), second
181: .getValidNewWindowStates())) {
182: return false;
183: }
184: if (!listSame(first.getMimeTypes(), second.getMimeTypes())) {
185: return false;
186: }
187: if (!listSame(first.getValidNewModes(), second
188: .getValidNewModes())) {
189: return false;
190: }
191: if (!listSame(first.getLocales(), second.getLocales())) {
192: return false;
193: }
194: if (!listSame(first.getMimeTypes(), second.getMimeTypes())) {
195: return false;
196: }
197: if (!listSame(first.getMarkupCharacterSets(), second
198: .getMarkupCharacterSets())) {
199: return false;
200: }
201: if (!strSame(first.getClientData().getUserAgent(), second
202: .getClientData().getUserAgent())) {
203: return false;
204: }
205:
206: return true;
207: }
208:
209: private static boolean strSame(String a, String b) {
210: if (a == null && b == null) {
211: return true;
212: }
213: if (a != null && a.equals(b)) {
214: return true;
215: }
216: return false;
217: }
218:
219: private static boolean listSame(String[] a, String[] b) {
220:
221: if (a == null && b == null) {
222: return true;
223: }
224:
225: if (a != null && b != null) {
226: if (a.length == b.length) {
227: for (int i = 0; i < a.length; i++) {
228: if (!strSame(a[i], b[i])) {
229: // element not same
230: return false;
231: }
232: }
233: // all elements same
234: return true;
235: } else {
236: // length is not same
237: return false;
238: }
239: } else {
240: // only one of them is null
241: return false;
242: }
243: }
244:
245: }
|