KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.jfox.ioc.depend.Dependency;
13 import org.jfox.ioc.depend.DependencyPack;
14 import org.jfox.ioc.depend.RefDependency;
15 import org.jfox.ioc.exception.ComponentException;
16 import org.jfox.ioc.ext.ManagableComponent;
17 import org.jfox.ioc.ext.SingletonComponent;
18 import org.jfox.ioc.factory.ComponentFactory;
19 import org.jfox.ioc.factory.ConstrComponentFactory;
20 import org.jfox.ioc.factory.InstanceComponentFactory;
21 import org.jfox.ioc.factory.PropertyComponentFactory;
22 import org.jfox.ioc.logger.Logger;
23 import org.jfox.ioc.management.ManagementInfo;
24
25 /**
26  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
27  */

28
29 public class ComponentMeta implements Comparable JavaDoc {
30
31     public final static boolean DEFAULT_SINGLETON = true;
32
33     public final static boolean DEFAULT_TYPE_SINGLETON = false;
34
35     public final static int DEFAULT_PRIORITY = 0;
36
37     private static Logger logger = Logger.getLogger(ComponentMeta.class
38             .getName());
39
40     /**
41      * 该组件的描述
42      */

43     protected String JavaDoc description = "";
44
45     /**
46      * 组件是否单例, true时Registry只会生成该组件的一个实例,
47      * false时Registry为每次getComponentInstance调用生成一个实例, 默认为 true <br>
48      * <br>
49      * 注意:如果以对象方式注册到Registry中,那么该组件必定是singleton的
50      */

51     protected boolean singleton = DEFAULT_SINGLETON;
52
53     /**
54      * 该类型的组件只能在Registry中注册一次,不检查其子类。 如果 B extents A,A组件注册之后,就不能再次注册了,但是 B
55      * 仍然可以注册。
56      */

57     protected boolean typeSingleton = DEFAULT_TYPE_SINGLETON;
58
59     /**
60      * 优先级,在以模块方式部署时,决定组件的注册顺序, 优先级高的组件,会先注册到Registry中,值小的优先级高
61      */

62     protected int priority = DEFAULT_PRIORITY;
63
64     /**
65      * 用来生成该组件实例的Factory
66      */

67     protected ComponentFactory compFactory;
68
69     /**
70      * 组件实例。如果组件是singleton,则缓存该实例;如果组件是非singleton,则缓存最后一个实例
71      */

72     protected Component component = null;
73
74     /**
75      * 组件引用的其它组件名称列表,在组件实例化之后,才有值
76      */

77     protected List JavaDoc refComponentNames = new ArrayList JavaDoc();
78
79     private String JavaDoc module = "";
80     /**
81      * 该组件所在的模块module
82      */

83     private String JavaDoc moduleDesc = "Default Module";
84
85     /**
86      * 该组件所在的模块所在的目录
87      */

88     private String JavaDoc moduleDir = null;
89
90     protected ComponentMeta(ComponentFactory compFactory) {
91         this.compFactory = compFactory;
92     }
93
94     /**
95      * /** 组件是否单例, true时Registry只会生成该组件的一个实例,
96      * false时Registry为每次getComponentInstance调用生成一个实例, 默认为 true <br>
97      * <br>
98      * 注意:如果以对象方式注册到Registry中,那么该组件必定是singleton的; 如果组件实现{@link SingletonComponent},那么也必定是singleton的
99      */

100     public boolean isSingleton() {
101         return SingletonComponent.class
102                 .isAssignableFrom(getComponentImplementation())
103                 || singleton;
104     }
105
106     public void setSingleton(boolean singleton) {
107         // 使用 InstanceComponentFactory 不能设置 singleton,只能是单例的
108
if (!(compFactory instanceof InstanceComponentFactory)) {
109             this.singleton = singleton;
110         }
111     }
112
113     public boolean isTypeSingleton() {
114         return typeSingleton;
115     }
116
117     public void setTypeSingleton(boolean typeSingleton) {
118         this.typeSingleton = typeSingleton;
119     }
120
121     public int getPriority() {
122         return priority;
123     }
124
125     public void setPriority(int priority) {
126         this.priority = priority;
127     }
128
129     /**
130      * get component instace by meta attention: will not init & start component
131      * by Registry!!!
132      *
133      * @param paramPacks
134      * 在实例化后,要进行的setter操作,进行setter之后,才进行 init和start 操作
135      * @return
136      * @throws ComponentException
137      */

138     public synchronized Component getComponentInstance(
139             DependencyPack[] paramPacks) throws ComponentException {
140         logger.debug("get component instance "
141                 + getComponentFactory().getComponentName());
142
143         if (isSingleton()) {
144             if (component == null) {
145                 logger.debug("make new component instance, name: "
146                         + getComponentFactory().getComponentName()
147                         + ", component factory: " + getComponentFactory());
148                 ComponentFactory factory = getComponentFactory();
149                 if (paramPacks != null && paramPacks.length != 0) {
150                     if (factory instanceof ConstrComponentFactory) {
151                         factory = new PropertyComponentFactory(
152                                 (ConstrComponentFactory) factory, paramPacks);
153                     }
154                     else if (factory instanceof PropertyComponentFactory) {
155                         ((PropertyComponentFactory) factory)
156                                 .addParameterPack(paramPacks);
157                     }
158                 }
159
160                 ComponentContext context = initComponentContext();
161                 if(factory instanceof PropertyComponentFactory){
162                     // properryComponentFactory will set ComponentContext before setters
163
component = ((PropertyComponentFactory)factory).makeComponent(context);
164                 }
165                 else {
166                     // set component ctx
167
component = factory.makeComponent();
168                     component.setComponentContext(context);
169                 }
170                 logger.debug("make new instance: " + component);
171                 buildRefComponentNames();
172
173                 // try to int & start only when created
174
if (getRegistry().isStarted()) {
175                     getRegistry().initComponent(this, component);
176                     getRegistry().startComponent(this, component);
177                 }
178             }
179             else {
180                 if (paramPacks != null && paramPacks.length != 0) {
181                     ComponentFactory factory = getComponentFactory();
182                     factory = new PropertyComponentFactory(
183                             new InstanceComponentFactory(component), paramPacks);
184                     component = factory.makeComponent();
185                 }
186             }
187         }
188         else {
189             logger.debug("make new component instance with component name "
190                     + getComponentFactory().getComponentName()
191                     + ",component factory " + getComponentFactory());
192             ComponentFactory factory = getComponentFactory();
193             if (paramPacks != null && paramPacks.length != 0) {
194                 if (factory instanceof ConstrComponentFactory) {
195                     factory = new PropertyComponentFactory(
196                             (ConstrComponentFactory) factory, paramPacks);
197                 }
198                 else if (factory instanceof PropertyComponentFactory) {
199                     ((PropertyComponentFactory) factory)
200                             .addParameterPack(paramPacks);
201                 }
202             }
203
204             ComponentContext context = initComponentContext();
205             Component comp = null;
206             if(factory instanceof PropertyComponentFactory) {
207                 // properryComponentFactory will set ComponentContext before setters
208
comp = ((PropertyComponentFactory) factory).makeComponent(context);
209             }
210             else {
211                 // set component ctx
212
comp = factory.makeComponent();
213                 comp.setComponentContext(context);
214             }
215
216             // only build at the first time
217
if(component == null) {
218                 buildRefComponentNames();
219             }
220             component = comp;
221
222             // try to int & start only when created
223
if (getRegistry().isStarted()) {
224                 getRegistry().initComponent(this, component);
225                 getRegistry().startComponent(this, component);
226             }
227
228         }
229         return component;
230     }
231
232     // find the depency component ref
233
private void buildRefComponentNames() {
234         // properties ref
235
if (compFactory instanceof PropertyComponentFactory) {
236             DependencyPack[] packs = ((PropertyComponentFactory) compFactory)
237                     .getParameterPacks();
238             for (int i = 0; packs != null && i < packs.length; i++) {
239                 Dependency param = packs[i].getParameter();
240                 if (param instanceof RefDependency) {
241                     refComponentNames.add(((RefDependency) param)
242                             .getRefComponentName());
243                 }
244             }
245         }
246
247         // constructor ref
248
Dependency[] params = getComponentFactory().getParameters();
249         for (int i = 0; params != null && i < params.length; i++) {
250             Dependency param = params[i];
251             if (param instanceof RefDependency) {
252                 refComponentNames.add(((RefDependency) param)
253                         .getRefComponentName());
254             }
255         }
256
257     }
258
259     public ComponentName getComponentName() {
260         return getComponentFactory().getComponentName();
261     }
262
263     public Class JavaDoc getComponentImplementation() {
264         return getComponentFactory().getImplementation();
265     }
266
267     public ComponentFactory getComponentFactory() {
268         return compFactory;
269     }
270
271     public ComponentName[] getRefComponentNames() {
272         return (ComponentName[]) refComponentNames
273                 .toArray(new ComponentName[refComponentNames.size()]);
274     }
275
276     /**
277      * return the Registry return null if this Component is not registry yet
278      *
279      * @return
280      */

281     public Registry getRegistry() {
282         return getComponentFactory().getRegistry();
283     }
284
285     void setRegistry(Registry registry) {
286         getComponentFactory().setRegistry(registry);
287     }
288
289     public ManagementInfo getManagementInfo() {
290         if (!isSingleton()) {
291             return null;
292         }
293         else if (component == null) {
294             return null;
295         }
296         else {
297             if (component instanceof ManagableComponent) {
298                 return ManagementInfo.getManagementInfoByAnnotation(component
299                         .getClass());
300             }
301             else {
302                 return null;
303             }
304         }
305     }
306
307     public String JavaDoc getDescription() {
308         return description;
309     }
310
311     public void setDescription(String JavaDoc description) {
312         this.description = description;
313     }
314
315     /**
316      * @return Returns the module.
317      */

318     public String JavaDoc getModule() {
319         return module;
320     }
321     
322
323     /**
324      * @param module The module to set.
325      */

326     public void setModule(String JavaDoc module) {
327         this.module = module;
328     }
329     
330
331     public String JavaDoc getModuleDesc() {
332         return moduleDesc;
333     }
334
335     public void setModuleDescription(String JavaDoc module) {
336         this.moduleDesc = module;
337     }
338
339     public String JavaDoc getModuleDir() {
340         return moduleDir;
341     }
342
343     public void setModuleDir(String JavaDoc moduleDir) {
344         this.moduleDir = moduleDir;
345     }
346
347     private ComponentContext initComponentContext() {
348         ComponentContext ctx = new ComponentContext();
349         if (moduleDir == null) {
350             // if register by manual, may not set module, so use the classpath
351
// as
352
moduleDir = getComponentImplementation().getProtectionDomain()
353                     .getCodeSource().getLocation().getPath();
354         }
355         ctx.setRegistry(getRegistry());
356         ctx.setModule(module);
357         ctx.setModuleDesc(moduleDesc);
358         ctx.setModuleDir(moduleDir);
359         ctx.setSingleton(singleton);
360         ctx.setTypeSingleton(typeSingleton);
361         return ctx;
362     }
363
364     /**
365      * 用来按照priority 值对 ComponentMeta 排序。 priority 小的优先级大,在 Registry 启动的时候,会先注册
366      *
367      * @param o
368      * @return
369      */

370     public int compareTo(Object JavaDoc o) {
371         if (!(o instanceof ComponentMeta))
372             return 0;
373         ComponentMeta cm = (ComponentMeta) o;
374         if (priority > cm.priority) {
375             return 1;
376         }
377         else if (priority < cm.priority) {
378             return -1;
379         }
380         else {
381             return getComponentName().toString().compareTo(
382                     cm.getComponentName().toString());
383         }
384     }
385
386     public String JavaDoc toString() {
387         return "ComponentMeta{" + "componentType="
388                 + getComponentImplementation().getName() + ", compFactory="
389                 + compFactory.toString() + ", singleton=" + singleton
390                 + ", typeSingleton=" + typeSingleton + "}";
391     }
392
393 }
Popular Tags