KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > MVCRegistry


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;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.apache.log4j.Logger;
14
15 import com.inversoft.util.ReflectionException;
16 import com.inversoft.util.ReflectionTools;
17 import com.inversoft.verge.mvc.controller.ControllerHandler;
18 import com.inversoft.verge.mvc.controller.ControllerParser;
19 import com.inversoft.verge.mvc.controller.DefaultControllerParser;
20 import com.inversoft.verge.mvc.model.DefaultModelParser;
21 import com.inversoft.verge.mvc.model.ModelHandler;
22 import com.inversoft.verge.mvc.model.ModelParser;
23 import com.inversoft.verge.mvc.model.ModelResolver;
24 import com.inversoft.verge.mvc.validator.DefaultValidatorParser;
25 import com.inversoft.verge.mvc.validator.ValidatorHandler;
26 import com.inversoft.verge.mvc.validator.ValidatorParser;
27
28
29 /**
30  * <p>
31  * This class is a common storage location for the MVC related
32  * classes and implementations. This is used by the default
33  * Inversoft MVCMediator to lookup the ModelParser and
34  * ControllerParser implementations to call. These
35  * implemenations are stored in this registry using a key (or
36  * a name) and they are retrieved by the MVCMediator using
37  * the values from either the request of context (see
38  * {@link com.inversoft.verge.mvc.DefaultMVCMediator DefaultMVCMediator} for more
39  * information).
40  * </p>
41  *
42  * <p>
43  * If a new ModelParser or ControllerParser is going to be
44  * used, they must be registered in this class. Once they
45  * are registered, the DefaultMVCMediator will locate and
46  * invoke them.
47  * </p>
48  *
49  * <p>
50  * This registry also stores other implementation classes
51  * used by the default Inversoft MVC implementation.
52  * </p>
53  *
54  * <p>
55  * For performance on high volume sites, this does not
56  * synchronize to Maps that store all the classes. Therefore,
57  * you should load all new implementations of these interface
58  * and store them in this Registry at application startup to
59  * prevent unknown behavior.
60  * </p>
61  *
62  * @author Brian Pontarelli
63  * @since 2.0
64  * @version 2.0
65  */

