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.composite;
20  
21  import java.beans.BeanInfo;
22  import java.beans.IntrospectionException;
23  import java.beans.PropertyDescriptor;
24  import java.lang.reflect.Method;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.el.ValueExpression;
29  import javax.faces.view.facelets.MetadataTarget;
30  import javax.faces.context.FacesContext;
31  import org.apache.myfaces.shared.util.ClassUtils;
32  
33  
34  /**
35   * Like MetadataTargetImpl but integrate composite component bean info
36   * with it.
37   * 
38   * @author Leonardo Uribe (latest modification by $Author$)
39   * @version $Revision$ $Date$
40   */
41  final class CompositeMetadataTargetImpl extends MetadataTarget
42  {
43      private final Map<String, PropertyDescriptor> _pd;
44      
45      private final MetadataTarget _delegate;
46      
47      private final BeanInfo _beanInfo;
48  
49      public CompositeMetadataTargetImpl(MetadataTarget delegate, BeanInfo beanInfo) throws IntrospectionException
50      {
51          _delegate = delegate;
52          _beanInfo = beanInfo;
53          
54          _pd = new HashMap<String, PropertyDescriptor>();
55          
56          for (PropertyDescriptor descriptor : _beanInfo.getPropertyDescriptors())
57          {
58              _pd.put(descriptor.getName(), descriptor);
59          }
60      }
61  
62      public PropertyDescriptor getProperty(String name)
63      {
64          PropertyDescriptor pd = _delegate.getProperty(name); 
65          if (pd == null)
66          {
67              pd = _pd.get(name);
68          }
69          return pd;
70      }
71  
72      public Class<?> getPropertyType(String name)
73      {
74          PropertyDescriptor pd = getProperty(name);
75          if (pd != null)
76          {
77              Object type = pd.getValue("type");
78              if (type != null)
79              {
80                  type = ((ValueExpression)type).getValue(FacesContext.getCurrentInstance().getELContext());
81                  if (type instanceof String)
82                  {
83                      try
84                      {
85                          type = ClassUtils.javaDefaultTypeToClass((String)type);
86                      }
87                      catch (ClassNotFoundException e)
88                      {
89                          type = Object.class;
90                      }
91                  }
92                  return (Class<?>) type;
93              }
94              return pd.getPropertyType();
95          }
96          
97          return null;
98      }
99  
100     public Method getReadMethod(String name)
101     {
102         PropertyDescriptor pd = getProperty(name);
103         if (pd != null)
104         {
105             return pd.getReadMethod();
106         }
107         
108         return null;
109     }
110 
111     public Class<?> getTargetClass()
112     {
113         return _delegate.getTargetClass();
114     }
115 
116     public Method getWriteMethod(String name)
117     {
118         PropertyDescriptor pd = getProperty(name);
119         if (pd != null)
120         {
121             return pd.getWriteMethod();
122         }
123         
124         return null;
125     }
126 
127     public boolean isTargetInstanceOf(Class type)
128     {
129         return _delegate.isTargetInstanceOf(type);
130     }
131 }