KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > config > ActionFlowConfigRegistry


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.actionflow.config;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.ServletRequest JavaDoc;
15
16 import com.inversoft.config.ConfigRegistry;
17 import com.inversoft.util.StringTools;
18
19
20 /**
21  * This class stores all the configuration objects models. This
22  * includes the top level Namespace objects which in
23  * turn contain the Node and ActionLink objects.
24  *
25  * @author Brian Pontarelli
26  * @since 2.0
27  * @version 2.0
28  */

29 public class ActionFlowConfigRegistry implements ConfigRegistry {
30
31     /**
32      * The key that the registry is stored under in the request to ensure that
33      * for a single request to the server, the configuration remains the same
34      */

35     public static final String JavaDoc KEY = ActionFlowConfigRegistry.class.getName();
36
37     /**
38      * ThreadLocals will NOT work because containers use Thread pooling so the
39      * execute Thread probably has already been used and the ThreadLocal will
40      * already have been setup
41      */

42     private static volatile ActionFlowConfigRegistry instance =
43         new ActionFlowConfigRegistry();
44
45
46     /**
47      * The Map used to store all the Namespaces
48      */

49     private Map JavaDoc namespaces;
50
51
52     /**
53      * Singleton class
54      */

55     protected ActionFlowConfigRegistry() {
56         namespaces = new HashMap JavaDoc();
57     }
58
59     /**
60      * Creates a new instance identical to the given instance
61      */

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

87     public static ActionFlowConfigRegistry getInstance(ServletRequest JavaDoc request) {
88         assert (instance != null) : "instance == null";
89
90         ActionFlowConfigRegistry localInstance = null;
91         if (request == null) {
92             localInstance = instance;
93         } else {
94             localInstance = (ActionFlowConfigRegistry) request.getAttribute(KEY);
95         }
96         
97         if (localInstance == null && request != null) {
98             request.setAttribute(KEY, instance);
99             localInstance = instance;
100         }
101
102         return localInstance;
103     }
104
105     /**
106      * Singleton setter method. This sets a new store instance into the singleton
107      * and it is very important to understand that this change
108      */

109     protected static void setInstance(ActionFlowConfigRegistry newStore) {
110         assert (newStore != null) : "newStore == null";
111         instance = newStore;
112     }
113
114
115     /**
116      * Stores the given Namespace into this store.
117      *
118      * @param namespace The Namespace to add to the store
119      * @return The old Namespace that was stored and had the same name
120      * as the given Namespace or null if one with the same name
121      * doesn't exist in the store
122      * @throws IllegalArgumentException If namespace is null
123      */

124     protected Namespace register(Namespace namespace)
125     throws IllegalArgumentException JavaDoc {
126         if (namespace == null) {
127             throw new IllegalArgumentException JavaDoc("The namespace is null");
128         }
129
130         return (Namespace) namespaces.put(namespace.getName(), namespace);
131     }
132
133     /**
134      * Finds the Namespace with the given name inside this store.
135      *
136      * @param name The name of the namespace to locate
137      * @return The Namespace that was stored under the given name or null
138      * if one was never stored
139      * @throws IllegalArgumentException If name is null or empty
140      */

141     public Namespace lookup(String JavaDoc name) throws IllegalArgumentException JavaDoc {
142         if (name == null || StringTools.isEmpty(name)) {
143             throw new IllegalArgumentException JavaDoc("The name is null or empty");
144         }
145
146         return (Namespace) namespaces.get(name);
147     }
148
149     /**
150      * Returns an Iterator over all the Namespaces stored in this ActionFlowConfigRegistry.
151      * This Iterator is NOT live and calling the remove method has no effect
152      * on the ActionFlowConfigRegistry. This Iterator might not reflect the exact values
153      * stored in the ActionFlowConfigRegistry because it is a copy.
154      *
155      * @return An Iterator over all the Namespaces stored in the ActionFlowConfigRegistry.
156      */

157     public Iterator JavaDoc iterator() {
158         Map JavaDoc temp = new HashMap JavaDoc(namespaces);
159         return temp.values().iterator();
160     }
161 }
Popular Tags