1 18 package org.apache.beehive.netui.pageflow.faces.internal; 19 20 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 21 import org.apache.beehive.netui.pageflow.internal.AnnotationReader; 22 import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotation; 23 import org.apache.beehive.netui.util.logging.Logger; 24 import org.apache.beehive.netui.util.internal.cache.FieldCache; 25 import org.apache.beehive.netui.util.internal.cache.MethodCache; 26 27 import javax.faces.el.MethodBinding; 28 import javax.faces.el.MethodNotFoundException; 29 import javax.faces.el.EvaluationException; 30 import javax.faces.context.FacesContext; 31 import javax.faces.component.StateHolder; 32 import javax.faces.component.UIComponentBase; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.ServletContext ; 35 import java.lang.reflect.Method ; 36 import java.lang.reflect.Field ; 37 38 43 public class BackingClassMethodBinding 44 extends MethodBinding 45 implements StateHolder 46 { 47 private static final Logger _log = Logger.getInstance( BackingClassMethodBinding.class ); 48 private static final FieldCache _fieldCache = new FieldCache(); 49 private static final MethodCache _methodCache = new MethodCache(); 50 51 private String _methodName; 52 private Class [] _params; 53 private MethodBinding _delegate; 54 private boolean _transient = false; 55 56 public BackingClassMethodBinding() 57 { 58 } 59 60 public BackingClassMethodBinding( String methodName, Class [] params, MethodBinding delegate ) 61 { 62 _methodName = methodName; 63 _params = params; 64 _delegate = delegate; 65 } 66 67 public Class getType( FacesContext context ) 68 throws MethodNotFoundException 69 { 70 return _delegate.getType( context ); 71 } 72 73 public String getExpressionString() { 74 return _delegate.getExpressionString(); 75 } 76 77 82 public Object invoke( FacesContext context, Object params[] ) 83 throws EvaluationException, MethodNotFoundException 84 { 85 Object result = _delegate.invoke( context, params ); 86 87 if ( result instanceof String ) 88 { 89 String action = ( String ) result; 90 Object request = context.getExternalContext().getRequest(); 91 assert request != null; 92 assert request instanceof HttpServletRequest : request.getClass().getName(); 93 94 HttpServletRequest httpRequest = ( HttpServletRequest ) request; 95 Object backingBean = InternalUtils.getFacesBackingBean( httpRequest ); 96 97 if ( backingBean != null ) 98 { 99 Class backingClass = backingBean.getClass(); 100 101 Object servletContextObject = context.getExternalContext().getContext(); 102 assert servletContextObject instanceof ServletContext : servletContextObject.getClass().getName(); 103 ServletContext servletContext = ( ServletContext ) servletContextObject; 104 Method method = _methodCache.getMethod( backingClass, _methodName, _params ); 105 106 if ( method == null ) throw new MethodNotFoundException( _methodName ); 107 AnnotationReader annReader = AnnotationReader.getAnnotationReader( backingClass, servletContext ); 108 ProcessedAnnotation ann = annReader.getJpfAnnotation( method, "CommandHandler" ); 109 110 if ( ann != null ) 111 { 112 ProcessedAnnotation[] raiseActions = 113 AnnotationReader.getAnnotationArrayAttribute( ann, "raiseActions" ); 114 115 if ( raiseActions != null ) 116 { 117 setOutputFormBeans( raiseActions, backingClass, backingBean, action, httpRequest ); 118 } 119 } 120 } 121 } 122 123 return result; 124 } 125 126 private static void setOutputFormBeans( ProcessedAnnotation[] raiseActions, Class backingClass, Object backingBean, 127 String action, HttpServletRequest httpRequest ) 128 { 129 for ( int i = 0; i < raiseActions.length; i++ ) 130 { 131 ProcessedAnnotation raiseAction = raiseActions[i]; 132 String actionAttr = AnnotationReader.getStringAttribute( raiseAction, "action" ); 133 134 if ( actionAttr.equals( action ) ) 135 { 136 String formBeanMember = 137 AnnotationReader.getStringAttribute( raiseAction, "outputFormBean" ); 138 139 if ( formBeanMember != null && formBeanMember.length() > 0 ) 140 { 141 try 142 { 143 Field field = _fieldCache.getDeclaredField( backingClass, formBeanMember ); 144 if ( field == null ) 145 { 146 _log.error( "Could not find field " + formBeanMember + " specified as the outputFormBean " 147 + "for action " + action + " raised by " + backingClass.getName() ); 148 return; 149 } 150 Object value = field.get( backingBean ); 151 InternalUtils.setForwardedFormBean( httpRequest, InternalUtils.wrapFormBean( value ) ); 152 } 153 catch ( IllegalAccessException e ) 154 { 155 _log.error( "Could not access field " + formBeanMember + " specified as the outputFormBean " 156 + "for action " + action + " raised by " + backingClass.getName(), e ); 157 } 158 } 159 } 160 } 161 } 162 163 public Object saveState( FacesContext context ) 164 { 165 return new Object []{ _methodName, _params, UIComponentBase.saveAttachedState( context, _delegate ) }; 166 } 167 168 public void restoreState( FacesContext context, Object state ) 169 { 170 Object [] values = ( Object [] ) state; 171 _methodName = ( String ) values[0]; 172 _params = ( Class [] ) values[1]; 173 _delegate = ( MethodBinding ) UIComponentBase.restoreAttachedState( context, values[2] ); 174 } 175 176 public boolean isTransient() 177 { 178 return _transient; 179 } 180 181 public void setTransient( boolean newTransientValue ) 182 { 183 _transient = newTransientValue; 184 } 185 } 186 | Popular Tags |