01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.desktop;
06:
07: import java.util.Collections;
08: import javax.servlet.http.HttpServletRequest;
09:
10: import java.util.Map;
11: import java.util.HashMap;
12:
13: class MapWrapper {
14: private Map map = null;
15:
16: MapWrapper(Map map) {
17: this .map = map;
18: }
19:
20: Map getMap() {
21: return map;
22: }
23: }
24:
25: /*
26: * this class adds a thin wrapper around the http request
27: * object to implement an per-request
28: * object cache (request object
29: * cache). there are many values in the
30: * system that we can store for at least one request
31: * to avoid duplicating expensive calls. storing
32: * values for longer than a request is not possible
33: * because we do not have a notification system
34: * to determine when they have changed.
35: */
36:
37: public class ROC {
38: public static final String ROC = "desktop.roc";
39:
40: public static boolean containsObject(Object key) {
41: HttpServletRequest req = RequestThreadLocalizer.getRequest();
42: if (req == null) {
43: return false;
44: }
45:
46: Map roc = getROC(req);
47: if (roc == null) {
48: return false;
49: }
50:
51: return roc.containsKey(key);
52: }
53:
54: public static Object getObject(Object key) {
55: Object o = null;
56: HttpServletRequest req = RequestThreadLocalizer.getRequest();
57: if (req != null) {
58: Map roc = getROC(req);
59: if (roc != null) {
60: o = roc.get(key);
61: }
62: }
63:
64: return o;
65: }
66:
67: public static void setObject(Object key, Object val) {
68: //
69: // since request attrs are backed by a hashtable,
70: // avoid null keys and putting null values
71: //
72: HttpServletRequest req = RequestThreadLocalizer.getRequest();
73: if (req != null) {
74: Map roc = getROC(req);
75: if (roc == null) {
76: roc = Collections.synchronizedMap(new HashMap());
77: setROC(req, roc);
78: }
79: roc.put(key, val);
80: }
81: }
82:
83: private static Map getROC(HttpServletRequest req) {
84: Map map = null;
85: MapWrapper mw = (MapWrapper) req.getAttribute(ROC);
86: if (mw != null) {
87: map = mw.getMap();
88: }
89: return map;
90: }
91:
92: private static void setROC(HttpServletRequest req, Map roc) {
93: req.setAttribute(ROC, new MapWrapper(roc));
94: }
95: }
|