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          {
58              return null;
59          }
60  
61          if (MethodExpression.class.equals(meta.getPropertyType(name)))
62          {
63              Method method = meta.getWriteMethod(name);
64              if (method != null)
65              {
66                  return new MethodExpressionMetadata(method, attribute, this.returnTypeClass, this.params);
67              }
68          }
69  
70          return null;
71      }
72  
73      private class MethodExpressionMetadata extends Metadata
74      {
75          private final Method _method;
76  
77          private final TagAttribute _attribute;
78  
79          private Class<?>[] _paramList;
80  
81          private Class<?> _returnType;
82  
83          public MethodExpressionMetadata(Method method, TagAttribute attribute, Class<?> returnType, 
84                                          Class<?>[] paramList)
85          {
86              _method = method;
87              _attribute = attribute;
88              _paramList = paramList;
89              _returnType = returnType;
90          }
91  
92          public void applyMetadata(FaceletContext ctx, Object instance)
93          {
94              MethodExpression expr = _attribute.getMethodExpression(ctx, _returnType, _paramList);
95  
96              try
97              {
98                  _method.invoke(instance, new Object[] { expr });
99              }
100             catch (InvocationTargetException e)
101             {
102                 throw new TagAttributeException(_attribute, e.getCause());
103             }
104             catch (Exception e)
105             {
106                 throw new TagAttributeException(_attribute, e);
107             }
108         }
109     }
110 }