KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > NodeExecutorRegistry


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;
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 com.inversoft.util.StringTools;
16
17
18 /**
19  * <p>
20  * This is a singleton class that stores all the registered
21  * NodeExecuters for the ActionFlow system. It uses static
22  * methods rather than the standard singleton getInstance
23  * layout.
24  * </p>
25  *
26  * <p>
27  * NodeExecuters are registered with a specific type of Node
28  * that they are able to execute. The type is always a String.
29  * </p>
30  *
31  * <p>
32  * NodeExecuters can be unregistered as well as replaced at
33  * runtime, however, this will not effect the ActionFlow system
34  * until the server is restarted. This is due to a design
35  * decision about the underlying system in order to increase
36  * performance in high volume and high traffic applications.
37  * </p>
38  *
39  * <p>
40  * This class needs to be renamed to the ActionFlowRegistry
41  * because it now also stores the ExceptionHandler instance
42  * for the ActionFlow system. This is used to handle any
43  * exceptions that might occur during executing.
44  * </p>
45  *
46  * @author Brian Pontarelli
47  * @since 2.0
48  * @version 2.0
49  */

50 public class NodeExecutorRegistry {
51
52     private final static Map JavaDoc executors = new HashMap JavaDoc();
53     private static ExceptionHandler exceptionHandler = new BaseExceptionHandler();
54
55     /**
56      * The name under which the default ActionHandlerExecutor is stored
57      */

58     public static final String JavaDoc ACTION_HANDLER_KEY = "actionHandler";
59
60     /**
61      * The name under which the default PresentationNodeExecutor is stored
62      */

63     public static final String JavaDoc PRESENTATION_KEY = "presentation";
64
65     /**
66      * Sets up the default executors
67      */

68     static {
69         register(ACTION_HANDLER_KEY, new ActionHandlerNodeExecutor());
70         register(PRESENTATION_KEY, new PresentationNodeExecutor());
71     }
72
73
74     /**
75      * This is a static class
76      */

77     private NodeExecutorRegistry() {
78         // Empty
79
}
80
81
82     /**
83      * Registers the given NodeExecutor under the given name. If there was
84      * already something registered under that name, it is returned.
85      *
86      * @param executor The NodeExecutor to register
87      * @param name The name to register the NodeExecutor under
88      * @return The old NodeExecutor registered under the given name, if any,
89      * or null.
90      * @throws IllegalArgumentException If executor or name are null or empty
91      */

92     public static NodeExecutor register(String JavaDoc name, NodeExecutor executor) {
93         if (executor == null || name == null || StringTools.isEmpty(name)) {
94             throw new IllegalArgumentException JavaDoc("The NodeExecutor and/or name are null or empty");
95         }
96
97         return (NodeExecutor) executors.put(name, executor);
98     }
99
100     /**
101      * Unregisters the NodeExecutor that is registered under the given name.
102      *
103      * @param name The name to unregister
104      * @return The old NodeExecutor registered under the given name, if any,
105      * or null.
106      * @throws IllegalArgumentException If name is null or empty
107      */

108     public static NodeExecutor unregister(String JavaDoc name) {
109         if (name == null || StringTools.isEmpty(name)) {
110             throw new IllegalArgumentException JavaDoc("The name is null or empty");
111         }
112
113         return (NodeExecutor) executors.remove(name);
114     }
115
116     /**
117      * Returns the NodeExecutor registered under the given name.
118      *
119      * @param name The name of the NodeExecutor to lookup
120      * @return The NodeExecutor that was registered under the given name or null
121      * if one was never registered
122      * @throws IllegalArgumentException If name is null or empty
123      */

124     public static NodeExecutor lookup(String JavaDoc name) {
125         if (name == null || StringTools.isEmpty(name)) {
126             throw new IllegalArgumentException JavaDoc("The name is null or empty");
127         }
128
129         return (NodeExecutor) executors.get(name);
130     }
131
132     /**
133      * Returns a List containing all the names that NodeExecutors are registed
134      * under. This List is not live and changes to it will not affect the registry.
135      *
136      * @return A List containing all the names NodeExecutors are registered under
137      * and never null
138      */

139     public static List JavaDoc registeredNames() {
140         return new ArrayList JavaDoc(executors.keySet());
141     }
142
143     /**
144      * Returns a List containing all the registered NodeExecutors. This List
145      * is not live and changes to it will not affect the registry.
146      *
147      * @return A List containing all the registered NodeExecutors and never
148      * null
149      */

150     public static List JavaDoc registeredExcutors() {
151         return new ArrayList JavaDoc(executors.values());
152     }
153
154     /**
155      * Returns the single ExceptionHandler instance for the ActionFlow system
156      *
157      * @return The single ExceptionHandler instance for the ActionFlow system
158      */

159     public static ExceptionHandler lookupExceptionHandler() {
160         return exceptionHandler;
161     }
162     
163     /**
164      * Registers the given ExceptionHandler instance for the ActionFlow system
165      * and returns the old one (if any).
166      *
167      * @param newHandler The single ExceptionHandler instance for the
168      * ActionFlow system
169      * @return The previously registered ExceptionHandler
170      */

171     public static ExceptionHandler registerExceptionHandler(
172         ExceptionHandler newHandler)
173     {
174         ExceptionHandler old = exceptionHandler;
175         exceptionHandler = newHandler;
176         return old;
177     }
178 }
179
Popular Tags