KickJava   Java API By Example, From Geeks To Geeks.

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


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.RequestCompletionListener;
27 import com.iplanet.jato.RequestContext;
28 import com.iplanet.jato.ViewBeanManager;
29 import com.iplanet.jato.view.ContainerView;
30 import com.iplanet.jato.view.View;
31 import com.iplanet.jato.view.ViewBean;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
37 import com.sun.enterprise.tools.guiframework.util.LogUtil;
38 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
39
40
41 public class DescriptorViewManager extends ViewBeanManager {
42
43     /*
44      * Create an instance with the specified request context and module
45      * package name
46      *
47      * @param context The request context for the current request
48      * @param basePackage
49      * The fully qualified package name of the module servlet
50      * creating this object. This package name is used to
51      * later qualify local <code>ViewBean</code> lookups.
52      */

53     public DescriptorViewManager(RequestContext context, String JavaDoc basePackage) {
54     super(context, basePackage);
55     _reqCtx = context; // private in super
56
}
57
58
59     /**
60      * <P>JATO's short-sidedness did not consider that 1 ViewBean can be
61      * associated with more than 1 JSP (with different fields). So they think
62      * that classname is unique... it's not, of course. So we will use "name"
63      * to store a unique identifier for ViewBean type. And so this over-riden
64      * method for getViewBean(String) will NOT expect a class name but a
65      * ViewBean name (since class name will normally be the same in our case).
66      *
67      * <P>This method is internally invoked when using JATO's useViewBean tag.
68      * You should specify the desired ViewBean "name" when using it, not the
69      * class name.
70      *
71      * @param name The name of the ViewBean to get
72      *
73      * @return A ViewBean of the requested type
74      */

75     public ViewBean getViewBean(String JavaDoc name) throws ClassNotFoundException JavaDoc {
76     // Delegate to getView(View parent, String name)
77
ViewBean vb = (ViewBean)getView(null, name);
78
79     // Check to make sure we found the ViewBean
80
if (vb == null) {
81         // Fall back to default JATO way, using class name...
82
try {
83         _useClassName = true;
84         if(name != null && name.endsWith("ViewBean")) {
85             //Strig off the ViewBean part, and the package name part for JATO to handle it correctly.
86
int beginIndex = name.lastIndexOf('.') + 1;
87             int endIndex = name.length() - VB_LEN;
88             name = name.substring(beginIndex, endIndex);
89         }
90         vb = super.getLocalViewBean(name);
91         } catch (ClassNotFoundException JavaDoc ex) {
92         throw new FrameworkException("Unable to locate ViewBean: "+name, ex);
93         } finally {
94         _useClassName = false;
95         }
96
97         // Register it so we always get the same one
98
registerInstance(vb);
99     }
100
101     // Return the ViewBean we created
102
return vb;
103     }
104
105
106     /**
107      * <P>This method is used indirectly in the case where a ViewDescriptor is
108      * not found and the default JATO way of locating a VB kicks in. In this
109      * case, super.getLocalViewBean() is called from the getViewBean(String)
110      * method. Which winds up calling this method after creating the class
111      * name. In general, this method should NOT be invoked directly as it is
112      * the intention of this implemenation of JATO NOT to refer to ViewBeans
113      * by class name. Instead a ViewDescriptor should be declared for every
114      * ViewBean you want to use. The ViewDescriptor's getInstance() method
115      * is capable of creating the desired ViewBean.</P>
116      *
117      * <P>This method is also used directly by the JSP's useViewBean tag. In
118      * this case, we want to do the regular XML lookup. We are using the
119      * _useClassName flag to do the right thing.</P>
120      */

121     public ViewBean getViewBeanByClassName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
122     if (_useClassName) {
123         // Invoke the correct getViewBean(String) method... in this case we
124
// want the super's getViewBean() b/c we are in the process of
125
// falling back to old-school JATO's way of doing things.
126
return super.getViewBean(className);
127     }
128
129     // This request is probably coming from the JSP, do a regular lookup
130
return getViewBean(className);
131     }
132
133
134     /**
135      * This method allows you to get a View (including ViewBean) by supplying
136      * the name. If the requested View is NOT a ViewBean, then you should
137      * supply the parent for the View. The Views are cached per request so
138      * that you are guarenteed to get the same instance if you ask for the
139      * same "name" twice during a request.
140      *
141      * @param parent This is only used for non-ViewBeans, should be the
142      * container of the requested View.
143      * @param name The name of the View you would like to create.
144      *
145      * @return The requested View, or null if no descriptor for the View was
146      * found.
147      */

