KickJava   Java API By Example, From Geeks To Geeks.

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


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
7 package org.jfox.ioc.factory;
8
9 import org.jfox.ioc.Component;
10 import org.jfox.ioc.ComponentName;
11 import org.jfox.ioc.Registry;
12 import org.jfox.ioc.depend.Dependency;
13 import org.jfox.ioc.exception.ComponentException;
14
15
16 /**
17  * 用来生成组件对象实例的工厂。
18  * <br>
19  * 在通过Registry取得组件实例的时候,Registry会调用该组件的工厂来生成实例
20  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
21  */

22
23 public abstract class ComponentFactory {
24
25     /**
26      * 生成一个组件实例
27      *
28      * @return Component instance
29      */

30     public abstract Component makeComponent() throws ComponentException;
31
32     /**
33      * 获得组件的注册名称
34      * @return
35      */

36     public abstract ComponentName getComponentName();
37
38     /**
39      * 获得组件的 Class
40      */

41     public abstract Class JavaDoc getImplementation();
42
43     /**
44      * 组件生成时,用于构造器的参数
45      */

46     public abstract Dependency[] getParameters();
47
48     /**
49      * 该组件所在Registry
50      */

51     public abstract Registry getRegistry();
52
53     /**
54      * 设置组件所在的Registry,一般不用用户手动设置,
55      * 在将组件注册到Registry时,Registry会自动回调该方法进行设置
56      * @param registry
57      */

58     public abstract void setRegistry(Registry registry);
59
60     /**
61      * 根据提供的参数数组,解析出参数所指向的组件的实例数组
62      * @param params 组件引用参数
63      * @return 组件实例数组
64      * @throws org.jfox.ioc.exception.ComponentException
65      */

66     protected Object JavaDoc[] resolveParameters(Dependency[] params) throws ComponentException {
67         if(params == null || params.length == 0) {
68             return new Object JavaDoc[]{};
69         }
70         else {
71             Object JavaDoc[] objs = new Object JavaDoc[params.length];
72             for(int i = 0; i < params.length; i++) {
73                 objs[i] = params[i].resolveValue(getRegistry());
74             }
75             return objs;
76         }
77     }
78
79     /**
80      * 根据提供的参数数组,得到参数所执行的组件的类型数组
81      * @param params
82      * @return
83      */

84     protected Class JavaDoc[] getParameterTypes(Dependency[] params) {
85         if(params == null) {
86             return new Class JavaDoc[]{};
87         }
88         else {
89             Class JavaDoc[] types = new Class JavaDoc[params.length];
90             for(int i = 0; i < params.length; i++) {
91                 types[i] = params[i].getType();
92             }
93             return types;
94         }
95     }
96
97 }
Popular Tags