KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > el > MethodBindingImpl


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 package org.apache.myfaces.el;
17
18 import org.apache.myfaces.el.ValueBindingImpl.NotVariableReferenceException;
19
20 import org.apache.commons.beanutils.MethodUtils;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.application.Application;
25 import javax.faces.component.StateHolder;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.*;
28 import javax.faces.event.AbortProcessingException;
29 import javax.faces.validator.ValidatorException;
30 import javax.servlet.jsp.el.ELException JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33
34
35 /**
36  * @author Anton Koinov (latest modification by $Author: matze $)
37  * @version $Revision: 1.17 $ $Date: 2004/10/13 11:51:00 $
38  * $Log: MethodBindingImpl.java,v $
39  * Revision 1.17 2004/10/13 11:51:00 matze
40  * renamed packages to org.apache
41  *
42  * Revision 1.16 2004/07/01 22:05:12 mwessendorf
43  * ASF switch
44  *
45  * Revision 1.15 2004/06/23 15:56:16 manolito
46  * removed our own version of commons-beanutils MethodUtils
47  *
48  * Revision 1.14 2004/05/18 17:09:29 manolito
49  * resolved the problem with inaccessible methods in private classes that implement a public interface
50  *
51  * Revision 1.13 2004/05/11 04:24:10 dave0000
52  * Bug 943166: add value coercion to ManagedBeanConfigurator
53  *
54  * Revision 1.12 2004/04/16 15:13:31 manolito
55  * validator attribute support and MethodBinding invoke exception handling fixed
56  *
57  */

