KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > DefaultPropertiesInjector


1 package jfun.yan;
2
3 import java.beans.IndexedPropertyDescriptor JavaDoc;
4 import java.beans.IntrospectionException JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import jfun.util.beans.BeanType;
10
11 /**
12  * The default PropertiesInjector implementation that uses java.beans introspection
13  * mechanism and injects properties by property names.
14  * <p>
15  * @author Ben Yu
16  * Dec 16, 2005 1:16:32 PM
17  */

18 public class DefaultPropertiesInjector implements PropertiesInjector {
19   /*The meta information of the bean. null if unknown*/
20   private final BeanType btype;
21   
22   /*The set of properties to set, null if every property is needed*/
23   private final java.util.Set JavaDoc props;
24   
25   /**
26    * Create an instance of PropertiesInjector.
27    * If type is null, dynamic binding will be used.
28    * @param type the bean class.
29    * @return the instance.
30    * @throws IntrospectionException
31    */

32   public static PropertiesInjector instance(Class JavaDoc type)
33   throws IntrospectionException JavaDoc{
34     final BeanType bt =jfun.yan.util.Utils.toBeanType(type);
35     return new DefaultPropertiesInjector(bt, null);
36   }
37   
38   /**
39    * Create an instance of PropertiesInjector.
40    * @param type the bean class. null if unknown.
41    * @param props the set of properties to set. null indicates all.
42    * @return the new component that handles property setting.
43    * @throws IntrospectionException
44    */

45   public static PropertiesInjector instance(final Class JavaDoc type, final java.util.Set JavaDoc props)
46   throws IntrospectionException JavaDoc{
47     final BeanType bt = jfun.yan.util.Utils.toBeanType(type);
48     checkIncludes(bt, props);
49     return new DefaultPropertiesInjector(bt, props);
50   }
51   /**
52    * Create an instance of DefaultPropertiesInjector using a BeanType object.
53    * @param btype the BeanType object.
54    * @param props the property names. Note, the constructor does not validate the property names.
55    */

56   public DefaultPropertiesInjector(final BeanType btype,
57       final Set JavaDoc props){
58     this.btype = btype;
59     this.props = props;
60   }
61   //make sure the property names contained in the set are all valid properties.
62
private static void checkIncludes(BeanType btype, java.util.Set JavaDoc s2){
63     //skip checking for dynamic binding.
64
if(btype==null) return;
65     for(Iterator JavaDoc it=s2.iterator(); it.hasNext();){
66       final Object JavaDoc name = it.next();
67       final java.beans.PropertyDescriptor JavaDoc desc =
68         btype.getPropertyDescriptor(""+name);
69       if(desc==null){
70         throw new IllegalPropertyNameException(btype.getType(), ""+name);
71       }
72       else{
73         if(desc.getWriteMethod()==null){
74           if(desc instanceof IndexedPropertyDescriptor JavaDoc){
75             final IndexedPropertyDescriptor JavaDoc idesc = (IndexedPropertyDescriptor JavaDoc)desc;
76             if(idesc.getIndexedWriteMethod()!=null){
77               return;
78             }
79           }
80           throw new NonWritablePropertyException(btype.getType(), ""+name);
81         }
82       }
83     }
84   }
85   private BeanType obtainBeanType(Class JavaDoc type){
86     //dynamically get the bean type.
87
//if the bean type is not early bound,
88
//use the object type to dynamically create it.
89
if(btype!=null) return btype;
90     else{
91       return jfun.yan.util.Utils.toBeanType(type);
92     }
93   }
94   private Set JavaDoc getProperties(BeanType bt){
95     //if the property set is null, get all the properties
96
//or, if the bean type is dynamically bound,
97
//check the validity of the pre-defined property names.
98
if(props==null){
99       final HashSet JavaDoc props = new HashSet JavaDoc();
100       for(Iterator JavaDoc it=bt.getPropertyNames().iterator();it.hasNext();){
101         final String JavaDoc name = (String JavaDoc)it.next();
102         if(bt.getWriter(name)!=null||bt.getIndexedWriter(name)!=null){
103           props.add(name);
104         }
105       }
106       return props;
107     }
108     else{
109       if(this.btype==null){
110         //dynamic binding
111
checkIncludes(bt, props);
112       }
113       return props;
114     }
115   }
116   public void injectProperties(Object JavaDoc obj, Dependency dep){
117     //if empty properties, do nothing.
118
if(props!=null && props.isEmpty()) return;
119     if(obj==null){
120       throw new NullBeanObjectException("null component");
121     }
122     //dynamically resolve bean type and properties if necessary.
123
final Class JavaDoc objtype = obj.getClass();
124     final BeanType btype = obtainBeanType(objtype);
125     final Set JavaDoc props = getProperties(btype);
126     jfun.yan.util.Utils.injectProperties(btype, obj, props, dep);
127   }
128
129   public void verifyProperties(Class JavaDoc type, Dependency dep){
130     //dynamically resolve bean type and properties if necessary.
131
//the type cannot be null now.
132
final BeanType btype = obtainBeanType(type);
133     final Set JavaDoc props = getProperties(btype);
134     jfun.yan.util.Utils.verifyProperties(btype, props, dep);
135   }
136   public String JavaDoc toString(){
137     return "bean";
138   }
139 }
140
Popular Tags