KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.tools.jsfext.component.ComponentUtil;
26 import com.sun.enterprise.tools.jsfext.event.UIComponentHolder;
27 import com.sun.enterprise.tools.jsfext.util.TypeConverter;
28
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.EventObject JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.faces.component.UIComponent;
36
37
38 /**
39  * <p> This class contains the information necessary to invoke a Handler. The
40  * {@link HandlerDefinition} class provides a definition of how to invoke
41  * a Handler, this class uses that information with in conjuction with
42  * information provided in this class to execute the <strong>handler
43  * method</strong>. This class typically will hold input values and
44  * specify where output should be stored.</p>
45  *
46  * <p> The <strong>handler method</strong> to be invoked must have the
47  * following method signature:</p>
48  *
49  * <p> <BLOCKQUOTE>
50  * </CODE>
51  * public void beginDisplay(HandlerContext handlerCtx)
52  * </CODE>
53  * </BLOCKQUOTE></p>
54  *
55  * <p> <code>void</code> above can return a value. Depending on the type of
56  * event, return values may be handled differently.</p>
57  *
58  * @author Ken Paulsen (ken.paulsen@sun.com)
59  */

60 public class Handler implements java.io.Serializable JavaDoc {
61
62     /**
63      * <p> Constructor.</p>
64      */

65     public Handler(HandlerDefinition handlerDef) {
66     setHandlerDefinition(handlerDef);
67     }
68
69     /**
70      * <p> Accessor for the {@link HandlerDefinition}.</p>
71      */

72     public HandlerDefinition getHandlerDefinition() {
73     return _handlerDef;
74     }
75
76     /**
77      * <p> This method sets the HandlerDefinition used by this Handler.</p>
78      */

79     protected void setHandlerDefinition(HandlerDefinition handler) {
80     _handlerDef = handler;
81     }
82
83     /**
84      *
85      */

86     public void setInputValue(String JavaDoc name, Object JavaDoc value) {
87     _inputs.put(name, value);
88     }
89
90     /**
91      * <p> This method returns a Map of NVPs representing the input to this
92      * handler.</p>
93      */

94     protected Map JavaDoc getInputMap() {
95     return _inputs;
96     }
97
98     /**
99      * <p> This method simply returns the named input value, null if not
100      * found. It will not attempt to resolve $...{...} expressions or
101      * do modifications of any kind. If you are looking for a method to
102      * do these types of operations, try:</p>
103      *
104      * getInputValue(FacesContext, String).
105      *
106      * @param name The name used to identify the input value.
107      */

108     public Object JavaDoc getInputValue(String JavaDoc name) {
109     return _inputs.get(name);
110     }
111
112     /**
113      * <p> This method returns the value for the named input. Input values
114      * are not stored in this HandlerContext itself, but in the Handler.
115      * If you are trying to set input values for a handler, you must
116      * create a new Handler object and set its input values.</p>
117      *
118      * <p> This method attempts to resolve $...{...} expressions. It also
119      * will return the default value if the value is null. If you don't
120      * want these things to happen, look at
121      * Handler.getInputValue(String).</p>
122      *
123      * @param name The input name
124      *
125      * @return The value of the input (null if not found)
126      */

127     public Object JavaDoc getInputValue(HandlerContext ctx, String JavaDoc name) {
128     // Make sure the requested name is valid
129
IODescriptor inDesc = getHandlerDefinition().getInputDef(name);
130     if (inDesc == null) {
131         throw new RuntimeException JavaDoc("Attempted to get input value '"
132             + name + "', however, this is not a declared input "
133             + "parameter in handler definition '"
134             + getHandlerDefinition().getId() + "'! Check your handler "
135             + " and/or the XML (near LayoutElement '"
136             + ctx.getLayoutElement().getId(ctx.getFacesContext(), null)
137             + "')");
138     }
139
140     // Get the value, and parse it
141
Object JavaDoc value = getInputValue(name);
142     if (value == null) {
143         if (inDesc.isRequired()) {
144         throw new RuntimeException JavaDoc("'" + name
145             + "' is required for handler '"
146             + getHandlerDefinition().getId() + "'!");
147         }
148         value = inDesc.getDefault();
149     }
150
151     // Resolve any expressions
152
EventObject JavaDoc event = ctx.getEventObject();
153     UIComponent component = null;
154     if (event instanceof UIComponentHolder) {
155         component = ((UIComponentHolder) event).getUIComponent();
156     }
157     if ((value != null) && (value instanceof String JavaDoc)) {
158         value = ComponentUtil.resolveValue(ctx.getFacesContext(),
159             ctx.getLayoutElement(), component, "" + value);
160     }
161
162     // Make sure the value is the correct type...
163
value = TypeConverter.asType(inDesc.getType(), value);
164
165     return value;
166     }
167
168     /**
169      * <p> This method retrieves an output value. Output values are stored
170      * in the location specified by the OutputType in the Handler.</p>
171      *
172      * @param context The HandlerContext
173      * @param name The output name
174      *
175      * @return The value of the output (null if not set)
176      */

177     public Object JavaDoc getOutputValue(HandlerContext context, String JavaDoc name) {
178     // Make sure the requested name is valid
179
HandlerDefinition handlerDef = getHandlerDefinition();
180     IODescriptor outIODesc = handlerDef.getOutputDef(name);
181     if (outIODesc == null) {
182         throw new RuntimeException JavaDoc("Attempted to get output value '"
183             + name + "' from handler '" + handlerDef.getId()
184             + "', however, this is not a declared output parameter! "
185             + "Check your handler and/or the XML.");
186     }
187
188     // Get the OutputMapping that describes how to store this output
189
OutputMapping outputDesc = getOutput(name);
190
191     // Return the value
192
return outputDesc.getOutputType().
193         getValue(context, outIODesc, outputDesc.getOutputKey());
194     }
195
196     /**
197      * <p> This method stores an output value. Output values are stored
198      * as specified by the OutputType in the Handler.</p>
199      *
200      * @param context The HandlerContext
201      * @param name The name the Handler uses for the output
202      * @param value The value to set
203      */

204     public void setOutputValue(HandlerContext context, String JavaDoc name, Object JavaDoc value) {
205     // Make sure the requested name is valid
206
HandlerDefinition handlerDef = getHandlerDefinition();
207     IODescriptor outIODesc = handlerDef.getOutputDef(name);
208     if (outIODesc == null) {
209         throw new RuntimeException JavaDoc("Attempted to set output value '"
210             + name + "' from handler '" + handlerDef.getId()
211             + "', however, this is not a declared output parameter! "
212             + "Check your handler and/or the XML.");
213     }
214
215     // Get the OutputMapping that describes how to store this output
216
OutputMapping outputMapping = getOutput(name);
217     if (outputMapping == null) {
218         // They did not Map the output, do nothing...
219
return;
220     }
221
222     // Make sure the value is the correct type...
223
value = TypeConverter.asType(outIODesc.getType(), value);
224
225     // Set the value
226
EventObject JavaDoc event = context.getEventObject();
227     UIComponent component = null;
228     if (event instanceof UIComponentHolder) {
229         component = ((UIComponentHolder) event).getUIComponent();
230     }
231     outputMapping.getOutputType().setValue(
232         context, outIODesc, "" + ComponentUtil.resolveValue(
233         context.getFacesContext(),
234         context.getLayoutElement(),
235         component,
236         outputMapping.getOutputKey()), value);
237     }
238
239     /**
240      * <p> This method adds a new OutputMapping to this handler. An
241      * OutputMapping allows the handler to return a value and have it
242      * "mapped" to the location of your choice. The "outputType"
243      * corresponds to a registered OutputType (see OutputTypeManager).</p>
244      *
245      * @param outputName The Handler's name for the output value
246      * @param targetKey The 'key' the OutputType uses to store the output
247      * @param targetType The OutputType implementation map the output
248      */

249     public void setOutputMapping(String JavaDoc outputName, String JavaDoc targetKey, String JavaDoc targetType) {
250     // Ensure the data is trim
251
if (targetKey != null) {
252         targetKey = targetKey.trim();
253         if (targetKey.length() == 0) {
254         targetKey = null;
255         }
256     }
257     targetType = targetType.trim();
258
259     try {
260         _outputs.put(outputName, new OutputMapping(
261             outputName, targetKey, targetType));
262     } catch (IllegalArgumentException JavaDoc ex) {
263         throw new RuntimeException JavaDoc(
264         "Unable to create OutputMapping with given information: "
265         + "outputName='" + outputName
266         + "', targetKey='" + targetKey
267         + "', targetType=" + targetType + "'", ex);
268     }
269     }
270
271     /**
272      *
273      */

274     public OutputMapping getOutput(String JavaDoc name) {
275     return (OutputMapping) _outputs.get(name);
276     }
277
278     /**
279      * <p> This method determines if the handler is static.</p>
280      */

281     public boolean isStatic() {
282     return getHandlerDefinition().isStatic();
283     }
284
285     /**
286      *
287      */

288     public Object JavaDoc invoke(HandlerContext handlerContext) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
289     Object JavaDoc retVal = null;
290     HandlerDefinition handlerDef = getHandlerDefinition();
291     Method JavaDoc method = handlerDef.getHandlerMethod();
292
293     // First execute all child handlers
294
// A copy is provided of the HandlerContext to avoid the Handler being
295
// changed before we execute this Handler.
296
Object JavaDoc result = handlerContext.getLayoutElement().dispatchHandlers(
297         new HandlerContextImpl(handlerContext),
298         handlerDef.getChildHandlers());
299
300     // Only attempt to do this if there is a handler method, there
301
// might only be child handlers
302
if (method != null) {
303         Object JavaDoc instance = null;
304         if (!isStatic()) {
305         // Get the class that contains the method
306
instance = method.getDeclaringClass().newInstance();
307         }
308
309         // Invoke the Method
310
retVal = method.invoke(instance, new Object JavaDoc[] {handlerContext});
311         if (retVal != null) {
312         result = retVal;
313         }
314     }
315
316     // Return the result (null if no result)
317
return result;
318     }
319
320
321     private HandlerDefinition _handlerDef = null;
322     private Map JavaDoc _inputs = new HashMap JavaDoc();
323     private Map JavaDoc _outputs = new HashMap JavaDoc();
324 }
325
Popular Tags