1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16/*17 * GenericMVCContext.java18 *19 * Created on January 27, 2003, 8:47 PM20 */21packageorg.apache.jetspeed.portal.portlets;
2223import java.util.Collection;
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Iterator;
27import java.util.Set;
2829import org.apache.velocity.context.Context;
3031/***32 *33 * Context that holds all the data you want to transfer to your view. You34 * populate this during your action handling.35 * <p>36 * This context also supports chaining of additional contexts. The initial37 * context created by this object always has priority over an addtional 38 * chained objects. In short matching keys within the initial context39 * will "hide" matching keys within the addtional context(s).40 * </p>41 * 42 * @author tkuebler43 * @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 thing46 * 47 */4849/*50 * Note:51 *52 * create the generic context interface later53 * just use Velocity's for now54 *55 */56publicclassGenericMVCContext implements Context
57 {
5859private HashMap data;
60private HashSet additionalContexts;
6162/*** Creates a new instance of GenericMVCContext */63publicGenericMVCContext()
64 {
65 data = new HashMap();
66 additionalContexts = new HashSet();
6768 }
6970/***71 * Adds an existing Collection of contexts into this one. 72 * Externally added contexts are maintained indvidually73 * 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 */77publicGenericMVCContext(Collection contexts)
78 {
79this();
80 additionalContexts.addAll(contexts);
81 }
8283/***84 * Adds an existing context into this one. 85 * Externally added contexts are maintained indvidually86 * and are not merged into the existing context87 * Done to facilitate the context chanining.88 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>89 */90publicGenericMVCContext(Context context)
91 {
92this();
93 additionalContexts.add(context);
94 }
959697publicboolean containsKey(java.lang.Object key)
98 {
99boolean found = data.containsKey(key);
100if (!found)
101 {
102 Iterator itr = additionalContexts.iterator();
103while (itr.hasNext() && !found)
104 {
105 found = ((Context) itr.next()).containsKey(key);
106 }
107 }
108109return found;
110 }
111112public Object get(java.lang.String key)
113 {
114 Object value = data.get(key);
115116// Proceed to search chained contexts117if (value == null)
118 {
119 Iterator itr = additionalContexts.iterator();
120while (itr.hasNext() && value == null)
121 {
122 value = ((Context) itr.next()).get(key);
123 }
124 }
125126return value;
127 }
128129public Object[] getKeys()
130 {
131 Set keySet = data.keySet();
132133 Iterator itr = additionalContexts.iterator();
134135while (itr.hasNext())
136 {
137 Object[] keys = ((Context) itr.next()).getKeys();
138for (int i = 0; i < keys.length; i++)
139 {
140 keySet.add(keys[i]);
141 }
142 }
143144// (Object[])java.lang.reflect.Array.newInstance((new Object()).getClass(),2);145return data.keySet().toArray();
146 }
147148public Object put(java.lang.String key, java.lang.Object value)
149 {
150151return data.put(key, value);
152 }
153154public Object remove(java.lang.Object key)
155 {
156 Object obj = data.remove(key);
157if (obj == null)
158 {
159 Iterator itr = additionalContexts.iterator();
160while (itr.hasNext() && obj == null)
161 {
162 obj = ((Context) itr.next()).remove(key);
163 }
164 }
165166return obj;
167 }
168169/***170 * Add an additional context to this one171 * @param Context context Additional Context object to add.172 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>173 */174publicvoid addContext(Context context)
175 {
176 additionalContexts.add(context);
177 }
178179/***180 * This Collection is "live" as it is the same Collection181 * that maintains this Context's chained contexts. This182 * Collection DOES NOT include objects maintained in183 * the initial context.184 * @return a Collection all the chained contexts185 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>-186 */187public Collection getChainedContexts()
188 {
189return additionalContexts;
190 }
191192 }