KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > FacesBackingBean


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.pageflow;
19
20 import org.apache.beehive.netui.pageflow.internal.InternalConstants;
21 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
22 import org.apache.beehive.netui.pageflow.internal.CachedFacesBackingInfo;
23 import org.apache.beehive.netui.pageflow.internal.AdapterManager;
24 import org.apache.beehive.netui.pageflow.internal.CachedSharedFlowRefInfo;
25 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
26 import org.apache.beehive.netui.util.internal.cache.ClassLevelCache;
27 import org.apache.beehive.netui.util.logging.Logger;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.lang.reflect.Field JavaDoc;
35
36 /**
37  * <p>
38  * A JavaServer Faces backing bean. An instance of this class will be created whenever a corresponding JSF path is
39  * requested (e.g., an instance of foo.MyPage will be created for the webapp-relative path "/foo/MyPage.faces"). The
40  * instance will be released (removed from the user session) when a non-matching path is requested. A faces backing
41  * bean can hold component references and event/command handlers, and it can raise actions with normal JSF command event
42  * handlers that are annotated with {@link org.apache.beehive.netui.pageflow.annotations.Jpf.CommandHandler &#64;Jpf.CommandHandler}.
43  * The bean instance can be bound to with a JSF-style expression like <code>#{backing.myComponent}</code>.
44  * </p>
45  * <p>
46  * JSF backing beans are configured using the
47  * {@link org.apache.beehive.netui.pageflow.annotations.Jpf.FacesBacking &#64;Jpf.FacesBacking} annotation.
48  * </p>
49  */

50 public abstract class FacesBackingBean
51         extends PageFlowManagedObject
52 {
53     private static final String JavaDoc CACHED_INFO_KEY = "cachedInfo";
54     private static final Logger _log = Logger.getInstance( FacesBackingBean.class );
55     
56     private Map JavaDoc _pageInputs;
57     
58     
59     /**
60      * Store this object in the user session, in the appropriate place. Used by the framework; normally should not be
61      * called directly.
62      */

63     public void persistInSession( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
64     {
65         HttpServletRequest JavaDoc unwrappedRequest = PageFlowUtils.unwrapMultipart( request );
66         ScopedServletUtils.setScopedSessionAttr( InternalConstants.FACES_BACKING_ATTR, this, unwrappedRequest );
67     }
68
69     /**
70      * Remove this instance from the session.
71      */

72     protected void removeFromSession( HttpServletRequest JavaDoc request )
73     {
74         HttpServletRequest JavaDoc unwrappedRequest = PageFlowUtils.unwrapMultipart( request );
75         ScopedServletUtils.removeScopedSessionAttr( InternalConstants.FACES_BACKING_ATTR, unwrappedRequest );
76     }
77
78     /**
79      * Ensures that any changes to this object will be replicated in a cluster (for failover),
80      * even if the replication scheme uses a change-detection algorithm that relies on
81      * HttpSession.setAttribute to be aware of changes. Note that this method is used by the framework
82      * and does not need to be called explicitly in most cases.
83      *
84      * @param request the current HttpServletRequest
85      */

86     public void ensureFailover( HttpServletRequest JavaDoc request )
87     {
88         HttpServletRequest JavaDoc unwrappedRequest = PageFlowUtils.unwrapMultipart( request );
89         String JavaDoc attr =
90                 ScopedServletUtils.getScopedSessionAttrName( InternalConstants.FACES_BACKING_ATTR, unwrappedRequest );
91         ServletContext JavaDoc servletContext = InternalUtils.getServletContext( request );
92         AdapterManager.getServletContainerAdapter( servletContext ).ensureFailover( attr, this, unwrappedRequest );
93     }
94
95     /**
96      * Get the display name for the bean. For FacesBackingBeans, this is simply the class name.
97      */

98     public String JavaDoc getDisplayName()
99     {
100         return getClass().getName();
101     }
102
103     /**
104      * Reinitialize the bean for a new request. Used by the framework; normally should not be called directly.
105      */

106     public void reinitialize( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext )
107     {
108         super.reinitialize( request, response, servletContext );
109         
110         if ( _pageInputs == null )
111         {
112             Map JavaDoc map = InternalUtils.getActionOutputMap( request, false );
113             if ( map != null ) _pageInputs = Collections.unmodifiableMap( map );
114         }
115         
116         //
117
// Initialize the page flow field.
118
//
119
Field JavaDoc pageFlowMemberField = getCachedInfo().getPageFlowMemberField();
120         
121         // TODO: should we add a compiler warning if this field isn't transient? All this reinitialization logic is
122
// for the transient case.
123
if ( fieldIsUninitialized( pageFlowMemberField ) )
124         {
125             PageFlowController pfc = PageFlowUtils.getCurrentPageFlow( request );
126             initializeField( pageFlowMemberField, pfc );
127         }
128         
129         //
130
// Initialize the shared flow fields.
131
//
132
CachedSharedFlowRefInfo.SharedFlowFieldInfo[] sharedFlowMemberFields =
133                 getCachedInfo().getSharedFlowMemberFields();
134         
135         if ( sharedFlowMemberFields != null )
136         {
137             for ( int i = 0; i < sharedFlowMemberFields.length; i++ )
138             {
139                 CachedSharedFlowRefInfo.SharedFlowFieldInfo fi = sharedFlowMemberFields[i];
140                 Field JavaDoc field = fi.field;
141                 
142                 if ( fieldIsUninitialized( field ) )
143                 {
144                     Map JavaDoc/*< String, SharedFlowController >*/ sharedFlows = PageFlowUtils.getSharedFlows( request );
145                     String JavaDoc name = fi.sharedFlowName;
146                     SharedFlowController sf =
147                             name != null ? ( SharedFlowController ) sharedFlows.get( name ) : PageFlowUtils.getGlobalApp( request );
148                     
149                     if ( sf != null )
150                     {
151                         initializeField( field, sf );
152                     }
153                     else
154                     {
155                         _log.error( "Could not find shared flow with name \"" + fi.sharedFlowName
156                                     + "\" to initialize field " + field.getName() + " in " + getClass().getName() );
157                     }
158                 }
159             }
160         }
161     }
162
163     /**
164      * Get a page input that was passed from a Page Flow action as an "action output".
165      *
166      * @param pageInputName the name of the page input. This is the same as the name of the "action output".
167      * @return the value of the page input, or <code>null</code> if the given one does not exist.
168      */

169     protected Object JavaDoc getPageInput( String JavaDoc pageInputName )
170     {
171         return _pageInputs != null ? _pageInputs.get( pageInputName ) : null;
172     }
173     
174     /**
175      * Get the map of all page inputs that was passed from a Page Flow action as "action outputs".
176      *
177      * @return a Map of page-input-name (String) to page-input-value (Object).
178      */

179     public Map JavaDoc getPageInputMap()
180     {
181         return _pageInputs;
182     }
183     
184     private CachedFacesBackingInfo getCachedInfo()
185     {
186         ClassLevelCache cache = ClassLevelCache.getCache( getClass() );
187         CachedFacesBackingInfo info = ( CachedFacesBackingInfo ) cache.getCacheObject( CACHED_INFO_KEY );
188         
189         if ( info == null )
190         {
191             info = new CachedFacesBackingInfo( getClass(), getServletContext() );
192             cache.setCacheObject( CACHED_INFO_KEY, info );
193         }
194         
195         return info;
196     }
197 }
198
Popular Tags