KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > Modes


1 package jfun.yan.xml;
2
3 import java.util.HashMap JavaDoc;
4
5 import jfun.util.Misc;
6 import jfun.yan.Component;
7 import jfun.yan.ComponentMap;
8 import jfun.yan.Components;
9 import jfun.yan.Container;
10 import jfun.yan.Creator;
11 import jfun.yan.DelegatingComponent;
12 import jfun.yan.Dependency;
13 import jfun.yan.ParameterBinder;
14 import jfun.yan.PropertyBinder;
15 import jfun.yan.factory.ThreadLocalScope;
16 import jfun.yan.function.Signature;
17
18 /**
19  * Some common wiring modes.
20  * <p>
21  * @author Ben Yu
22  * Nov 16, 2005 11:00:43 AM
23  */

24 public class Modes {
25   /**
26    * The singleton mode.
27    */

28   public static final SingletonMode simple_singleton = new SingletonMode(){
29     public Component decorate(Component c) {
30       return c.singleton();
31     }
32     public String JavaDoc toString(){
33       return Constants.ON;
34     }
35   };
36   /**
37    * The thread local scope singleton mode.
38    */

39   public static final SingletonMode thread_local_singleton = new SingletonMode(){
40     public Component decorate(Component c) {
41       return c.singleton(new ThreadLocalScope());
42     }
43     public String JavaDoc toString(){
44       return Constants.ON;
45     }
46   };
47   /**
48    * Wire parameters by type.
49    */

50   public static ParameterBinder params_bytype = new ParameterBinder(){
51     private final HashMap JavaDoc cache = new HashMap JavaDoc();
52     public Creator bind(Signature src, int ind, Class JavaDoc type) {
53       return useType(type, cache);
54     }
55     public String JavaDoc toString(){
56       return Constants.BYTYPE;
57     }
58   };
59   /**
60    * Wire properties by name.
61    */

62   public static PropertyBinder props_byname = new PropertyBinder(){
63     private final HashMap JavaDoc cache = new HashMap JavaDoc();
64     public Creator bind(Class JavaDoc component_type, Object JavaDoc key, Class JavaDoc type) {
65       return useKey(key, cache);
66     }
67     public String JavaDoc toString(){
68       return Constants.BYNAME;
69     }
70   };
71   /**
72    * Wire properties by type.
73    */

74   public static PropertyBinder props_bytype = new PropertyBinder(){
75     private final HashMap JavaDoc cache = new HashMap JavaDoc();
76     public Creator bind(Class JavaDoc component_type, Object JavaDoc key, Class JavaDoc type) {
77       return useType(type, cache);
78     }
79     public String JavaDoc toString(){
80       return Constants.BYTYPE;
81     }
82   };
83   /**
84    * Wire properties by fully qualified name.
85    * <p>
86    * For example, property balance in "com.ajoo.BankAccount" will resolve to
87    * a component named "com.ajoo.Bankaccount.balance".
88    * </p>
89    */

90   public static PropertyBinder props_byqualifiedname = new PropertyBinder(){
91     private final HashMap JavaDoc cache = new HashMap JavaDoc();
92     public Creator bind(Class JavaDoc component_type, Object JavaDoc key, Class JavaDoc type) {
93       return useKey(Misc.getTypeName(component_type)+"."+key, cache);
94     }
95     public String JavaDoc toString(){
96       return Constants.BYQUALIFIEDNAME;
97     }
98   };
99   /**
100    * The autodetect mode.
101    * <p>
102    * It will search by type first, if ambiguity happens or dependency not found,
103    * byname is used, then byqualifiedname, until the component is found.
104    * </p>
105    */

106   public static PropertyBinder props_autodetect = new PropertyBinder(){
107     public Creator bind(final Class JavaDoc component_type, final Object JavaDoc key, final Class JavaDoc type) {
108       final Object JavaDoc[] alt_keys = {key, Misc.getTypeName(component_type)+'.'+key};
109       return Components.autodetect(type, alt_keys);
110     }
111     public String JavaDoc toString(){
112       return Constants.AUTODETECT;
113     }
114   };
115   private static Component useKey(Object JavaDoc key, java.util.Map JavaDoc cache){
116     synchronized(cache){
117       Component result = (Component)cache.get(key);
118       if(result == null){
119         result = useKey(key);
120         cache.put(key, result);
121       }
122       return result;
123     }
124   }
125   private static Component useType(Class JavaDoc type, java.util.Map JavaDoc cache){
126     synchronized(cache){
127       Component result = (Component)cache.get(type);
128       if(result == null){
129         result = useType(type);
130         cache.put(type, result);
131       }
132       return result;
133     }
134   }
135   //useKey used in auto-wiring expects no property and argument, so optimize.
136
private static Component useKey(final Object JavaDoc key){
137     return new DelegatingComponent(Components.useKey(key)){
138       public Object JavaDoc create(Dependency dep){
139         final ComponentMap cmap = dep.getComponentMap();
140         if(cmap instanceof Container){
141           return ((Container)cmap).getInstance(key);
142         }
143         else return super.create(dep);
144       }
145     };
146   }
147   //useKey used in auto-wiring expects no property and argument, so optimize.
148
private static Component useType(final Class JavaDoc type){
149     return new DelegatingComponent(Components.useType(type)){
150       public Object JavaDoc create(Dependency dep){
151         final ComponentMap cmap = dep.getComponentMap();
152         if(cmap instanceof Container){
153           return ((Container)cmap).getInstanceOfType(type);
154         }
155         else return super.create(dep);
156       }
157     };
158   }
159 }
160
Popular Tags