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.faces.view.facelets.FaceletContext;
25  import javax.faces.view.facelets.MetaRule;
26  import javax.faces.view.facelets.Metadata;
27  import javax.faces.view.facelets.MetadataTarget;
28  import javax.faces.view.facelets.TagAttribute;
29  import javax.faces.view.facelets.TagAttributeException;
30  
31  /**
32   * 
33   * @author Jacob Hookom
34   * @version $Id$
35   */
36  public final class BeanPropertyTagRule extends MetaRule
37  {
38      public final static BeanPropertyTagRule INSTANCE = new BeanPropertyTagRule();
39  
40      public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
41      {
42          Method m = meta.getWriteMethod(name);
43  
44          // if the property is writable
45          if (m != null)
46          {
47              if (attribute.isLiteral())
48              {
49                  return new LiteralPropertyMetadata(m, attribute);
50              }
51              else
52              {
53                  return new DynamicPropertyMetadata(m, attribute);
54              }
55          }
56  
57          return null;
58      }
59      
60      final static class LiteralPropertyMetadata extends Metadata
61      {
62  
63          private final Method method;
64  
65          private final TagAttribute attribute;
66  
67          private Object[] value;
68  
69          public LiteralPropertyMetadata(Method method, TagAttribute attribute)
70          {
71              this.method = method;
72              this.attribute = attribute;
73          }
74  
75          public void applyMetadata(FaceletContext ctx, Object instance)
76          {
77              if (value == null)
78              {
79                  String str = this.attribute.getValue();
80                  value = new Object[] { ctx.getExpressionFactory().coerceToType(str, method.getParameterTypes()[0]) };
81              }
82              try
83              {
84                  method.invoke(instance, this.value);
85              }
86              catch (InvocationTargetException e)
87              {
88                  throw new TagAttributeException(this.attribute, e.getCause());
89              }
90              catch (Exception e)
91              {
92                  throw new TagAttributeException(this.attribute, e);
93              }
94          }
95  
96      }
97  
98      final static class DynamicPropertyMetadata extends Metadata
99      {
100 
101         private final Method method;
102 
103         private final TagAttribute attribute;
104 
105         private final Class<?> type;
106 
107         public DynamicPropertyMetadata(Method method, TagAttribute attribute)
108         {
109             this.method = method;
110             this.type = method.getParameterTypes()[0];
111             this.attribute = attribute;
112         }
113 
114         public void applyMetadata(FaceletContext ctx, Object instance)
115         {
116             try
117             {
118                 method.invoke(instance, new Object[] { attribute.getObject(ctx, type) });
119             }
120             catch (InvocationTargetException e)
121             {
122                 throw new TagAttributeException(attribute, e.getCause());
123             }
124             catch (Exception e)
125             {
126                 throw new TagAttributeException(attribute, e);
127             }
128         }
129     }
130 
131 }