66 public class MVCRegistry {
67
68     /**
69      * This classes logger
70      */

71     private static final Logger logger = Logger.getLogger(MVCRegistry.class);
72
73     /**
74      * The Map that stores the ControllerParsers
75      */

76     private static Map JavaDoc controllerParsers = new HashMap JavaDoc();
77
78     /**
79      * The Map that stores the ControllerHandlers
80      */

81     private static Map JavaDoc controllerHandlers = new HashMap JavaDoc();
82
83     /**
84      * The Map that stores the ModelParsers
85      */

86     private static Map JavaDoc modelParsers = new HashMap JavaDoc();
87
88     /**
89      * The Map that stores the ModelResolvers
90      */

91     private static Map JavaDoc modelResolvers = new HashMap JavaDoc();
92
93     /**
94      * The Map that stores the ModelHandlers
95      */

96     private static Map JavaDoc modelHandlers = new HashMap JavaDoc();
97
98     /**
99      * The Map that stores the ValidatorParsers
100      */

101     private static Map JavaDoc validatorParsers = new HashMap JavaDoc();
102
103     /**
104      * The Map that stores the ValidatorHandlers
105      */

106     private static Map JavaDoc validatorHandlers = new HashMap JavaDoc();
107
108
109     /**
110      * Setup the Default implementations of everything. This uses reflection so
111      * that there are no class file dependencies and any JAR file can be left out
112      * of the application and it will still function.
113      */

114     static {
115         controllerParsers.put(MVCConstants.DEFAULT_NAME, new DefaultControllerParser());
116         modelParsers.put(MVCConstants.DEFAULT_NAME, new DefaultModelParser());
117         validatorParsers.put(MVCConstants.DEFAULT_NAME, new DefaultValidatorParser());
118
119         try {
120             controllerHandlers.put(MVCConstants.ACTIONFLOW_NAME,
121                 ReflectionTools.instantiate(
122                     "com.inversoft.verge.mvc.controller.actionflow.ActionFlowControllerHandler"));
123             modelHandlers.put(MVCConstants.ACTIONFLOW_NAME,
124                 ReflectionTools.instantiate(
125                     "com.inversoft.verge.mvc.model.DefaultModelHandler"));
126             modelResolvers.put(MVCConstants.ACTIONFLOW_NAME,
127                 ReflectionTools.instantiate(
128                     "com.inversoft.verge.mvc.model.actionflow.ActionFlowModelResolver"));
129             validatorHandlers.put(MVCConstants.ACTIONFLOW_NAME,
130                 ReflectionTools.instantiate(
131                     "com.inversoft.verge.mvc.validator.actionflow.ActionFlowValidatorHandler"));
132         } catch (ReflectionException re) {
133             logger.info("ActionFlow system disabled (see REASON)");
134             logger.info("[REASON] " + re.toString());
135         }
136
137         try {
138             controllerHandlers.put(MVCConstants.FORM_NAME,
139                 ReflectionTools.instantiate(
140                     "com.inversoft.verge.mvc.controller.form.FormControllerHandler"));
141             modelHandlers.put(MVCConstants.FORM_NAME,
142                 ReflectionTools.instantiate(
143                     "com.inversoft.verge.mvc.model.DefaultModelHandler"));
144             modelResolvers.put(MVCConstants.FORM_NAME,
145                 ReflectionTools.instantiate(
146                     "com.inversoft.verge.mvc.model.form.FormModelResolver"));
147             validatorHandlers.put(MVCConstants.FORM_NAME,
148                 ReflectionTools.instantiate(
149                     "com.inversoft.verge.mvc.validator.form.FormValidatorHandler"));
150         } catch (ReflectionException re) {
151             logger.info("Form-Based MVC system disabled (see REASON)");
152             logger.info("[REASON] " + re.toString());
153         }
154
155         try {
156             modelHandlers.put(MVCConstants.REPOSITORY_NAME,
157                 ReflectionTools.instantiate(
158                     "com.inversoft.verge.mvc.model.repository.RepositoryModelHandler"));
159             modelResolvers.put(MVCConstants.REPOSITORY_NAME,
160                 ReflectionTools.instantiate(
161                     "com.inversoft.verge.mvc.model.repository.RepositoryModelResolver"));
162         } catch (ReflectionException re) {
163             logger.info("Repository system disabled (see REASON)");
164             logger.info("[REASON] " + re.toString());
165         }
166
167         try {
168             modelHandlers.put(MVCConstants.WEB_NAME,
169                 ReflectionTools.instantiate("com.inversoft.verge.mvc.model.web.WebModelHandler"));
170             modelResolvers.put(MVCConstants.WEB_NAME,
171                 ReflectionTools.instantiate("com.inversoft.verge.mvc.model.web.WebModelResolver"));
172         } catch (ReflectionException re) {
173             logger.info("Web MVC system disabled (see REASON)");
174             logger.info("[REASON] " + re.toString());
175         }
176     }
177
178
179     /**
180      * Constructs a new <code>MVCRegistry</code>.
181      */

182     private MVCRegistry() {
183         // Static class
184
}
185
186
187     /**
188      * Registers the given {@link ControllerParser} under the given name.
189      *
190      * @param name The name to register the ControllerParser under
191      * @param parser The ControllerParser to register
192      */

193     public static void register(String JavaDoc name, ControllerParser parser) {
194         controllerParsers.put(name, parser);
195     }
196
197     /**
198      * Retrieves the {@link ControllerParser} registered under the given name.
199      *
200      * @param name The name of the ControllerParser was registered under
201      * @return The ControllerRegister or null if the name is not registered
202      */

203     public static ControllerParser lookupControllerParser(String JavaDoc name) {
204         return (ControllerParser) controllerParsers.get(name);
205     }
206
207     /**
208      * Registers the given {@link ControllerHandler} under the given name.
209      *
210      * @param name The name to register the ControllerHandler under
211      * @param handler The ControllerHandler to register
212      */

213     public static void register(String JavaDoc name, ControllerHandler handler) {
214         controllerHandlers.put(name, handler);
215     }
216
217     /**
218      * Retrieves the {@link ControllerHandler} registered under the given name.
219      *
220      * @param name The name of the ControllerHandler was registered under
221      * @return The ControllerHandler or null if the name is not registered
222      */

223     public static ControllerHandler lookupControllerHandler(String JavaDoc name) {
224         return (ControllerHandler) controllerHandlers.get(name);
225     }
226
227     /**
228      * Registers the given {@link ModelParser} under the given name.
229      *
230      * @param name The name to register the ModelParser under
231      * @param parser The ModelParser to register
232      */

233     public static void register(String JavaDoc name, ModelParser parser) {
234         modelParsers.put(name, parser);
235     }
236
237     /**
238      * Retrieves the {@link ModelParser} registered under the given name
239      *
240      * @param name The name of the ModelParser was registered under
241      * @return The ModelParser or null if the name is not registered
242      */

243     public static ModelParser lookupModelParser(String JavaDoc name) {
244         return (ModelParser) modelParsers.get(name);
245     }
246
247     /**
248      * Registers the given {@link ModelResolver} under the given name.
249      *
250      * @param name The name to register the ModelResolver under
251      * @param resolver The ModelResolver to register
252      */

253     public static void register(String JavaDoc name, ModelResolver resolver) {
254         modelResolvers.put(name, resolver);
255     }
256
257     /**
258      * Retrieves the {@link ModelResolver} registered under the given name
259      *
260      * @param name The name of the ModelResolver was registered under
261      * @return The ModelResolver or null if the name is not registered
262      */

263     public static ModelResolver lookupModelResolver(String JavaDoc name) {
264         return (ModelResolver) modelResolvers.get(name);
265     }
266
267     /**
268      * Registers the given {@link ModelHandler} under the given name.
269      *
270      * @param name The name to register the ModelHandler under
271      * @param handler The ModelHandler to register
272      */

273     public static void register(String JavaDoc name, ModelHandler handler) {
274         modelHandlers.put(name, handler);
275     }
276
277     /**
278      * Retrieves the {@link ModelHandler} registered under the given name
279      *
280      * @param name The name of the ModelHandler was registered under
281      * @return The ModelHandler or null if the name is not registered
282      */

283     public static ModelHandler lookupModelHandler(String JavaDoc name) {
284         return (ModelHandler) modelHandlers.get(name);
285     }
286
287     /**
288      * Registers the given {@link ValidatorParser} under the given name.
289      *
290      * @param name The name to register the ValidatorParser under
291      * @param parser The ValidatorParser to register
292      */

293     public static void register(String JavaDoc name, ValidatorParser parser) {
294         validatorParsers.put(name, parser);
295     }
296
297     /**
298      * Retrieves the {@link ValidatorParser} registered under the given name
299      *
300      * @param name The name of the ValidatorParser was registered under
301      * @return The ValidatorParser or null if the name is not registered
302      */

303     public static ValidatorParser lookupValidatorParser(String JavaDoc name) {
304         return (ValidatorParser) validatorParsers.get(name);
305     }
306
307     /**
308      * Registers the given {@link ValidatorHandler} under the given name.
309      *
310      * @param name The name to register the ValidatorHandler under
311      * @param handler The ValidatorHandler to register
312      */

313     public static void register(String JavaDoc name, ValidatorHandler handler) {
314         validatorHandlers.put(name, handler);
315     }
316
317     /**
318      * Retrieves the {@link ValidatorHandler ValidatorHandler} registered under
319      * the given name
320      *
321      * @param name The name of the ValidatorHandler was registered under
322      * @return The ValidatorHandler or null if the name is not registered
323      */

324     public static ValidatorHandler lookupValidatorHandler(String JavaDoc name) {
325         return (ValidatorHandler) validatorHandlers.get(name);
326     }
327 }
Popular Tags