KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.iplanet.jato.ViewBeanManager;
28 import com.iplanet.jato.view.DisplayField;
29 import com.iplanet.jato.view.ContainerView;
30 import com.iplanet.jato.view.View;
31 import com.iplanet.jato.view.ViewBean;
32 import com.iplanet.jato.view.ViewBeanBase;
33
34 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
35 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
36 import com.sun.enterprise.tools.guiframework.view.DescriptorViewManager;
37
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41
42
43 /**
44  * This class defines both Source and Target mappings to and from a
45  * DisplayField.
46  */

47 public class SessionHandlers {
48
49     /**
50      * <p> This handler retrieves the requested page session value. It
51      * requires an input parameter named {@link #NAME} be provided to
52      * specify the page session name. You may optionally pass in the
53      * <code>ViewBean</code> to use via the input parameter
54      * {@link #VIEW_BEAN}.</p>
55      *
56      * @param reqCtx The RequestContext
57      * @param handlerCtx The HandlerContext
58      */

59     public void getPageSessionValue(RequestContext reqCtx, HandlerContext handlerCtx) {
60         String JavaDoc name = (String JavaDoc)handlerCtx.getInputValue(NAME);
61     if (name == null) {
62         throw new FrameworkException("'"+NAME+
63         "' specifying the PageSession name must be provided!");
64     }
65
66     ViewBean vb = (ViewBean)handlerCtx.getInputValue(VIEW_BEAN);
67     if (vb == null) {
68         vb = getViewBean(reqCtx, handlerCtx);
69     }
70
71     // Set the output value
72
handlerCtx.setOutputValue(VALUE, vb.getPageSessionAttribute(name));
73     }
74
75
76     /**
77      * <p> This is a helper method for obtaining a View.</p>
78      */

79     private ViewBean getViewBean(RequestContext reqCtx, HandlerContext handlerCtx) {
80         // Get the View ID
81
Object JavaDoc viewID = handlerCtx.getInputValue(VIEW_ID);
82     if (viewID == null) {
83         throw new IllegalArgumentException JavaDoc(
84         "The parameter map did not contain "+VIEW_ID+"!");
85     }
86
87     // Try to obtain the ContainerView
88
ViewBeanManager vm = reqCtx.getViewBeanManager();
89     ViewBean vb = null;
90     String JavaDoc topName = (viewID instanceof List JavaDoc) ?
91         ((List JavaDoc)viewID).get(0).toString() : viewID.toString();
92     try {
93         vb = vm.getViewBean(topName);
94     } catch (ClassCastException JavaDoc ex) {
95         vb = (ViewBean)((DescriptorViewManager)vm).getView(null, topName);
96     } catch (ClassNotFoundException JavaDoc ex) {
97         throw new RuntimeException JavaDoc(ex);
98     }
99     if (vb == null) {
100         throw new RuntimeException JavaDoc(
101         "Unable to obtain the view for mapping!");
102     }
103
104     // Set the output value
105
return vb;
106     }
107
108
109     /**
110      * This method sets a DisplayField value. "params" must contain the View
111      * key and the DisplayField name. The key for the View is "viewID", the
112      * key for the DisplayField is "fieldName". You may optionally specify
113      * that you want an array of values. To do this, pass in the parameters
114      * "multiple" equal to "true". Note, if you are obtaining a View (not a
115      * ViewBean), then the View must be cached in order for this to work
116      * correctly (the container will not get set correctly). If you attmempt
117      * to set an array of values, but do not pass in an array type, an
118      * Object[] will be created with the value as its only emelent.
119      *
120      * @param reqCtx The RequestContext
121      * @param handlerCtx The HandlerContext
122      */

123     public void setPageSessionValue(RequestContext reqCtx, HandlerContext handlerCtx) {
124         String JavaDoc name = (String JavaDoc)handlerCtx.getInputValue(NAME);
125     if (name == null) {
126         throw new FrameworkException("'"+NAME+
127         "' specifying the PageSession name must be provided!");
128     }
129
130     ViewBean vb = (ViewBean)handlerCtx.getInputValue(VIEW_BEAN);
131     if (vb == null) {
132         vb = getViewBean(reqCtx, handlerCtx);
133     }
134
135     Object JavaDoc value = handlerCtx.getInputValue(VALUE);
136         vb.setPageSessionAttribute(name, (java.io.Serializable JavaDoc)value);
137     }
138
139
140     /**
141      * <p> This method retrieves the value from session. It requires that a
142      * parameter named {@link #KEY} be passed in via the parameter Map. It
143      * sets the value of the session key in output parameter
144      * {@link #VALUE}.</p>
145      *
146      * @param reqCtx The RequestContext
147      * @param handlerCtx The HandlerContext
148      */

149     public void getSessionValue(RequestContext reqCtx, HandlerContext handlerCtx) {
150         Object JavaDoc key = handlerCtx.getInputValue(KEY);
151     if (key == null) {
152         throw new IllegalArgumentException JavaDoc(
153         "The parameter map did not contain a key!");
154     }
155
156     // Set the output value
157
handlerCtx.setOutputValue(VALUE,
158         reqCtx.getRequest().getSession().getAttribute(key.toString()));
159     }
160
161
162     /**
163      * This method stores the given value in session. The key under which the
164      * value is to be stored must be specified in the parameter Map under KEY.
165      *
166      * @param reqCtx The RequestContext
167      * @param handlerCtx The HandlerContext
168      */

169     public void setSessionValue(RequestContext reqCtx, HandlerContext handlerCtx) {
170     Object JavaDoc value = handlerCtx.getInputValue(VALUE);
171     Object JavaDoc key = handlerCtx.getInputValue(KEY);
172     if (key == null) {
173         throw new IllegalArgumentException JavaDoc(
174         "The parameter map did not contain a key!");
175     }
176     reqCtx.getRequest().getSession().setAttribute(key.toString(), value);
177     }
178
179
180     /**
181      * ('viewBean').
182      */

183     public static final String JavaDoc VIEW_BEAN = "viewBean";
184
185     /**
186      * ('name').
187      */

188     public static final String JavaDoc NAME = "name";
189
190     /**
191      * ('viewID').
192      */

193     public static final String JavaDoc VIEW_ID = "viewID";
194
195     /**
196      * ('key').
197      */

198     public static final String JavaDoc KEY = "key";
199
200     /**
201      * ('value').
202      */

203     public static final String JavaDoc VALUE = "value";
204 }
205
Popular Tags