KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > etc > InjectorHelper


1 package jfun.yan.etc;
2 import jfun.yan.Binder;
3 import jfun.yan.Component;
4 import jfun.yan.Map2;
5 import jfun.yan.Monad;
6 import jfun.yan.Mutation;
7 import jfun.yan.util.Utils;
8
9 /**
10  * This class provides helper functions to help dependency injections
11  * on objects not created by Yan.
12  * <p>
13  * @author Michelle Lei
14  *
15  */

16 public class InjectorHelper{
17   /**
18    * To create a Component object that will instantiate an object
19    * that implements a certain injector interface.
20    * <p>
21    * The injector interface should describe at least one method
22    * that expects one and only one parameter object. This method is responsible
23    * for injecting dependencies into this parameter object.
24    * </p>
25    * <p>
26    * The injection logic is encapsulated in a Binder object.
27    * </p>
28    * @param injector_itf The injector interface class.
29    * @param binder the Binder object encapsulating the dependency injection logic.
30    * @return the Component that instantiates the injector object.
31    */

32   public static Component getInjectorComponent(Class JavaDoc injector_itf, Binder binder){
33     return Utils.asComponent(binder).factory(injector_itf);
34   }
35   
36   /**
37    * To create a Component that instantiates proxy.
38    * <p>
39    * The proxy object is responsible for injecting dependencies
40    * into return values from the proxied methods.
41    * </p>
42    * <p>
43    * The dependency injection logic is described by the <i>binder</i> object.
44    * </p>
45    * <p>
46    * The instance instantiated by the <i>proxied</i> Component is proxied.
47    * </p>
48    * <p>
49    * Only the dependencies of return values of <i>injectee_type</i> are injected.
50    * </p>
51    * @param cloader The ClassLoader used by the dynamic proxy.
52    * @param itf the interface that the proxy implements.
53    * @param proxied the Component that instantiates the proxied object.
54    * @param injectee_type only return values of this type gets injected.
55    * @param binder this object encapsulates the injection logic.
56    * @return the new Component that instantiates the proxied object.
57    */

58   public static Component getProxyComponentReturningInjected(
59       final ClassLoader JavaDoc cloader, final Class JavaDoc itf,
60       Component proxied, final Class JavaDoc injectee_type, Binder binder){
61     final Component mutation = getInjectorComponent(Mutation.class, binder);
62     final Component injected = Monad.map(proxied, mutation, new Map2(){
63       public Object JavaDoc map(Object JavaDoc proxied, Object JavaDoc mut){
64         return InjectingProxy.getInjectingProxy(cloader, itf,
65             proxied, injectee_type, (Mutation)mut);
66       }
67     });
68     return injected;
69   }
70   /**
71    * To create a Component that instantiates proxy.
72    * <p>
73    * The proxy object is responsible for injecting dependencies
74    * into return values from the proxied methods.
75    * </p>
76    * <p>
77    * The dependency injection logic is described by the <i>binder</i> object.
78    * </p>
79    * <p>
80    * The instance instantiated by the <i>proxied</i> Component is proxied.
81    * </p>
82    * <p>
83    * Only the dependencies of return values of <i>injectee_type</i> are injected.
84    * </p>
85    * <p>
86    * The ClassLoader that loads the proxied instance is used to load the dynamic proxy.
87    * </p>
88    * @param itf the interface that the proxy implements.
89    * @param proxied the Component that instantiates the proxied object.
90    * @param injectee_type only return values of this type gets injected.
91    * @param binder this object encapsulates the injection logic.
92    * @return the new Component that instantiates the proxied object.
93    */

94   public static Component getProxyComponentReturningInjected(final Class JavaDoc itf,
95       Component proxied, Class JavaDoc injectee_type, Binder binder){
96     return getProxyComponentReturningInjected(injectee_type.getClassLoader(),
97         itf, proxied, injectee_type, binder);
98   }
99 }
Popular Tags