KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > faces > internal > BackingClassMethodBinding


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.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 JavaDoc;
34 import javax.servlet.ServletContext JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.lang.reflect.Field JavaDoc;
37
38 /**
39  * Internal class used in JSF/Page Flow integration. This exists to cause form beans to be submitted to Page Flow
40  * actions raised from JSF command event handlers.
41  * @see org.apache.beehive.netui.pageflow.faces.PageFlowApplicationFactory
42  */

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 JavaDoc _methodName;
52     private Class JavaDoc[] _params;
53     private MethodBinding _delegate;
54     private boolean _transient = false;
55     
56     public BackingClassMethodBinding()
57     {
58     }
59     
60     public BackingClassMethodBinding( String JavaDoc methodName, Class JavaDoc[] params, MethodBinding delegate )
61     {
62         _methodName = methodName;
63         _params = params;
64         _delegate = delegate;
65     }
66
67     public Class JavaDoc getType( FacesContext context )
68         throws MethodNotFoundException
69     {
70         return _delegate.getType( context );
71     }
72
73     public String JavaDoc getExpressionString() {
74         return _delegate.getExpressionString();
75     }
76
77     /**
78      * Before returning the result from the base MethodBinding, see if the bound method is annotated with
79      * Jpf.CommandHandler. If it is, look through the "raiseActions" annotation array for a form bean member variable
80      * associated with the action being raised. If one is found, set it in the request so it gets passed to the action.
81      */

82     public Object JavaDoc invoke( FacesContext context, Object JavaDoc params[] )
83         throws EvaluationException, MethodNotFoundException
84     {
85         Object JavaDoc result = _delegate.invoke( context, params );
86         
87         if ( result instanceof String JavaDoc )
88         {
89             String JavaDoc action = ( String JavaDoc ) result;
90             Object JavaDoc request = context.getExternalContext().getRequest();
91             assert request != null;
92             assert request instanceof HttpServletRequest JavaDoc : request.getClass().getName();
93             
94             HttpServletRequest JavaDoc httpRequest = ( HttpServletRequest JavaDoc ) request;
95             Object JavaDoc backingBean = InternalUtils.getFacesBackingBean( httpRequest );
96             
97             if ( backingBean != null )
98             {
99                 Class JavaDoc backingClass = backingBean.getClass();
100                 
101                 Object JavaDoc servletContextObject = context.getExternalContext().getContext();
102                 assert servletContextObject instanceof ServletContext JavaDoc : servletContextObject.getClass().getName();
103                 ServletContext JavaDoc servletContext = ( ServletContext JavaDoc ) servletContextObject;
104                 Method JavaDoc 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 JavaDoc backingClass, Object JavaDoc backingBean,
127                                             String JavaDoc action, HttpServletRequest JavaDoc httpRequest )
128     {
129         for ( int i = 0; i < raiseActions.length; i++ )
130         {
131             ProcessedAnnotation raiseAction = raiseActions[i];
132             String JavaDoc actionAttr = AnnotationReader.getStringAttribute( raiseAction, "action" );
133                                 
134             if ( actionAttr.equals( action ) )
135             {
136                 String JavaDoc formBeanMember =
137                         AnnotationReader.getStringAttribute( raiseAction, "outputFormBean" );
138                                     
139                 if ( formBeanMember != null && formBeanMember.length() > 0 )
140                 {
141                     try
142                     {
143                         Field JavaDoc 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 JavaDoc value = field.get( backingBean );
151                         InternalUtils.setForwardedFormBean( httpRequest, InternalUtils.wrapFormBean( value ) );
152                     }
153                     catch ( IllegalAccessException JavaDoc 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 JavaDoc saveState( FacesContext context )
164     {
165         return new Object JavaDoc[]{ _methodName, _params, UIComponentBase.saveAttachedState( context, _delegate ) };
166     }
167     
168     public void restoreState( FacesContext context, Object JavaDoc state )
169     {
170         Object JavaDoc[] values = ( Object JavaDoc[] ) state;
171         _methodName = ( String JavaDoc ) values[0];
172         _params = ( Class JavaDoc[] ) 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