1 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 ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.ServletContext ; 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 request, 40 HttpServletResponse response, 41 ServletContext servletContext ) 42 { 43 String uri = InternalUtils.getDecodedServletPath( request ); 44 assert uri.charAt( 0 ) == '/' : uri; 45 String backingClassName = FileUtils.stripFileExtension( uri.substring( 1 ).replace( '/', '.' ) ); 46 FacesBackingBean currentBean = InternalUtils.getFacesBackingBean( request ); 47 48 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 if ( bean == null ) bean = new DefaultFacesBackingBean(); 65 66 if ( bean != null ) 70 { 71 try 72 { 73 bean.create( request, response, servletContext ); 74 } 75 catch ( Exception 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 HttpServletRequest 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 request, ServletContext servletContext, 106 String backingClassName ) 107 { 108 try 109 { 110 Class 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 e ) 147 { 148 _log.error( "Could not create backing bean instance of " + backingClassName, e ); 149 } 150 catch ( IllegalAccessException 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 |