KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > factory > PropertyComponentFactory


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6 package org.jfox.ioc.factory;
7
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9 import java.lang.reflect.Method JavaDoc;
10 import java.util.Arrays JavaDoc;
11
12 import org.jfox.ioc.Component;
13 import org.jfox.ioc.ComponentContext;
14 import org.jfox.ioc.depend.Dependency;
15 import org.jfox.ioc.depend.DependencyPack;
16 import org.jfox.ioc.exception.ComponentException;
17 import org.jfox.ioc.util.Classes;
18
19 /**
20  * 方法注射器使用的组件Factory。
21  * <br>
22  * 该工厂在构造组件实例之后,会调用Setter参数指定的方法,
23  * 完成通过setter来注射依赖组件的工作。
24  *
25  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
26  */

27
28 public class PropertyComponentFactory extends DecorateComponentFactory {
29     private DependencyPack[] paramPacks;
30
31     /**
32      * 存储由 getComponentInstance 传来的临时 ParameterPack[]
33      */

34     private DependencyPack[] tempParamPacks = DependencyPack.EMPTY_PARAMETER_PACKS;
35
36     public PropertyComponentFactory(ConstrComponentFactory delegate, DependencyPack[] propParamPacks) {
37         super(delegate);
38         this.paramPacks = propParamPacks;
39     }
40
41     /**
42      * 先生成组件实例,然后调用 setter
43      */

44     public synchronized Component makeComponent() throws ComponentException {
45         Component component = super.makeComponent();
46         doMethods(component);
47         return component;
48     }
49
50     /**
51      * makeComponent 之后,先设置ComponentContext,再反射调用 setter method
52      * @param context
53      * @return
54      * @throws ComponentException
55      */

56     public synchronized Component makeComponent(ComponentContext context) throws ComponentException {
57         Component component = super.makeComponent();
58         component.setComponentContext(context);
59         doMethods(component);
60         return component;
61     }
62
63     /**
64      * 得到所有 Setter 参数
65      */

66     public DependencyPack[] getParameterPacks() {
67         return paramPacks;
68     }
69
70     /**
71      * 追加 ParameterPack,由 getComponentInstance 引入
72      *
73      * @param propParamPacks
74      */

75     public void addParameterPack(DependencyPack[] propParamPacks) {
76         if(propParamPacks != null && propParamPacks.length != 0) {
77             tempParamPacks = propParamPacks;
78         }
79     }
80
81     /**
82      * 按次序调用所有的 Setter
83      *
84      * @param component
85      * @throws ComponentException
86      */

87     private void doMethods(Component component) throws ComponentException {
88         if(paramPacks == null || paramPacks.length == 0) return;
89         for(int i = 0; i < paramPacks.length; i++) {
90             DependencyPack pack = paramPacks[i];
91             doMethod(component, pack);
92         }
93
94         if(tempParamPacks != null && tempParamPacks.length != 0) {
95             try {
96
97                 for(int i = 0; i < tempParamPacks.length; i++) {
98                     DependencyPack pack = tempParamPacks[i];
99                     doMethod(component, pack);
100                 }
101
102             }
103             finally {
104                 tempParamPacks = DependencyPack.EMPTY_PARAMETER_PACKS;
105             }
106         }
107     }
108
109     /**
110      * 使用反射调用一个 Setter
111      */

112     protected void doMethod(Component component, DependencyPack pack) throws ComponentException {
113         Object JavaDoc[] params = resolveParameters(new Dependency[]{pack.getParameter()});
114         Class JavaDoc[] types = getParameterTypes(new Dependency[]{pack.getParameter()});
115
116
117         Method JavaDoc method = null;
118         try {
119             method = Classes.getMethod(component.getClass(), pack.getName(), types);
120         }
121         catch(Exception JavaDoc e) {
122             throw new ComponentException("get method " + pack.getName() + Arrays.asList(types) + " error", e);
123         }
124         try {
125             method.invoke(component, params);
126         }
127         catch(InvocationTargetException JavaDoc e) {
128             throw new ComponentException("do method " + pack.getName() + " error", e.getTargetException());
129         }
130         catch(Exception JavaDoc e) {
131             throw new ComponentException("do method " + pack.getName() + " error", e);
132         }
133     }
134
135     public static void main(String JavaDoc[] args) {
136
137     }
138 }
139
140
Popular Tags