KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > portlets > GenericMVCContext


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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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 and
14  * limitations under the License.
15  */

16 /*
17  * GenericMVCContext.java
18  *
19  * Created on January 27, 2003, 8:47 PM
20  */

21 package org.apache.jetspeed.portal.portlets;
22
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
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  * Note:
51  *
52  * create the generic context interface later
53  * just use Velocity's for now
54  *
55  */

56 public class GenericMVCContext implements Context
57 {
58
59     private HashMap JavaDoc data;
60     private HashSet JavaDoc additionalContexts;
61
62     /** Creates a new instance of GenericMVCContext */
63     public GenericMVCContext()
64     {
65         data = new HashMap JavaDoc();
66         additionalContexts = new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc key)
98     {
99         boolean found = data.containsKey(key);
100         if (!found)
101         {
102             Iterator JavaDoc 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 JavaDoc get(java.lang.String JavaDoc key)
113     {
114         Object JavaDoc value = data.get(key);
115
116         // Proceed to search chained contexts
117
if (value == null)
118         {
119             Iterator JavaDoc 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 JavaDoc[] getKeys()
130     {
131         Set JavaDoc keySet = data.keySet();
132
133         Iterator JavaDoc itr = additionalContexts.iterator();
134
135         while (itr.hasNext())
136         {
137             Object JavaDoc[] keys = ((Context) itr.next()).getKeys();
138             for (int i = 0; i < keys.length; i++)
139             {
140                 keySet.add(keys[i]);
141             }
142         }
143
144         // (Object[])java.lang.reflect.Array.newInstance((new Object()).getClass(),2);
145
return data.keySet().toArray();
146     }
147
148     public Object JavaDoc put(java.lang.String JavaDoc key, java.lang.Object JavaDoc value)
149     {
150
151         return data.put(key, value);
152     }
153
154     public Object JavaDoc remove(java.lang.Object JavaDoc key)
155     {
156         Object JavaDoc obj = data.remove(key);
157         if (obj == null)
158         {
159             Iterator JavaDoc 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 JavaDoc getChainedContexts()
188     {
189         return additionalContexts;
190     }
191
192 }
193
Popular Tags