KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > common > ImplicitObjectUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.common;
19
20 import java.util.Map JavaDoc;
21 import java.util.Collections JavaDoc;
22 import javax.servlet.ServletRequest JavaDoc;
23 import javax.servlet.ServletResponse JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.jsp.JspContext JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.el.VariableResolver JavaDoc;
30
31 import org.apache.beehive.netui.pageflow.FacesBackingBean;
32 import org.apache.beehive.netui.pageflow.GlobalApp;
33 import org.apache.beehive.netui.pageflow.PageFlowController;
34 import org.apache.beehive.netui.pageflow.PageFlowUtils;
35 import org.apache.beehive.netui.pageflow.SharedFlowController;
36 import org.apache.beehive.netui.pageflow.internal.AnyBeanActionForm;
37 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
38 import org.apache.beehive.netui.pageflow.internal.FacesBackingBeanFactory;
39 import org.apache.beehive.netui.script.el.NetUIUpdateVariableResolver;
40 import org.apache.beehive.netui.util.logging.Logger;
41
42 /**
43  *
44  */

45 public class ImplicitObjectUtil {
46
47     private static final Logger LOGGER = Logger.getInstance(ImplicitObjectUtil.class);
48
49     private static final String JavaDoc PAGE_FLOW_IMPLICIT_OBJECT_KEY = "pageFlow";
50     private static final String JavaDoc SHARED_FLOW_IMPLICIT_OBJECT_KEY = "sharedFlow";
51     private static final String JavaDoc GLOBAL_APP_IMPLICIT_OBJECT_KEY = "globalApp";
52     private static final String JavaDoc BUNDLE_IMPLICIT_OBJECT_KEY = "bundle";
53     private static final String JavaDoc BACKING_IMPLICIT_OBJECT_KEY = "backing";
54     private static final String JavaDoc PAGE_INPUT_IMPLICIT_OBJECT_KEY = "pageInput";
55     private static final String JavaDoc ACTION_FORM_IMPLICIT_OBJECT_KEY = "actionForm";
56     private static final String JavaDoc OUTPUT_FORM_BEAN_OBJECT_KEY = "outputFormBean";
57
58     /* do not construct */
59     private ImplicitObjectUtil() {}
60
61     public static final void loadActionForm(JspContext JavaDoc jspContext, Object JavaDoc form) {
62         jspContext.setAttribute(ACTION_FORM_IMPLICIT_OBJECT_KEY, unwrapForm(form));
63     }
64
65     public static final void unloadActionForm(JspContext JavaDoc jspContext) {
66         jspContext.removeAttribute(ACTION_FORM_IMPLICIT_OBJECT_KEY);
67     }
68
69     public static final void loadPageFlow(ServletRequest JavaDoc request, PageFlowController pageFlow, FacesBackingBean fbb) {
70         if(pageFlow != null)
71             request.setAttribute(PAGE_FLOW_IMPLICIT_OBJECT_KEY, pageFlow);
72         if(fbb != null)
73             request.setAttribute(BACKING_IMPLICIT_OBJECT_KEY, fbb);
74
75         Map JavaDoc map = InternalUtils.getPageInputMap(request);
76         request.setAttribute(PAGE_INPUT_IMPLICIT_OBJECT_KEY, map != null ? map : Collections.EMPTY_MAP);
77     }
78
79     public static final void loadSharedFlow(ServletRequest JavaDoc request, Map JavaDoc/*<String, SharedFlowController>*/ sharedFlows) {
80         if(sharedFlows != null)
81             request.setAttribute(SHARED_FLOW_IMPLICIT_OBJECT_KEY, sharedFlows);
82     }
83
84     public static final void loadGlobalApp(ServletRequest JavaDoc request, GlobalApp globalApp) {
85         if(globalApp != null)
86             request.setAttribute(GLOBAL_APP_IMPLICIT_OBJECT_KEY, globalApp);
87     }
88
89     public static final void loadBundleMap(ServletRequest JavaDoc servletRequest, BundleMap bundleMap) {
90         servletRequest.setAttribute(BUNDLE_IMPLICIT_OBJECT_KEY, bundleMap);
91     }
92
93     public static final Object JavaDoc unwrapForm(Object JavaDoc form) {
94         if(LOGGER.isDebugEnabled() && form instanceof AnyBeanActionForm)
95             LOGGER.debug("using form of type: " + ((AnyBeanActionForm)form != null ? ((AnyBeanActionForm)form).getClass().getName() : "null"));
96
97         if(form instanceof AnyBeanActionForm)
98             return ((AnyBeanActionForm)form).getBean();
99         else
100             return form;
101     }
102
103     public static final Map JavaDoc/*<String, SharedFlowController>*/ getSharedFlow(ServletRequest JavaDoc request) {
104         return (Map JavaDoc/*<String, SharedFlowController>*/)request.getAttribute(SHARED_FLOW_IMPLICIT_OBJECT_KEY);
105     }
106
107     public static final PageFlowController getPageFlow(ServletRequest JavaDoc request, ServletResponse JavaDoc response) {
108         assert request instanceof HttpServletRequest JavaDoc;
109
110         PageFlowController jpf = PageFlowUtils.getCurrentPageFlow((HttpServletRequest JavaDoc)request);
111         if(jpf != null)
112             return jpf;
113         else {
114             // @todo: i18n
115
RuntimeException JavaDoc re = new RuntimeException JavaDoc("There is no current PageFlow for the expression.");
116             if(LOGGER.isErrorEnabled()) LOGGER.error("", re);
117             throw re;
118         }
119     }
120
121     public static final GlobalApp getGlobalApp(ServletRequest JavaDoc request, ServletResponse JavaDoc response) {
122         assert request instanceof HttpServletRequest JavaDoc;
123
124         GlobalApp ga = PageFlowUtils.getGlobalApp((HttpServletRequest JavaDoc)request);
125
126         if(ga == null) {
127             // @todo: i18n
128
RuntimeException JavaDoc re = new RuntimeException JavaDoc("Can not create the globalApp binding context; the GlobalApp object is null.");
129             if(LOGGER.isErrorEnabled()) LOGGER.error("", re);
130             throw re;
131         }
132
133         return ga;
134     }
135
136     /**
137      *
138      */

139     public static final VariableResolver JavaDoc getUpdateVariableResolver(ServletRequest JavaDoc request, ServletResponse JavaDoc response, boolean isHandlingPost) {
140         Object JavaDoc form = ImplicitObjectUtil.unwrapForm(request.getAttribute(org.apache.struts.taglib.html.Constants.BEAN_KEY));
141
142         /* todo: need to provide get(Read|Update)VariableResolver methods on the ExpressionEngineFactory */
143         return new NetUIUpdateVariableResolver(form, request, response, isHandlingPost);
144     }
145
146     public static final VariableResolver JavaDoc getUpdateVariableResolver(Object JavaDoc form, ServletRequest JavaDoc request, ServletResponse JavaDoc response, boolean isHandlingPost) {
147         Object JavaDoc realForm = ImplicitObjectUtil.unwrapForm(form);
148
149         /* todo: need to provide get(Read|Update)VariableResolver methods on the ExpressionEngineFactory */
150         return new NetUIUpdateVariableResolver(realForm, request, response, isHandlingPost);
151     }
152
153     public static final VariableResolver JavaDoc getReadVariableResolver(PageContext JavaDoc pageContext) {
154         assert pageContext != null;
155         return pageContext.getVariableResolver();
156     }
157
158     public static final void loadImplicitObjects(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext, PageFlowController curJpf) {
159         // @todo: need to wrap this in checks for JSP 1.2
160
// @todo: feature: need to add support for chaining in user-code to run when setting implicit objects on the request
161
FacesBackingBean fbb =
162             FacesBackingBeanFactory.getFacesBackingBeanForRequest(request, response, servletContext);
163         loadPageFlow(request, curJpf, fbb);
164         
165         // @todo: need to move bundleMap creation to a BundleMapFactory
166
BundleMap bundleMap = new BundleMap(request, servletContext);
167         loadBundleMap(request, bundleMap);
168     }
169
170     public static final void loadOutputFormBean(ServletRequest JavaDoc request, Object JavaDoc bean) {
171         if(bean != null)
172             request.setAttribute(OUTPUT_FORM_BEAN_OBJECT_KEY, bean);
173     }
174 }
175
Popular Tags