KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > view > HandlerContextImpl


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.view;
25
26 import com.iplanet.jato.RequestManager;
27 import com.iplanet.jato.view.View;
28 import com.iplanet.jato.view.ViewBean;
29 import com.iplanet.jato.util.TypeConverter;
30
31 import com.sun.enterprise.tools.guiframework.event.descriptors.HandlerDescriptor;
32 import com.sun.enterprise.tools.guiframework.event.descriptors.IODescriptor;
33 import com.sun.enterprise.tools.guiframework.event.descriptors.UseHandlerDescriptor;
34 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
35 import com.sun.enterprise.tools.guiframework.util.Util;
36 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
37 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
38
39 import java.io.Serializable JavaDoc;
40 import java.util.EventObject JavaDoc;
41
42
43 /**
44  *
45  */

46 public class HandlerContextImpl implements HandlerContext {
47
48     /**
49      * Constructor
50      */

51     protected HandlerContextImpl(UseHandlerDescriptor useHandler, HandlerDescriptor handler, View view, ViewDescriptor vd, EventObject JavaDoc event) {
52     _eventObject = event;
53     _useHandlerDesc = useHandler;
54     _handlerDesc = handler;
55     _view = view;
56     _viewDesc = vd;
57     }
58
59
60     /**
61      * This returns the nearest View associated with the handler.
62      */

63     public View getView() {
64     return _view;
65     }
66
67
68     /**
69      * This method returns the nearest ViewDescriptor associated with the
70      * handler.
71      */

72     public ViewDescriptor getViewDescriptor() {
73     return _viewDesc;
74     /*
75     if (_viewDesc != null) {
76         return _viewDesc;
77     }
78
79     // First find a DescriptorContainerView so we can get a ViewDescriptor
80     View view = getView();
81     View tmp = view;
82     String lastName = "";
83     while ((tmp != null) && !(tmp instanceof DescriptorContainerView)) {
84         lastName = tmp.getName();
85         tmp = tmp.getParent();
86     }
87     if (tmp == null) {
88         // Didn't find one, return null
89         return null;
90     }
91
92     // Pull off a ViewDescriptor
93     _viewDesc = ((DescriptorContainerView)tmp).getViewDescriptor();
94
95     // Unless we have a Descriptor for 'view', try the child descriptor
96     if ((tmp != view) && (_viewDesc.getChildDescriptor(lastName) != null)) {
97         _viewDesc = _viewDesc.getChildDescriptor(lastName);
98     }
99
100     // Return the closest ViewDescriptor
101     return _viewDesc;
102     */

103     }
104
105
106     /**
107      * This method returns the Event that caused the handler to be invoked.
108      */

109     public EventObject JavaDoc getEvent() {
110     return _eventObject;
111     }
112
113
114     /**
115      * This method returns the UseHandlerDescriptor associated with the
116      * handler. The UseHandlerDescriptor can be thought of the instance
117      * of the handler that is being invoked. It contains the actual input
118      * parameters to sent to the handler (if any), and a reference to the
119      * handler definition (HandlerDescriptor).
120      */

121     public UseHandlerDescriptor getUseHandlerDescriptor() {
122     return _useHandlerDesc;
123     }
124
125
126     /**
127      * This method returns the HandlerDescriptor associated with the handler.
128      * The handler descriptor can be thought of the definition to the handler
129      * itself. It contains the method, I/O definition, and may also define
130      * other UseHandlers to be invoked prior to executing itself.
131      */

132     public HandlerDescriptor getHandlerDescriptor() {
133     return _handlerDesc;
134     }
135
136
137     /**
138      * This method returns the input value for the given input name. The name
139      * must be valid or an exception will be thrown. This method WILL parse
140      * the value for "$()"; if you don't want this to happen, you can get the
141      * value directly from the UseHandlerDescriptor.
142      */

143     public Object JavaDoc getInputValue(String JavaDoc name) {
144     // Make sure the requested name is valid
145
IODescriptor inDesc = getHandlerDescriptor().getInputDescriptor(name);
146     if (inDesc == null) {
147         throw new FrameworkException("Attempted to get input value '"+name+
148         "', however, this is not a declared input parameter in "+
149         "function '"+getHandlerDescriptor().getName()+"'! Check your"+
150         " handler and/or the XML (near ViewDescriptor '"+
151         getViewDescriptor().getName()+"').", getViewDescriptor(),
152         getView());
153     }
154
155     // Get the value, and parse it
156
Object JavaDoc value = getUseHandlerDescriptor().getInputValue(name);
157     if (value == null) {
158         value = inDesc.getDefault();
159     }
160     value = Util.replaceVariablesWithAttributes(value, getViewDescriptor());
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     /**
170      * This method sets the specified output value. The name must be valid or
171      * an exception will be thrown. Output values are currently stored as
172      * request attributes. The key will be the HandlerDescriptor's name +
173      * OUTPUT_VALUE_SEPARATOR + name (i.e. "handlerName.outputParamName").
174      *
175      * @param name The name of the output variable
176      * @param value The value of the output variable (may be converted to
177      * the correct type if not already correct).
178      */

179     public void setOutputValue(String JavaDoc name, Object JavaDoc value) {
180     // Make sure the requested name is valid
181
HandlerDescriptor handler = getHandlerDescriptor();
182     IODescriptor outDesc = handler.getOutputDescriptor(name);
183     if (outDesc == null) {
184         throw new FrameworkException("Attempted to set output value '"+name+
185         "' from handler '"+handler.getName()+"', however, this is not "+
186         "a declared output parameter! Check your handler and/or the "+
187         "XML.", getViewDescriptor(), getView());
188     }
189
190     // Make sure the value is the correct type...
191
value = TypeConverter.asType(outDesc.getType(), value);
192
193     // Store the value as requested (or use default)
194
String JavaDoc targetKey;
195     String JavaDoc targetType;
196     String JavaDoc outputParams[] = getUseHandlerDescriptor().getOutputMapping(name);
197     if (outputParams == null) {
198         targetKey = handler.getName()+OUTPUT_VALUE_SEPARATOR+name;
199         targetType = ATTRIBUTE_TYPE;
200     } else {
201         targetKey = outputParams[0];
202         if (targetKey == null) {
203         targetKey = handler.getName()+OUTPUT_VALUE_SEPARATOR+name;
204         }
205         targetType = outputParams[1];
206         if (targetType == null) {
207         targetType = ATTRIBUTE_TYPE;
208         }
209     }
210     if (targetType.equalsIgnoreCase(ATTRIBUTE_TYPE)) {
211         RequestManager.getRequestContext().getRequest().
212         setAttribute(targetKey, value);
213     } else if (targetType.equalsIgnoreCase(SESSION_TYPE)) {
214         RequestManager.getRequestContext().getRequest().getSession().
215         setAttribute(targetKey, value);
216     } else if (targetType.equalsIgnoreCase(PAGE_SESSION_TYPE)) {
217         ViewBean vb = Util.getParentViewBean(getView());
218         vb.setPageSessionAttribute(targetKey, (Serializable JavaDoc)value);
219     } else {
220         throw new FrameworkException("Invalid target type '"+targetKey+
221         "' used for assigning output from function '"+
222         handler.getName()+"'.", getViewDescriptor(), getView());
223     }
224     }
225
226
227     /**
228      * This method retrieves an output value that has been set.
229      */

230     public Object JavaDoc getOutputValue(String JavaDoc name) {
231     // Make sure the requested name is valid
232
HandlerDescriptor handler = getHandlerDescriptor();
233     IODescriptor outDesc = handler.getOutputDescriptor(name);
234     if (outDesc == null) {
235         throw new FrameworkException("Attempted to get output value '"+name+
236         "' from handler '"+handler.getName()+"', however, this is not "+
237         "a declared output parameter! Check your handler and/or the "+
238         "XML.", getViewDescriptor(), getView());
239     }
240
241     // Find the targetKey/targetType (or use default)
242
String JavaDoc targetKey;
243     String JavaDoc targetType;
244     String JavaDoc outputParams[] =
245         getUseHandlerDescriptor().getOutputMapping(name);
246     if (outputParams == null) {
247         targetKey = handler.getName()+OUTPUT_VALUE_SEPARATOR+name;
248         targetType = ATTRIBUTE_TYPE;
249     } else {
250         targetKey = outputParams[0];
251         if (targetKey == null) {
252         targetKey = handler.getName()+OUTPUT_VALUE_SEPARATOR+name;
253         }
254         targetType = outputParams[1];
255         if (targetType == null) {
256         targetType = ATTRIBUTE_TYPE;
257         }
258     }
259
260     // Get the requested output value
261
Object JavaDoc value;
262     if (targetType.equalsIgnoreCase(ATTRIBUTE_TYPE)) {
263         value = RequestManager.getRequestContext().getRequest().
264         getAttribute(targetKey);
265     } else if (targetType.equalsIgnoreCase(SESSION_TYPE)) {
266         value = RequestManager.getRequestContext().getRequest().
267         getSession().getAttribute(targetKey);
268     } else if (targetType.equalsIgnoreCase(PAGE_SESSION_TYPE)) {
269         ViewBean vb = Util.getParentViewBean(getView());
270         value = vb.getPageSessionAttribute(targetKey);
271     } else {
272         throw new FrameworkException("Invalid target type '"+targetKey+
273         "' used for assigning output from function '"+
274         handler.getName()+"'.", getViewDescriptor(), getView());
275     }
276     return value;
277     }
278
279
280     public static final String JavaDoc OUTPUT_VALUE_SEPARATOR = ".";
281     public static final String JavaDoc ATTRIBUTE_TYPE = "attribute";
282     public static final String JavaDoc SESSION_TYPE = "session";
283     public static final String JavaDoc PAGE_SESSION_TYPE = "pageSession";
284     
285
286     private EventObject JavaDoc _eventObject = null;
287     private HandlerDescriptor _handlerDesc = null;
288     private UseHandlerDescriptor _useHandlerDesc = null;
289     private View _view = null;
290     private ViewDescriptor _viewDesc = null;
291 }
292
Popular Tags