View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  
24  import javax.el.MethodExpression;
25  import javax.faces.view.facelets.FaceletContext;
26  import javax.faces.view.facelets.MetaRule;
27  import javax.faces.view.facelets.Metadata;
28  import javax.faces.view.facelets.MetadataTarget;
29  import javax.faces.view.facelets.TagAttribute;
30  import javax.faces.view.facelets.TagAttributeException;
31  
32  /**
33   * Optional Rule for binding Method[Binding|Expression] properties
34   * 
35   * @author Mike Kienenberger
36   * @author Jacob Hookom
37   */
38  public final class MethodRule extends MetaRule
39  {
40  
41      private final String methodName;
42  
43      private final Class<?> returnTypeClass;
44  
45      private final Class<?>[] params;
46  
47      public MethodRule(String methodName, Class<?> returnTypeClass, Class<?>[] params)
48      {
49          this.methodName = methodName;
50          this.returnTypeClass = returnTypeClass;
51          this.params = params;
52      }
53  
54      public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
55      {
56          if (false == name.equals(this.methodName))
57              return null;
58  
59          if (MethodExpression.class.equals(meta.getPropertyType(name)))
60          {
61              Method method = meta.getWriteMethod(name);
62              if (method != null)
63              {
64                  return new MethodExpressionMetadata(method, attribute, this.returnTypeClass, this.params);
65              }
66          }
67  
68          return null;
69      }
70  
71      private class MethodExpressionMetadata extends Metadata
72      {
73          private final Method _method;
74  
75          private final TagAttribute _attribute;
76  
77          private Class<?>[] _paramList;
78  
79          private Class<?> _returnType;
80  
81          public MethodExpressionMetadata(Method method, TagAttribute attribute, Class<?> returnType, 
82                                          Class<?>[] paramList)
83          {
84              _method = method;
85              _attribute = attribute;
86              _paramList = paramList;
87              _returnType = returnType;
88          }
89  
90          public void applyMetadata(FaceletContext ctx, Object instance)
91          {
92              MethodExpression expr = _attribute.getMethodExpression(ctx, _returnType, _paramList);
93  
94              try
95              {
96                  _method.invoke(instance, new Object[] { expr });
97              }
98              catch (InvocationTargetException e)
99              {
100                 throw new TagAttributeException(_attribute, e.getCause());
101             }
102             catch (Exception e)
103             {
104                 throw new TagAttributeException(_attribute, e);
105             }
106         }
107     }
108 }