KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > event > handlers > HandlerDefinition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.event.handlers;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32
33 /**
34  * <p> A HandlerDefinition defines a "handler" that may be invoked in the
35  * process of executing an event. A HandlerDefinition has an
36  * <strong>id</strong>, <strong>java method</strong>, <strong>input
37  * definitions</strong>, <strong>output definitions</strong>, and
38  * <strong>child handlers</strong>.</p>
39  *
40  * <p> The <strong>java method</strong> to be invoked must have the
41  * following method signature:</p>
42  *
43  * <p> <BLOCKQUOTE></CODE>
44  * public void beginDisplay(HandlerContext handlerCtx)
45  * </CODE></BLOCKQUOTE></p>
46  *
47  * <p> <code>void</code> above can return a value. Depending on the type of
48  * event, return values may be handled differently.</p>
49  *
50  * @author Ken Paulsen (ken.paulsen@sun.com)
51  */

52 public class HandlerDefinition implements java.io.Serializable JavaDoc {
53
54     /**
55      * Constructor
56      */

57     public HandlerDefinition(String JavaDoc id) {
58     _id = id;
59     }
60
61     /**
62      * This method returns the id for this handler.
63      */

64     public String JavaDoc getId() {
65     return _id;
66     }
67
68     /**
69      * For future tool support
70      */

71     public String JavaDoc getDescription() {
72     return _description;
73     }
74
75     /**
76      * For future tool support
77      */

78     public void setDescription(String JavaDoc desc) {
79     _description = desc;
80     }
81
82     /**
83      * <p> This method sets the event handler (method) to be invoked. The
84      * method should be public and accept a prameter of type
85      * "HandlerContext" Example:</p>
86      *
87      * <p> <BLOCKQUOTE>
88      * public void beginDisplay(HandlerContext handlerCtx)
89      * </BLOCKQUOTE></p>
90      *
91      * @param cls The full class name containing method
92      * @param methodName The method name of the handler within class
93      */

94     public void setHandlerMethod(String JavaDoc cls, String JavaDoc methodName) {
95     if ((cls == null) || (methodName == null)) {
96         throw new IllegalArgumentException JavaDoc(
97         "Class name and method name must be non-null!");
98     }
99     _methodClass = cls;
100     _methodName = methodName;
101     }
102
103     /**
104      *
105      */

106     public void setHandlerMethod(Method JavaDoc method) {
107     if (method != null) {
108         _methodName = method.getName();
109         _methodClass = method.getDeclaringClass().getName();
110     } else {
111         _methodName = null;
112         _methodClass = null;
113     }
114     _method = method;
115     }
116
117     /**
118      * <p> This method determines if the handler is static.</p>
119      */

120     public boolean isStatic() {
121     if (_static == null) {
122         _static = Boolean.valueOf(
123             Modifier.isStatic(getHandlerMethod().getModifiers()));
124     }
125     return _static.booleanValue();
126     }
127
128     /**
129      *
130      */

131     public Method JavaDoc getHandlerMethod() {
132     if (_method != null) {
133         // return cached Method
134
return _method;
135     }
136
137     // See if we have the info to find it
138
if ((_methodClass != null) && (_methodName != null)) {
139         // Find the class
140
Class JavaDoc clzz = null;
141         try {
142         clzz = Class.forName(_methodClass);
143         } catch (ClassNotFoundException JavaDoc ex) {
144         throw new RuntimeException JavaDoc("'"
145             + _methodClass + "' not found!", ex);
146         }
147
148         // Find the method on the class
149
Method JavaDoc method = null;
150         try {
151         method = clzz.getMethod(_methodName, EVENT_ARGS);
152         } catch (NoSuchMethodException JavaDoc ex) {
153         throw new RuntimeException JavaDoc(
154             "Method '" + _methodName + "' not found!", ex);
155         }
156
157         // Cache the _method
158
_method = method;
159     }
160
161     // Return the Method if there is one
162
return _method;
163     }
164
165     /**
166      * This method adds an IODescriptor to the list of input descriptors.
167      * These descriptors define the input parameters to this handler.
168      *
169      * @param desc The input IODescriptor to add
170      */

171     public void addInputDef(IODescriptor desc) {
172     _inputDefs.put(desc.getName(), desc);
173     }
174
175     /**
176      * This method sets the input IODescriptors for this handler.
177      *
178      * @param inputDefs The Map of IODescriptors
179      */

180     public void setInputDefs(Map JavaDoc inputDefs) {
181     if (inputDefs == null) {
182         throw new IllegalArgumentException JavaDoc(
183         "inputDefs cannot be null!");
184     }
185     _inputDefs = inputDefs;
186     }
187
188     /**
189      * This method retrieves the Map of input IODescriptors.
190      *
191      * @return The Map of IODescriptors
192      */

193     public Map JavaDoc getInputDefs() {
194     return _inputDefs;
195     }
196
197     /**
198      * This method returns the requested IODescriptor, null if not found.
199      */

200     public IODescriptor getInputDef(String JavaDoc name) {
201     return (IODescriptor) _inputDefs.get(name);
202     }
203
204     /**
205      * This method adds an IODescriptor to the list of output descriptors.
206      * These descriptors define the output parameters to this handler.
207      *
208      * @param desc The IODescriptor to add
209      */

210     public void addOutputDef(IODescriptor desc) {
211     _outputDefs.put(desc.getName(), desc);
212     }
213
214     /**
215      * This method sets the output IODescriptors for this handler.
216      *
217      * @param outputDefs The Map of output IODescriptors
218      */

219     public void setOutputDefs(Map JavaDoc outputDefs) {
220     if (outputDefs == null) {
221         throw new IllegalArgumentException JavaDoc(
222         "outputDefs cannot be null!");
223     }
224     _outputDefs = outputDefs;
225     }
226
227     /**
228      * This method retrieves the Map of output IODescriptors.
229      *
230      * @return The Map of output IODescriptors
231      */

232     public Map JavaDoc getOutputDefs() {
233     return _outputDefs;
234     }
235
236     /**
237      * This method returns the requested IODescriptor, null if not found.
238      */

239     public IODescriptor getOutputDef(String JavaDoc name) {
240     return (IODescriptor) _outputDefs.get(name);
241     }
242
243     /**
244      * This method adds a Handler to the list of child handlers. Child
245      * Handlers are executed PRIOR to this handler executing.
246      *
247      * @param desc The Handler to add
248      */

249     public void addChildHandler(Handler desc) {
250     _childHandlers.add(desc);
251     }
252
253     /**
254      * This method sets the List of child Handlers for this HandlerDefinition.
255      *
256      * @param childHandlers The List of child Handler objects
257      */

258     public void setChildHandlers(List JavaDoc childHandlers) {
259     if (childHandlers == null) {
260         throw new IllegalArgumentException JavaDoc(
261         "childHandlers cannot be null!");
262     }
263     _childHandlers = childHandlers;
264     }
265
266     /**
267      * This method retrieves the List of child Handler.
268      *
269      * @return The List of child Handler for this handler.
270      */

271     public List JavaDoc getChildHandlers() {
272     return _childHandlers;
273     }
274
275
276     public static final Class JavaDoc[] EVENT_ARGS = new Class JavaDoc[] {HandlerContext.class};
277
278     private String JavaDoc _id = null;
279     private String JavaDoc _description = null;
280     private String JavaDoc _methodClass = null;
281     private String JavaDoc _methodName = null;
282     private transient Method JavaDoc _method = null;
283     private Map JavaDoc _inputDefs = new HashMap JavaDoc(5);
284     private Map JavaDoc _outputDefs = new HashMap JavaDoc(5);
285     private List JavaDoc _childHandlers = new ArrayList JavaDoc(5);
286     private transient Boolean JavaDoc _static = null;
287
288     private static final long serialVersionUID = 0xA8B7C6D5E4F30211L;
289 }
290
Popular Tags