1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.jetspeed.portal.portlets;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Set;
28
29 import org.apache.velocity.context.Context;
30
31 /***
32 *
33 * Context that holds all the data you want to transfer to your view. You
34 * populate this during your action handling.
35 * <p>
36 * This context also supports chaining of additional contexts. The initial
37 * context created by this object always has priority over an addtional
38 * chained objects. In short matching keys within the initial context
39 * will "hide" matching keys within the addtional context(s).
40 * </p>
41 *
42 * @author tkuebler
43 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
44 * @version $Id: GenericMVCContext.java,v 1.3 2003/02/05 00:32:13 tkuebler Exp $
45 * @stereotype thing
46 *
47 */
48
49
50
51
52
53
54
55
56 public class GenericMVCContext implements Context
57 {
58
59 private HashMap data;
60 private HashSet additionalContexts;
61
62 /*** Creates a new instance of GenericMVCContext */
63 public GenericMVCContext()
64 {
65 data = new HashMap();
66 additionalContexts = new HashSet();
67
68 }
69
70 /***
71 * Adds an existing Collection of contexts into this one.
72 * Externally added contexts are maintained indvidually
73 * and are not merged into the existing context.
74 * Done to facilitate the context chanining.
75 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
76 */
77 public GenericMVCContext(Collection contexts)
78 {
79 this();
80 additionalContexts.addAll(contexts);
81 }
82
83 /***
84 * Adds an existing context into this one.
85 * Externally added contexts are maintained indvidually
86 * and are not merged into the existing context
87 * Done to facilitate the context chanining.
88 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
89 */
90 public GenericMVCContext(Context context)
91 {
92 this();
93 additionalContexts.add(context);
94 }
95
96
97 public boolean containsKey(java.lang.Object key)
98 {
99 boolean found = data.containsKey(key);
100 if (!found)
101 {
102 Iterator itr = additionalContexts.iterator();
103 while (itr.hasNext() && !found)
104 {
105 found = ((Context) itr.next()).containsKey(key);
106 }
107 }
108
109 return found;
110 }
111
112 public Object get(java.lang.String key)
113 {
114 Object value = data.get(key);
115
116
117 if (value == null)
118 {
119 Iterator itr = additionalContexts.iterator();
120 while (itr.hasNext() && value == null)
121 {
122 value = ((Context) itr.next()).get(key);
123 }
124 }
125
126 return value;
127 }
128
129 public Object[] getKeys()
130 {
131 Set keySet = data.keySet();
132
133 Iterator itr = additionalContexts.iterator();
134
135 while (itr.hasNext())
136 {
137 Object[] keys = ((Context) itr.next()).getKeys();
138 for (int i = 0; i < keys.length; i++)
139 {
140 keySet.add(keys[i]);
141 }
142 }
143
144
145 return data.keySet().toArray();
146 }
147
148 public Object put(java.lang.String key, java.lang.Object value)
149 {
150
151 return data.put(key, value);
152 }
153
154 public Object remove(java.lang.Object key)
155 {
156 Object obj = data.remove(key);
157 if (obj == null)
158 {
159 Iterator itr = additionalContexts.iterator();
160 while (itr.hasNext() && obj == null)
161 {
162 obj = ((Context) itr.next()).remove(key);
163 }
164 }
165
166 return obj;
167 }
168
169 /***
170 * Add an additional context to this one
171 * @param Context context Additional Context object to add.
172 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
173 */
174 public void addContext(Context context)
175 {
176 additionalContexts.add(context);
177 }
178
179 /***
180 * This Collection is "live" as it is the same Collection
181 * that maintains this Context's chained contexts. This
182 * Collection DOES NOT include objects maintained in
183 * the initial context.
184 * @return a Collection all the chained contexts
185 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>-
186 */
187 public Collection getChainedContexts()
188 {
189 return additionalContexts;
190 }
191
192 }