KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > FacesBackingBeanFactory


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.internal;
19
20 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
21 import org.apache.beehive.netui.pageflow.FacesBackingBean;
22 import org.apache.beehive.netui.pageflow.PageFlowUtils;
23 import org.apache.beehive.netui.pageflow.handler.Handlers;
24 import org.apache.beehive.netui.util.internal.FileUtils;
25 import org.apache.beehive.netui.util.logging.Logger;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30
31
32
33 public class FacesBackingBeanFactory
34         implements InternalConstants
35 {
36     private static final Logger _log = Logger.getInstance( FacesBackingBeanFactory.class );
37     
38     
39     public static FacesBackingBean getFacesBackingBeanForRequest( HttpServletRequest JavaDoc request,
40                                                                   HttpServletResponse JavaDoc response,
41                                                                   ServletContext JavaDoc servletContext )
42     {
43         String JavaDoc uri = InternalUtils.getDecodedServletPath( request );
44         assert uri.charAt( 0 ) == '/' : uri;
45         String JavaDoc backingClassName = FileUtils.stripFileExtension( uri.substring( 1 ).replace( '/', '.' ) );
46         FacesBackingBean currentBean = InternalUtils.getFacesBackingBean( request );
47         
48         //
49
// If there is no current backing bean, or if the current one doesn't match the desired classname, create one.
50
//
51
if ( currentBean == null || ! currentBean.getClass().getName().equals( backingClassName ) )
52         {
53             FacesBackingBean bean = null;
54             
55             if ( FileUtils.uriEndsWith( uri, FACES_EXTENSION ) || FileUtils.uriEndsWith( uri, JSF_EXTENSION ) )
56             {
57                 bean = loadFacesBackingBean( request, servletContext, backingClassName );
58                 
59                 //
60
// If we didn't create (or failed to create) a backing bean, and if this is a JSF request, then create
61
// a default one. This ensures that there will be a place for things like page inputs, that get stored
62
// in the backing bean across postbacks to the same JSF.
63
//
64
if ( bean == null ) bean = new DefaultFacesBackingBean();
65                 
66                 //
67
// If we created a backing bean, invoke its create callback, and tell it to store itself in the session.
68
//
69
if ( bean != null )
70                 {
71                     try
72                     {
73                         bean.create( request, response, servletContext );
74                     }
75                     catch ( Exception JavaDoc e )
76                     {
77                         _log.error( "Error while creating backing bean instance of " + backingClassName, e );
78                     }
79                     
80                     bean.persistInSession( request, response );
81                     return bean;
82                 }
83             }
84             
85             //
86
// We didn't create a backing bean. If there's one in the session (an inappropriate one), remove it.
87
//
88
HttpServletRequest JavaDoc unwrappedRequest = PageFlowUtils.unwrapMultipart( request );
89             ScopedServletUtils.removeScopedSessionAttr( FACES_BACKING_ATTR, unwrappedRequest );
90         }
91         else if ( currentBean != null )
92         {
93             if ( _log.isDebugEnabled() )
94             {
95                 _log.debug( "Using existing backing bean instance " + currentBean + " for request " +
96                             request.getRequestURI() );
97             }
98             
99             currentBean.reinitialize( request, response, servletContext );
100         }
101         
102         return currentBean;
103     }
104     
105     private static FacesBackingBean loadFacesBackingBean( HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext,
106                                                           String JavaDoc backingClassName )
107     {
108         try
109         {
110             Class JavaDoc backingClass =
111                 Handlers.get( servletContext ).getReloadableClassHandler().loadCachedClass( backingClassName );
112                 
113             if ( backingClass == null )
114             {
115                 if ( _log.isTraceEnabled() )
116                 {
117                     _log.trace( "No backing bean class " + backingClassName + " found for request "
118                                 + request.getRequestURI() );
119                 }
120             }
121             else
122             {
123                 AnnotationReader annReader = AnnotationReader.getAnnotationReader( backingClass, servletContext );
124                     
125                 if ( annReader.getJpfAnnotation( backingClass, "FacesBacking" ) != null )
126                 {
127                     if ( _log.isDebugEnabled() )
128                     {
129                         _log.debug( "Found backing class " + backingClassName + " for request "
130                                     + request.getRequestURI() + "; creating a new instance." );
131                     }
132                         
133                     return ( FacesBackingBean ) backingClass.newInstance();
134                 }
135                 else
136                 {
137                     if ( _log.isDebugEnabled() )
138                     {
139                         _log.debug( "Found matching backing class " + backingClassName + " for request "
140                                     + request.getRequestURI() + ", but it does not have the "
141                                     + ANNOTATION_QUALIFIER + "FacesBacking" + " annotation." );
142                     }
143                 }
144             }
145         }
146         catch ( InstantiationException JavaDoc e )
147         {
148             _log.error( "Could not create backing bean instance of " + backingClassName, e );
149         }
150         catch ( IllegalAccessException JavaDoc e )
151         {
152             _log.error( "Could not create backing bean instance of " + backingClassName, e );
153         }
154         
155         return null;
156     }
157     
158     private static class DefaultFacesBackingBean
159         extends FacesBackingBean
160     {
161     }
162 }
163
Popular Tags