1   /*
2    * Scope: a generic MVC framework.
3    * Copyright (c) 2000-2002, The Scope team
4    * All rights reserved.
5    *
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * Neither the name "Scope" nor the names of its contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   *
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
27   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34   *
35   *
36   * $Id: BeanInfos.java,v 1.7 2002/09/05 15:41:45 ludovicc Exp $
37   */
38  package org.scopemvc.model.beans;
39  
40  
41  import java.beans.BeanInfo;
42  import java.beans.IntrospectionException;
43  import java.beans.Introspector;
44  import java.beans.PropertyDescriptor;
45  import java.util.HashMap;
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  import org.scopemvc.util.Debug;
49  
50  /***
51   * <P>
52   *
53   * Internal class used by the Beans ModelManagers to access BeanInfo for model
54   * objects. </P>
55   *
56   * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
57   * @created 05 September 2002
58   * @version $Revision: 1.7 $ $Date: 2002/09/05 15:41:45 $
59   */
60  final class BeanInfos {
61  
62      private final static Log LOG = LogFactory.getLog(BeanInfos.class);
63  
64      private static HashMap beanInfos = new HashMap();
65      private static HashMap propertyDescriptors = new HashMap();
66  
67  
68      /***
69       * Gets the bean info
70       *
71       * @param inModelClass TODO: Describe the Parameter
72       * @return The beanInfo value
73       */
74      static BeanInfo getBeanInfo(Class inModelClass) {
75          // Got it cached? Note that Introspector maintains a cache but
76          // ... it returns a copy of cached BeanInfos. Since Scope treats
77          // ... the BeanInfo as immutable we may as well cache BeanInfo
78          // ... here to avoid the BeanInfo copy.
79          BeanInfo result = (BeanInfo) beanInfos.get(inModelClass);
80          if (result == null) {
81              // Not in cache so get one in standard JavaBeans way
82              try {
83                  result = Introspector.getBeanInfo(inModelClass, Object.class);
84                  if (Debug.ON) {
85                      Debug.assertTrue(result != null);
86                  }
87                  beanInfos.put(inModelClass, result);
88                  cachePropertyDescriptors(inModelClass, result);
89              } catch (IntrospectionException e) {
90                  LOG.fatal("Can't find BeanInfo for: " + inModelClass, e);
91                  if (Debug.ON) {
92                      Debug.assertTrue(1 == 0, e.toString());
93                  }
94              }
95          }
96          return result;
97      }
98  
99  
100     /***
101      * Gets the property descriptor
102      *
103      * @param inModelClass TODO: Describe the Parameter
104      * @param inPropertyName TODO: Describe the Parameter
105      * @return The propertyDescriptor value
106      */
107     static PropertyDescriptor getPropertyDescriptor(Class inModelClass, String inPropertyName) {
108         if (Debug.ON) {
109             Debug.assertTrue(inPropertyName != null, "null inPropertyName");
110         }
111         Object descriptors = propertyDescriptors.get(inModelClass);
112         if (descriptors == null) {
113             getBeanInfo(inModelClass);
114             descriptors = propertyDescriptors.get(inModelClass);
115         }
116         if (Debug.ON) {
117             Debug.assertTrue(descriptors instanceof HashMap, "descriptors not HashMap: " + descriptors);
118         }
119         return (PropertyDescriptor) ((HashMap) descriptors).get(inPropertyName);
120     }
121 
122 
123     private static void cachePropertyDescriptors(Class inModelClass, BeanInfo beanInfo) {
124         HashMap descriptors = new HashMap();
125         PropertyDescriptor[] beanDescriptors = beanInfo.getPropertyDescriptors();
126         for (int i = 0; i < beanDescriptors.length; ++i) {
127             PropertyDescriptor desc = beanDescriptors[i];
128             if (Debug.ON) {
129                 Debug.assertTrue(desc != null, "null desc");
130             }
131             descriptors.put(desc.getName(), desc);
132         }
133         propertyDescriptors.put(inModelClass, descriptors);
134     }
135 }
This page was automatically generated by Maven