KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > event > handlers > AttributeHandlers


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
24 package com.sun.enterprise.tools.guiframework.event.handlers;
25
26 import com.iplanet.jato.RequestContext;
27
28 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
29
30 import java.lang.reflect.Array JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36
37 /**
38  *
39  */

40 public class AttributeHandlers {
41
42     /**
43      * <p> This method retrieves the object stored in an request attribute
44      * under the given key. If the object does not exist, null will be
45      * returned. It requires that a parameter named KEY be passed in via
46      * the parameter Map.</p>
47      *
48      * @param reqCtx The RequestContext
49      * @param handlerCtx The HandlerContext
50      */

51     public void getAttribute(RequestContext reqCtx, HandlerContext handlerCtx) {
52         Object JavaDoc key = handlerCtx.getInputValue(KEY);
53     if (key == null) {
54         throw new IllegalArgumentException JavaDoc(
55         "The parameter map did not contain a key!");
56     }
57     handlerCtx.setOutputValue(
58         VALUE, reqCtx.getRequest().getAttribute(key.toString()));
59     }
60
61
62     /**
63      * This method stores the given value in an attribute. The key under
64      * which the value is to be stored must be specified in the parameter Map
65      * under KEY.
66      *
67      * @param reqCtx The RequestContext
68      * @param handlerCtx The HandlerContext
69      */

70     public void setAttribute(RequestContext reqCtx, HandlerContext handlerCtx) {
71         Object JavaDoc key = handlerCtx.getInputValue(KEY);
72         Object JavaDoc value = handlerCtx.getInputValue(VALUE);
73     if (key == null) {
74         throw new IllegalArgumentException JavaDoc(
75         "The parameter map did not contain a key!");
76     }
77     reqCtx.getRequest().setAttribute(key.toString(), value);
78     }
79
80
81     /**
82      * This method retrieves the value from a List within an attribute. It
83      * requires a parameter named KEY be passed in via the parameter Map to
84      * specify the request attribute. It also requires a parameter named
85      * INDEX that specifies the index within the List to attempt to
86      * retrieve. This Mapping supports List types as well as array types.
87      * The index may be specified as a Integer or String.
88      *
89      * @param reqCtx The RequestContext
90      * @param handlerCtx The HandlerContext
91      */

92     public void getListElement(RequestContext reqCtx, HandlerContext handlerCtx) {
93         // Get the key
94
Object JavaDoc key = handlerCtx.getInputValue(KEY);
95     if (key == null) {
96         throw new IllegalArgumentException JavaDoc(
97         "The parameter map did not contain 'key'!");
98     }
99
100         // Get the index
101
int index = getIndex((Integer JavaDoc)handlerCtx.getInputValue(INDEX));
102
103     // Get the List (or array)
104
Object JavaDoc attr = reqCtx.getRequest().getAttribute(key.toString());
105     if (attr == null) {
106         throw new IllegalArgumentException JavaDoc(
107         "No request attribute is stored under the key: '"+key+"'.");
108     }
109
110     // Check for "List"
111
if (attr instanceof List JavaDoc) {
112         // Set the output value
113
handlerCtx.setOutputValue(VALUE, ((List JavaDoc)attr).get(index));
114         return;
115     }
116
117     // Check for an array
118
if (attr.getClass().isArray()) {
119         // Return the value
120
handlerCtx.setOutputValue(VALUE, Array.get(attr, index));
121         return;
122     }
123
124     // Not an array or List, throw an exception
125
throw new IllegalArgumentException JavaDoc(
126         "Value stored under request attribute '"+key+
127         "' is not an array or List!");
128     }
129
130
131     /**
132      * This method stores the given value in an array or List within an
133      * attribute. The attribute key and the array index must be specified in
134      * the parameter Map under KEY and INDEX respectively. The List or
135      * array will be extended to accomodate INDEX if INDEX is greater than
136      * the current last index. If there is no array or List stored under the
137      * specified attribute, then a new List will be created.
138      *
139      * @param reqCtx The RequestContext
140      * @param handlerCtx The HandlerContext
141      */

142     public void setListElement(RequestContext reqCtx, HandlerContext handlerCtx) {
143         Object JavaDoc value = handlerCtx.getInputValue(VALUE);
144         // Get the key
145
Object JavaDoc key = handlerCtx.getInputValue(KEY);
146     if (key == null) {
147         throw new IllegalArgumentException JavaDoc(
148         "The parameter map did not contain 'key'!");
149     }
150
151         // Get the index
152
int index = getIndex((Integer JavaDoc)handlerCtx.getInputValue(INDEX));
153
154     // Get the List (or array)
155
Object JavaDoc list = reqCtx.getRequest().getAttribute(key.toString());
156     if (list == null) {
157         list = new ArrayList JavaDoc(index);
158     }
159
160     // Check for List...
161
if (list instanceof List JavaDoc) {
162         int len = ((List JavaDoc)list).size();
163         if (index >= len) {
164             // In this case we are growing the List
165
// 1st make sure the List is 1 too short by adding null's
166
Object JavaDoc val = null;
167         while (index > len) {
168             try {
169             try {
170                 ((List JavaDoc)list).add(val);
171             } catch (NullPointerException JavaDoc ex) {
172                 // Some Lists don't support 'null', try ""
173
val = "";
174                 ((List JavaDoc)list).add(val);
175             }
176             } catch (ClassCastException JavaDoc ex2) {
177                 // Some Lists may require certain types... assume that
178
// the 'value' is of the right type and use it
179
val = value;
180             ((List JavaDoc)list).add(val);
181             }
182             len++;
183         }
184
185         // Add 1 more value (the one we want)
186
((List JavaDoc)list).add(value);
187         } else {
188             // In this case we are replacing a value
189
((List JavaDoc)list).set(index, value);
190         }
191
192     // Check for array...
193
} else if (list.getClass().isArray()) {
194         // Make sure index is valid
195
int len = Array.getLength(list);
196         Class JavaDoc type = list.getClass().getComponentType();
197         if (index >= len) {
198         // index is out of bounds, grow array
199
Object JavaDoc newList = Array.newInstance(type, index+1);
200
201         // Copy values from old array to new array
202
for (int count=0; count<len; count++) {
203             Array.set(newList, count, Array.get(list, count));
204         }
205
206         // Use the new (bigger) list
207
list = newList;
208         }
209
210         // Set the value in the array
211
try {
212         Array.set(list, index, value);
213         } catch (Exception JavaDoc ex) {
214         throw new RuntimeException JavaDoc(
215             "Unable to set '"+value+"' to array!", ex);
216         }
217     } else {
218         throw new IllegalArgumentException JavaDoc(
219         "Value stored in request attribute '"+key+
220         "' is not an array or List: '"+list.getClass()+"'.");
221     }
222
223     // Call set setAttribute in case we created in the List or array
224
reqCtx.getRequest().setAttribute(key.toString(), list);
225     }
226
227
228     /**
229      * This method returns the int index value given an Integer
230      * representation of the desired int. If unable to do so, an
231      * IllegalArgumentException may be thrown.
232      *
233      * @param idx The index object to convert to an int
234      *
235      * @param The value of 'idx' as an int
236      */

237     private int getIndex(Integer JavaDoc idx) {
238     if (idx == null) {
239         throw new IllegalArgumentException JavaDoc(
240         "The parameter map did not contain 'index'!");
241     }
242     int index = ((Integer JavaDoc)idx).intValue();
243
244     // Make sure the index is not negative
245
if (index < 0) {
246         throw new IllegalArgumentException JavaDoc(
247         "'index' must be non-negative (received '"+index+"').");
248     }
249
250     // Return the value
251
return index;
252     }
253
254     /**
255      * This method calls a particular method of a given object.
256      *
257      * @param reqCtx The RequestContext
258      * @param handlerCtx The HandlerContext
259      *
260      */

261     public String JavaDoc callMethod(RequestContext reqCtx, HandlerContext handlerCtx) {
262         Object JavaDoc obj = handlerCtx.getInputValue(OBJECT);
263         String JavaDoc methodName = (String JavaDoc)handlerCtx.getInputValue(METHOD);
264         if (obj == null || methodName == null) {
265             throw new IllegalArgumentException JavaDoc("Null parameter(s) sent to 'callMethod'.");
266         }
267         Method JavaDoc method = null;
268         try {
269             method = obj.getClass().getMethod(methodName, (Class JavaDoc[])null);
270         } catch (Exception JavaDoc ex) {
271             throw new RuntimeException JavaDoc("Method not found: " + methodName, ex);
272         }
273         Object JavaDoc value = obj;
274         try {
275             value = method.invoke(obj, (Object JavaDoc[])null);
276         } catch (Exception JavaDoc ex) {
277             throw new RuntimeException JavaDoc("Error calling: " + methodName, ex);
278         }
279         handlerCtx.setOutputValue(VALUE, value);
280         if (value != null)
281             return value.toString();
282         else
283             return "";
284     }
285
286     /**
287      *
288      */

289     public static final String JavaDoc KEY = "key";
290     public static final String JavaDoc VALUE = "value";
291     public static final String JavaDoc INDEX = "index";
292     public static final String JavaDoc OBJECT = "object";
293     public static final String JavaDoc METHOD = "method";
294 }
295
Popular Tags