58 public class MethodBindingImpl extends MethodBinding
59     implements StateHolder
60 {
61     static final Log log = LogFactory.getLog(MethodBindingImpl.class);
62
63     //~ Instance fields -------------------------------------------------------
64

65     ValueBindingImpl _valueBinding;
66     Class JavaDoc[] _argClasses;
67
68     //~ Constructors ----------------------------------------------------------
69

70     public MethodBindingImpl(Application application, String JavaDoc reference,
71         Class JavaDoc[] argClasses)
72     {
73         // Note: using ValueBindingImpl, istead of creating a common subclass,
74
// to share single Expression cache
75
// Note: we can trim() reference, since string-binding mixed
76
// expressions are not allowed for MethodBindings
77
_valueBinding = new ValueBindingImpl(application, reference.trim());
78         _argClasses = argClasses;
79     }
80
81     //~ Methods ---------------------------------------------------------------
82

83     public String JavaDoc getExpressionString()
84     {
85         return _valueBinding._expressionString;
86     }
87
88     public Class JavaDoc getType(FacesContext facesContext)
89     {
90         try
91         {
92             Object JavaDoc[] baseAndProperty = resolveToBaseAndProperty(facesContext);
93             Object JavaDoc base = baseAndProperty[0];
94             Object JavaDoc property = baseAndProperty[1];
95
96             return base.getClass().getMethod(property.toString(), _argClasses)
97                 .getReturnType();
98         }
99         catch (ReferenceSyntaxException e)
100         {
101             throw e;
102         }
103         catch (IndexOutOfBoundsException JavaDoc e)
104         {
105             // ArrayIndexOutOfBoundsException also here
106
throw new PropertyNotFoundException("Expression: "
107                 + getExpressionString(), e);
108         }
109         catch (Exception JavaDoc e)
110         {
111             log.error(
112                 "Cannot get type for expression " + getExpressionString(), e);
113             throw new EvaluationException("Expression: "
114                 + getExpressionString(), e);
115         }
116     }
117
118     public Object JavaDoc invoke(FacesContext facesContext, Object JavaDoc[] args)
119         throws EvaluationException, MethodNotFoundException
120     {
121         try
122         {
123             Object JavaDoc[] baseAndProperty = resolveToBaseAndProperty(facesContext);
124             Object JavaDoc base = baseAndProperty[0];
125             Object JavaDoc property = baseAndProperty[1];
126
127             Method JavaDoc m = base.getClass().getMethod(property.toString(), _argClasses);
128
129             // Check if the concrete class of this method is accessible and if not
130
// search for a public interface that declares this method
131
m = MethodUtils.getAccessibleMethod(m);
132             if (m == null)
133             {
134                 throw new MethodNotFoundException(
135                     getExpressionString() + " (not accessible!)");
136             }
137
138             return m.invoke(base, args);
139         }
140         catch (ReferenceSyntaxException e)
141         {
142             throw e;
143         }
144         catch (IndexOutOfBoundsException JavaDoc e)
145         {
146             // ArrayIndexOutOfBoundsException also here
147
throw new PropertyNotFoundException("Expression: "
148                 + getExpressionString(), e);
149         }
150         catch (InvocationTargetException JavaDoc e)
151         {
152             Throwable JavaDoc cause = e.getCause();
153             if (cause != null)
154             {
155                 if (cause instanceof ValidatorException ||
156                     cause instanceof AbortProcessingException)
157                 {
158                     throw new EvaluationException(cause);
159                 }
160                 else
161                 {
162                     log.error("Exception while invoking expression "
163                         + getExpressionString(), cause);
164                     throw new EvaluationException("Expression: "
165                         + getExpressionString(), cause);
166                 }
167             }
168             else
169             {
170                 log.error("Exception while invoking expression "
171                     + getExpressionString(), e);
172                 throw new EvaluationException("Expression: "
173                     + getExpressionString(), e);
174             }
175         }
176         catch (Exception JavaDoc e)
177         {
178             log.error("Exception while invoking expression "
179                 + getExpressionString(), e);
180             throw new EvaluationException("Expression: "
181                 + getExpressionString(), e);
182         }
183     }
184
185     protected Object JavaDoc[] resolveToBaseAndProperty(FacesContext facesContext)
186         throws ELException JavaDoc
187     {
188         if (facesContext == null)
189         {
190             throw new NullPointerException JavaDoc("facesContext");
191         }
192
193         try
194         {
195             Object JavaDoc base = _valueBinding.resolveToBaseAndProperty(facesContext);
196
197             if (!(base instanceof Object JavaDoc[]))
198             {
199                 String JavaDoc errorMessage = "Expression not a valid method binding: "
200                     + getExpressionString();
201                 log.error(errorMessage);
202                 throw new ReferenceSyntaxException(errorMessage);
203             }
204
205             return (Object JavaDoc[]) base;
206         }
207         catch (NotVariableReferenceException e)
208         {
209             throw new ReferenceSyntaxException("Expression: "
210                 + getExpressionString(), e);
211         }
212     }
213     
214     public String JavaDoc toString()
215     {
216         return _valueBinding.toString();
217     }
218
219     //~ StateHolder implementation --------------------------------------------
220

221     private boolean _transient = false;
222
223     /**
224      * Empty constructor, so that new instances can be created when restoring
225      * state.
226      */

227     public MethodBindingImpl()
228     {
229         _valueBinding = null;
230         _argClasses = null;
231     }
232
233     public Object JavaDoc saveState(FacesContext facescontext)
234     {
235         return new Object JavaDoc[] { _valueBinding.saveState(facescontext),
236             _argClasses};
237     }
238
239     public void restoreState(FacesContext facescontext, Object JavaDoc obj)
240     {
241         Object JavaDoc[] ar = (Object JavaDoc[]) obj;
242         _valueBinding = new ValueBindingImpl();
243         _valueBinding.restoreState(facescontext, ar[0]);
244         _argClasses = (Class JavaDoc[]) ar[1];
245     }
246
247     public boolean isTransient()
248     {
249         return _transient;
250     }
251
252     public void setTransient(boolean flag)
253     {
254         _transient = flag;
255     }
256
257 }
Popular Tags