KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > ComponentMetaFactory


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;
8
9 import org.jfox.ioc.depend.Dependency;
10 import org.jfox.ioc.depend.DependencyPack;
11 import org.jfox.ioc.exception.ComponentException;
12 import org.jfox.ioc.ext.InterceptableComponent;
13 import org.jfox.ioc.factory.ComponentFactory;
14 import org.jfox.ioc.factory.ConstrComponentFactory;
15 import org.jfox.ioc.factory.DynProxyComponentFactory;
16 import org.jfox.ioc.factory.InstanceComponentFactory;
17 import org.jfox.ioc.factory.PropertyComponentFactory;
18
19 /**
20  * 用来生成 ComponentMeta 的工厂,可以根据用户提供的参数自动生成ComponentMeta需要的ComponentName 和 ComponentFactory 对象
21  *
22  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
23  */

24
25 public class ComponentMetaFactory {
26
27     /**
28      * 根据组件实例生成ComponentMeta。
29      * 这种方式生成的ComponentMeta的singleton只能是true。
30      * @param component 组件实例
31      */

32     public static ComponentMeta getComponentMeta(Component component) {
33         return new ComponentMeta(new InstanceComponentFactory(component));
34     }
35
36     /**
37      * 根据组件类名构造ComponentMeta。其它参数使用默认值。
38      * 使用没有参数的默认构造器。
39      * @param implementation
40      * @return
41      * @throws ComponentException
42      */

43     public static ComponentMeta getComponentMeta(Class JavaDoc implementation) throws ComponentException {
44         return getComponentMeta(implementation,implementation);
45     }
46
47     public static ComponentMeta getComponentMeta(Class JavaDoc superClass,Class JavaDoc implementation) throws ComponentException {
48         return getComponentMeta(superClass,implementation, Dependency.EMPTY_PARAMETERS);
49     }
50
51     public static ComponentMeta getComponentMeta(Class JavaDoc implementation, Dependency[] constructorTypes) throws ComponentException {
52         return getComponentMeta(implementation,implementation,constructorTypes,DependencyPack.EMPTY_PARAMETER_PACKS,true,false);
53     }
54
55     public static ComponentMeta getComponentMeta(Class JavaDoc superClass,Class JavaDoc implementation, Dependency[] constructorTypes) throws ComponentException {
56         return getComponentMeta(superClass,implementation, constructorTypes, DependencyPack.EMPTY_PARAMETER_PACKS, true, false);
57     }
58
59     public static ComponentMeta getComponentMeta(Class JavaDoc superClass,
60                                                  Class JavaDoc implementation,
61                                                  Dependency[] constructTypes,
62                                                  DependencyPack[] setterParams,
63                                                  boolean singleton,
64                                                  boolean typeSingleton) throws ComponentException {
65         ComponentName name = ComponentName.newInstance(superClass);
66         return getComponentMeta(name,implementation,constructTypes,setterParams,singleton,typeSingleton);
67     }
68
69     /**
70      * 根据组件实例生成ComponentMeta,可以设置 typeSingleton。
71      * 这种方式生成的ComponentMeta的singleton只能是true。
72      * @param component 组件实例
73      * @param typeSingleton 类单例,该类组件只能注册一次
74      */

75     public static ComponentMeta getComponentMeta(Component component, boolean typeSingleton) {
76         ComponentMeta meta = getComponentMeta(component);
77         meta.setTypeSingleton(typeSingleton);
78         return meta;
79     }
80
81     /**
82      * 根据ComponentName构造ComponentMeta。其它参数使用默认值.
83      * 使用没有参数的默认构造器。
84      * @param name 组件注册的名称,已经包含了组件的类名
85      * @throws ComponentException
86      */

87     public static ComponentMeta getComponentMeta(ComponentName name)
88             throws ComponentException {
89         return getComponentMeta(name,name.getType());
90     }
91
92     public static ComponentMeta getComponentMeta(ComponentName name, Class JavaDoc implementation)
93             throws ComponentException {
94         return getComponentMeta(name,implementation,Dependency.EMPTY_PARAMETERS);
95     }
96
97     public static ComponentMeta getComponentMeta(ComponentName name,
98                                                  Dependency[] constructorTypes)
99             throws ComponentException {
100         return getComponentMeta(name,name.getType(),constructorTypes);
101     }
102
103     public static ComponentMeta getComponentMeta(ComponentName name,
104                                                  Class JavaDoc implementation,
105                                                  Dependency[] constructorTypes)
106             throws ComponentException {
107         return getComponentMeta(name,implementation,constructorTypes, DependencyPack.EMPTY_PARAMETER_PACKS);
108     }
109     /**
110      * 根据组件名称、构造器参数、setter参数构造ComponentMeta。其它参数使用默认值。
111      *
112      * @param name 组件注册的名称,已经包含了组件的类名
113      * @param constructorTypes 构造器参数类型
114      * @throws ComponentException
115      */

116     public static ComponentMeta getComponentMeta(ComponentName name,
117                                                  Class JavaDoc implementation,
118                                                  Dependency[] constructorTypes,
119                                                  DependencyPack[] setterParams
120                                                     ) throws ComponentException {
121         return getComponentMeta(name,implementation,constructorTypes,setterParams,true,false);
122     }
123
124     public static ComponentMeta getComponentMeta(ComponentName name,
125                                                  Dependency[] constructCompParamPack,
126                                                  DependencyPack[] setterParams,
127                                                  boolean singleton,
128                                                  boolean typeSingleton
129                                                  )
130
131             throws ComponentException {
132         return getComponentMeta(name,name.getType(),constructCompParamPack,setterParams,singleton,typeSingleton);
133     }
134
135     public static ComponentMeta getComponentMeta(ComponentName name,
136                                                  Class JavaDoc implementation,
137                                                  Dependency[] constrctCompParamPack,
138                                                  DependencyPack[] setterParams,
139                                                  boolean singleton,
140                                                  boolean typeSingleton)
141
142             throws ComponentException {
143         ComponentFactory componentFactory = null;
144         Class JavaDoc superClass = name.getType();
145         if(!superClass.isAssignableFrom(implementation)){
146             throw new ComponentException("component name " + name + " 's super class " + superClass.getName() + " is not the superClass the implementation class " + implementation.getName());
147         }
148
149         if(InterceptableComponent.class.isAssignableFrom(implementation)) {
150             componentFactory = new DynProxyComponentFactory(name, implementation, constrctCompParamPack);
151         }
152         else {
153             componentFactory = new ConstrComponentFactory(name, implementation, constrctCompParamPack);
154         }
155         if(setterParams != null && setterParams.length != 0) {
156             componentFactory = new PropertyComponentFactory((ConstrComponentFactory) componentFactory, setterParams);
157         }
158
159         ComponentMeta meta = new ComponentMeta(componentFactory);
160         meta.setTypeSingleton(typeSingleton);
161         meta.setSingleton(singleton);
162         return meta;
163     }
164
165     public static void main(String JavaDoc[] args) {
166
167     }
168 }
169
170
Popular Tags