KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > event > descriptors > HandlerDescriptor


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 package com.sun.enterprise.tools.guiframework.event.descriptors;
23
24 import com.iplanet.jato.RequestContext;
25
26 import com.sun.enterprise.tools.guiframework.FrameworkDescriptor;
27 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
28 import com.sun.enterprise.tools.guiframework.util.PermissionChecker;
29 import com.sun.enterprise.tools.guiframework.util.Util;
30 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
31 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
32
33 import java.lang.reflect.Method JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39
40 /**
41  * This class describes an event handler (method). Optionally, "mappings"
42  * can be performed before or after a handler. This class maintains 2 lists
43  * of mappings, one for before and one for after.
44  */

45 public class HandlerDescriptor implements FrameworkDescriptor {
46
47     /**
48      * Constructor.
49      *
50      * @param name The name
51      */

52     public HandlerDescriptor(String JavaDoc name) {
53     setName(name);
54     }
55
56
57     /**
58      * This method returns the name for this handler definition.
59      */

60     public String JavaDoc getName() {
61     return _name;
62     }
63
64
65     /**
66      * This method sets the handler definitions name (used by the contsrutor).
67      */

68     protected void setName(String JavaDoc name) {
69     if (name == null) {
70         throw new FrameworkException("Handler name cannot be null!");
71     }
72     _name = name;
73     }
74
75
76     /**
77      * For future tool support
78      */

79     public String JavaDoc getDescription() {
80     return _description;
81     }
82
83
84     /**
85      * For future tool support
86      */

87     public void setDescription(String JavaDoc desc) {
88     _description = desc;
89     }
90
91
92     /**
93      * This method returns the EventDescriptor that contains this handler.
94      *
95      * @return The parent descriptor
96      */

97 //NOTE: HandlerDescriptor.getParent() will not work because we are not going to fully instantiate the tree (it may have multiple parents). See if we really need this anyway. If we do, consider adding an execution stack instead.
98
public FrameworkDescriptor getParent() {
99     throw new FrameworkException(
100         "Get parent is not implemented for HandlerDescriptors!");
101     }
102
103
104     /**
105      * This method sets the parent EventDescriptor.
106      *
107      * @param parent The parent EventDescriptor
108      * /
109     protected void setParent(EventDescriptor parent) {
110     if (parent == null) {
111         throw new RuntimeException("You must provide a the " +
112         "EventDescriptor which contains this HandlerDescriptor!");
113     }
114     _parent = parent;
115     }
116 */

117
118
119     /**
120      * <P>This method sets the event handler (method) to be invoked. The
121      * method should be public; accept 2 parameters: 1 of type
122      * "RequestContext", one of type "HandlerContext" Example:</P>
123      *
124      * <P> <BLOCKQUOTE>
125      * public void beginDisplay(
126      * RequestContext ctx, HandlerContext handlerCtx)
127      * throws ModelControlException
128      * </BLOCKQUOTE></P>
129      *
130      * @param cls The full class name containing method
131      * @param methodName The method name of the handler within class
132      */

133     public void setHandlerMethod(String JavaDoc cls, String JavaDoc methodName) {
134     if ((cls == null) || (methodName == null)) {
135         throw new IllegalArgumentException JavaDoc(
136         "Class name and method name must be non-null!");
137     }
138
139     // Save class / method name info
140
_cls = cls;
141     _methodName = methodName;
142     }
143
144     private void setMethod() {
145     if ((_cls == null) || (_methodName == null)) {
146         return;
147     }
148
149     // Find the class
150
Class JavaDoc clzz = null;
151     try {
152         clzz = Class.forName(_cls);
153     } catch (ClassNotFoundException JavaDoc ex) {
154         throw new RuntimeException JavaDoc("Class '"+_cls+"' not found!", ex);
155     }
156
157     // Find the method on the class
158
Method JavaDoc method = null;
159     try {
160         method = clzz.getMethod(_methodName, EVENT_ARGS);
161     } catch (NoSuchMethodException JavaDoc ex) {
162         throw new RuntimeException JavaDoc("Method '"+_methodName+"' not found!", ex);
163     }
164
165     // Add to the list
166
_method = method;
167     }
168
169     public void setHandlerMethod(Method JavaDoc method) {
170     _method = method;
171     _cls = method.getDeclaringClass().getName();
172     _methodName = method.getName();
173     }
174
175     
176     public Method JavaDoc getHandlerMethod() {
177     if (_method == null) {
178         setMethod();
179     }
180     return _method;
181     }
182
183
184     /**
185      * This method adds an IODescriptor to the list of input descriptors.
186      * These descriptors define the input parameters to this handler.
187      *
188      * @param desc The input IODescriptor to add
189      */

190     public void addInputDescriptor(IODescriptor desc) {
191     _inputDescriptors.put(desc.getName(), desc);
192     }
193
194
195     /**
196      * This method sets the input IODescriptors for this handler.
197      *
198      * @param inputDescriptors The Map of IODescriptors
199      */

200     public void setInputDescriptors(Map JavaDoc inputDescriptors) {
201     if (inputDescriptors == null) {
202         throw new IllegalArgumentException JavaDoc(
203         "inputDescriptors cannot be null!");
204     }
205     _inputDescriptors = inputDescriptors;
206     }
207
208
209     /**
210      * This method retrieves the Map of input IODescriptors.
211      *
212      * @return The Map of IODescriptors
213      */

214     public Map JavaDoc getInputDescriptors() {
215     return _inputDescriptors;
216     }
217
218
219     /**
220      * This method returns the requested IODescriptor, null if not found.
221      */

222     public IODescriptor getInputDescriptor(String JavaDoc name) {
223     return (IODescriptor)_inputDescriptors.get(name);
224     }
225
226
227     /**
228      * This method adds an IODescriptor to the list of output descriptors.
229      * These descriptors define the output parameters to this handler.
230      *
231      * @param desc The IODescriptor to add
232      */

233     public void addOutputDescriptor(IODescriptor desc) {
234     _outputDescriptors.put(desc.getName(), desc);
235     }
236
237
238     /**
239      * This method sets the output IODescriptors for this handler.
240      *
241      * @param outputDescriptors The Map of output IODescriptors
242      */

243     public void setOutputDescriptors(Map JavaDoc outputDescriptors) {
244     if (outputDescriptors == null) {
245         throw new IllegalArgumentException JavaDoc(
246         "outputDescriptors cannot be null!");
247     }
248     _outputDescriptors = outputDescriptors;
249     }
250
251
252     /**
253      * This method retrieves the Map of output IODescriptors.
254      *
255      * @return The Map of output IODescriptors
256      */

257     public Map JavaDoc getOutputDescriptors() {
258     return _outputDescriptors;
259     }
260
261
262     /**
263      * This method returns the requested IODescriptor, null if not found.
264      */

265     public IODescriptor getOutputDescriptor(String JavaDoc name) {
266     return (IODescriptor)_outputDescriptors.get(name);
267     }
268
269
270     /**
271      * This method adds a useHandler to the list of child handlers. Child
272      * handlers are executed PRIOR to this handler executing.
273      *
274      * @param desc The UseHandlerDescriptor to add
275      */

276     public void addChildHandlerDescriptor(UseHandlerDescriptor desc) {
277     _childHandlers.add(desc);
278     }
279
280
281     /**
282      * This method sets the List of child handlers for this handler.
283      *
284      * @param childHandlers The List of child handlers
285      */

286     public void setChildHandlerDescriptors(List JavaDoc childHandlers) {
287     if (childHandlers == null) {
288         throw new IllegalArgumentException JavaDoc(
289         "childHandlers cannot be null!");
290     }
291     _childHandlers = childHandlers;
292     }
293
294
295     /**
296      * This method retrieves the List of child UseHandlerDescriptors.
297      *
298      * @return The List of child UseHandlerDescriptors for this handler.
299      */

300     public List JavaDoc getChildHandlerDescriptors() {
301     return _childHandlers;
302     }
303
304
305     /**
306      * Accessor for the ifCheck String
307      */

308     public String JavaDoc getIfCheck() {
309     return _ifCheck;
310     }
311
312
313     /**
314      * Setter for the ifCheck String
315      */

316     public void setIfCheck(String JavaDoc ifCheck) {
317     _ifCheck = ifCheck;
318     }
319
320
321     /**
322      * This method uses a PermissionChecker to evaluate the "ifCheck
323      * String". The ifCheck String is a boolean expression, see
324      * PermissionChecker for details. If the ifCheck String is null,
325      * then this method will return true. $(attribute-name) entries within
326      * the ifCheck String will be evaluated; they should evaluate to the
327      * Strings "true" or "false".
328      *
329      * @param vd The ViewDescriptor
330      *
331      * @return True if this Mapping has permission, false if not
332      */

333     public boolean hasPermission(ViewDescriptor vd) {
334     // Get the if="" value
335
String JavaDoc ifCheck = getIfCheck();
336     if ((ifCheck == null) || (ifCheck.trim().length() == 0)) {
337         // Default is to allow access
338
return true;
339     }
340
341     // Delegate to permission checker to determine result
342
return new PermissionChecker(ifCheck, vd).hasPermission();
343     }
344
345
346     public static final Class JavaDoc[] EVENT_ARGS = new Class JavaDoc[] {RequestContext.class, HandlerContext.class};
347
348 // FIXME: Make sure we don't need this (consider execution stack instead)
349
// private EventDescriptor _parent = null;
350
private String JavaDoc _name = null;
351     private String JavaDoc _description = null;
352     private String JavaDoc _cls = null;
353     private String JavaDoc _methodName = null;
354     private transient Method JavaDoc _method = null;
355     private String JavaDoc _ifCheck = null;
356     private Map JavaDoc _inputDescriptors = new HashMap JavaDoc(5);
357     private Map JavaDoc _outputDescriptors = new HashMap JavaDoc(5);
358     private List JavaDoc _childHandlers = new ArrayList JavaDoc(5);
359 }
360
Popular Tags