KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > config > FormMVCConfigRegistry


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.form.config;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.servlet.ServletRequest JavaDoc;
16
17 import com.inversoft.config.ConfigRegistry;
18 import com.inversoft.verge.mvc.config.BaseFormConfig;
19 import com.inversoft.verge.mvc.config.FormConfigRegistry;
20
21
22 /**
23  * <p>
24  * This class is the ConfigRegistry for the Form based MVC
25  * system of the Inversoft Portal framework. This stores
26  * all the configuration objects that are built by this
27  * configuration package.
28  * </p>
29  *
30  * @author Brian Pontarelli
31  * @since 2.0
32  * @version 2.0
33  */

34 public class FormMVCConfigRegistry implements ConfigRegistry, FormConfigRegistry {
35     
36     /**
37      * The key that the registry is stored under in the request to ensure that
38      * for a single request to the server, the configuration remains the same
39      */

40     public static final String JavaDoc KEY = FormMVCConfigRegistry.class.getName();
41
42     /**
43      * ThreadLocals will NOT work because containers use Thread pooling so the
44      * execute Thread probably has already been used and the ThreadLocal will
45      * already have been setup
46      */

47     private static volatile FormMVCConfigRegistry instance = new FormMVCConfigRegistry();
48
49     private Map JavaDoc forms;
50     private Map JavaDoc actions;
51     private Map JavaDoc mappings;
52
53
54     /**
55      * Constructs a new <code>FormMVCConfigRegisty</code>.
56      */

57     protected FormMVCConfigRegistry() {
58         forms = new HashMap JavaDoc();
59         actions = new HashMap JavaDoc();
60         mappings = new HashMap JavaDoc();
61     }
62
63
64     /**
65      * <p>
66      * Singleton accessor method except that the HttpServletRequest is used in
67      * order to maintain the context of a single request. Since a single request
68      * could be in progress while the configuration is changing, this prevents
69      * that request from seeing the changes because a reference to the
70      * configuration is stored in the request and then retrieved from there.
71      * </p>
72      *
73      * <p>
74      * Passing null to this method returns the current configuration. Subsequent
75      * calls with null are not guarenteed to return the same configuration
76      * instance.
77      * </p>
78      *
79      * @param request (Optional) The ServletRequest used to maintained request
80      * consistency
81      * @return The singleton instance
82      */

83     public static FormMVCConfigRegistry getInstance(ServletRequest JavaDoc request) {
84         assert (instance != null) : "instance == null";
85
86         FormMVCConfigRegistry localInstance = null;
87         if (request == null) {
88             localInstance = instance;
89         } else {
90             localInstance = (FormMVCConfigRegistry) request.getAttribute(KEY);
91         }
92         
93         if (localInstance == null && request != null) {
94             request.setAttribute(KEY, instance);
95             localInstance = instance;
96         }
97
98         return localInstance;
99     }
100
101     /**
102      * Singleton setter for reloading the configuration
103      *
104      * @param newInstance The new singleton instance
105      */

106     protected static void setInstance(FormMVCConfigRegistry newInstance) {
107         assert (newInstance != null) : "newInstance == null";
108         instance = newInstance;
109     }
110
111
112     /**
113      * Retrieves a form configuration from the repository with the given String
114      * name
115      *
116      * @param name The name of the form configuration to fetch from the
117      * repository
118      * @return The FormConfig from the repository with the given name, or null
119      * it the name does not exist in the repository
120      */

121     public BaseFormConfig lookupForm(String JavaDoc name) {
122         return (FormConfig) forms.get(name);
123     }
124
125     /**
126      * Default visibility method that stores an form configuration in the
127      * repository. Only the RepositoryBuilder should ever be calling this method
128      *
129      * @param name The name to register the form under
130      * @param config The FormConfig object being registered
131      */

132     protected void register(String JavaDoc name, FormConfig config) {
133
134         assert (name != null) : "name == null";
135         assert (config != null) : "config == null";
136
137         forms.put(name, config);
138     }
139
140     /**
141      * Returns the action with the given name. First the action is searched for
142      * in the form config given (unless it is null) then it is searched for in
143      * the actions hash of this registry
144      *
145      * @param name The name of the action to search for
146      * @param formConfig (Optional) The form config where the action may be
147      * stored
148      * @return The action if found, otherwise null
149      */

150     public ActionConfig lookupAction(String JavaDoc name, FormConfig formConfig) {
151
152         ActionConfig config = null;
153
154         if (formConfig != null) {
155             config = formConfig.getActionConfig(name);
156         }
157
158         if (config == null) {
159             config = (ActionConfig) actions.get(name);
160         }
161
162         return config;
163     }
164
165     /**
166      * Returns the action with the given name from the global list which is also.
167      * the hash in this registry.
168      *
169      * @param name The name of the action to search for
170      * @return The action if found, otherwise null
171      */

172     public ActionConfig lookupAction(String JavaDoc name) {
173         return (ActionConfig) actions.get(name);
174     }
175
176     /**
177      * Registers the given action config in the actions hash
178      *
179      * @param name The name to register the action under
180      * @param config The action config to register
181      */

182     protected void register(String JavaDoc name, ActionConfig config) {
183         assert (name != null) : "name == null";
184         assert (config != null) : "config == null";
185
186         actions.put(name, config);
187     }
188
189     /**
190      * Returns the mapping with the given name. First the mapping is searched
191      * for in the form config given (unless it is null) then it is searched for
192      * in the mappings hash of this registry
193      *
194      * @param name The name of the mapping to search for
195      * @param formConfig (Optional) The form config where the mapping may be
196      * stored
197      * @return The action if found, otherwise null
198      */

199     public MappingConfig lookupMapping(String JavaDoc name, FormConfig formConfig) {
200
201         MappingConfig config = null;
202
203         if (formConfig != null) {
204             config = formConfig.getMappingConfig(name);
205         }
206
207         if (config == null) {
208             config = (MappingConfig) mappings.get(name);
209         }
210
211         return config;
212     }
213
214     /**
215      * Returns the mapping with the given name from the global list which is also
216      * the hash in this registry.
217      *
218      * @param name The name of the mapping to search for
219      * @return The MappingConfig if found, otherwise null
220      */

221     public MappingConfig lookupMapping(String JavaDoc name) {
222         return (MappingConfig) mappings.get(name);
223     }
224
225     /**
226      * Registers the given mapping config in the mappings hash
227      *
228      * @param name The name to register the mapping under
229      * @param config The mapping config to register
230      */

231     protected void register(String JavaDoc name, MappingConfig config) {
232         assert (name != null) : "name == null";
233         assert (config != null) : "config == null";
234
235         mappings.put(name, config);
236     }
237
238     /**
239      * Returns all the configurations in a List
240      *
241      * @return All the configurations in a List which is never null
242      */

243     public List JavaDoc getAllConfigs() {
244         List JavaDoc list = new ArrayList JavaDoc(actions.values());
245         list.addAll(forms.values());
246
247         return list;
248     }
249 }
250
Popular Tags