148     public View getView(View parent, String JavaDoc name) {
149     if (name == null) {
150         throw new IllegalArgumentException JavaDoc("Name cannot be null!");
151     }
152
153     // If we've created it during the request before, use the same one
154
View view = (View)_instances.get(name);
155     if (view != null) {
156         return view;
157     }
158
159     // Get the ViewDescriptorManager
160
ViewDescriptorManager mgr = ViewDescriptorManager.getInstance();
161
162     // mgr.clearCache() here forces reloading on every request
163
//mgr.clearCache();
164

165         // Get the ViewDescriptor from the manager
166
ViewDescriptor desc = mgr.getViewDescriptor(name);
167
168     // Make sure we got it
169
if (desc == null) {
170         if (LogUtil.isLoggable(LogUtil.FINE)) {
171         LogUtil.log(LogUtil.FINE, "framework.getViewDescriptor", name);
172         }
173
174             // This method ONLY handles creating from a ViewDescriptor
175
return null;
176         }
177
178     // Tracing message...
179
if (LogUtil.isLoggable(LogUtil.FINER)) {
180         LogUtil.log(LogUtil.FINER, "trace.gotViewDescriptor", name);
181     }
182
183     return getView(parent, name, desc);
184     }
185
186
187     /**
188      * This method allows you to get a View (including ViewBean) by supplying
189      * the name. If the requested View is NOT a ViewBean, then you should
190      * supply the parent for the View. The Views are cached per request so
191      * that you are guarenteed to get the same instance if you ask for the
192      * same "name" twice during a request.
193      *
194      * @param parent This is only used for non-ViewBeans, should be the
195      * container of the requested View.
196      * @param name The name of the View you would like to create.
197      * @param desc The view descriptor
198      *
199      * @return The requested View
200      */

201     public View getView(View parent, String JavaDoc name, ViewDescriptor desc) {
202     // If we've created it during the request before, use the same one
203
View view = (View)_instances.get(name);
204     if (view != null) {
205         return view;
206     }
207
208     // Make sure we have a ViewDescriptor
209
if (desc == null) {
210         throw new IllegalArgumentException JavaDoc("ViewDescriptor was null!!");
211     }
212
213     // Fire our "beforeCreate" event. This provides a spot for
214
// initializing stuff before we create something. This, of course, is
215
// different than beforeDisplay which happens after the child is
216
// created. This is useful for doing Model initialization-type
217
// operations.
218
DescriptorViewHelper.beforeCreate(_reqCtx,
219         ((parent instanceof DescriptorContainerView) ?
220         (DescriptorContainerView)parent : null), desc);
221
222     // Create a new instance
223
view = desc.getInstance(_reqCtx, (ContainerView)parent, name);
224
225     // Register it so we always get the same one
226
registerInstance(view);
227
228     // Tracing message...
229
if (LogUtil.isLoggable(LogUtil.FINER)) {
230         LogUtil.log(LogUtil.FINER, "trace.createdView",
231         new Object JavaDoc[] {name, view});
232     }
233
234     // Fire our "afterCreate" event. This provides a spot for
235
// initializing stuff after we create something. This is useful for
236
// doing anything you would like to do once for this View per request
237
// and need a reference to the View.
238
DescriptorViewHelper.afterCreate(_reqCtx, view, desc);
239
240     // Return the View we created
241
return view;
242     }
243
244
245     /**
246      * Simply pass the "name" of the ViewBean you want. This works the same
247      * as getViewBean(String).
248      *
249      * @param name The name of the ViewBean to get
250      *
251      * @return A ViewBean of the requested type
252      *
253      * @see #getViewBean(String)
254      */

255     public ViewBean getLocalViewBean(String JavaDoc name) throws ClassNotFoundException JavaDoc {
256     return getViewBean(name);
257     }
258
259
260     /**
261      * This method is not recommended by this implementation (this
262      * implementation DOES NOT store ViewBeans or Views by Class).
263      *
264      * @deprecated Please read the DescriptorViewManager documentation, this
265      * version does not recommend this method. If you really did mean
266      * to get a ViewBean by Class, either declare a descriptor and use
267      * that, or call getViewBean(String) which will fallback to here (not
268      * recommended).
269      */

270     public ViewBean getViewBean(Class JavaDoc cls) {
271     if (_useClassName) {
272         _useClassName = false;
273         return super.getViewBean(cls);
274     }
275     throw new FrameworkException("This method is unsupported!");
276     }
277
278
279     /**
280      * Register the specified view bean with the <code>ViewBeanManager</code>.
281      * Subsequent requests for the same view bean within the current request
282      * will return the registered instance. This method overrides the super
283      * class in order to store the instances by NAME (instead of class). All
284      * ViewBean "names" should be unique (these are defined in the
285      * descriptors). Also, View's are also registered in the same namespace.
286      *
287      *
288      * @param bean The <code>ViewBean</code> to register
289      */

290     public void registerInstance(ViewBean bean) {
291     registerInstance((View)bean);
292     }
293
294
295     /**
296      * This method provides the actual implementation, the ViewBean version
297      * delegates to this method. This allows View's to be stored as well.
298      *
299      * @param view The View to register.
300      */

301     public void registerInstance(View view) {
302     if (!_instances.containsKey(view.getName())) {
303         _instances.put(view.getName(), view);
304         if (view instanceof RequestCompletionListener) {
305         _reqCtx.addRequestCompletionListener((RequestCompletionListener)view);
306         }
307     }
308     }
309
310
311     protected RequestContext _reqCtx;
312     protected boolean _useClassName = false;
313
314     public static final int VB_LEN = "ViewBean".length();
315     private Map JavaDoc _instances = new HashMap JavaDoc();
316 }
317
Popular Tags