KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > depend > ObjectDependency


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.depend;
7
8 import org.jfox.ioc.Registry;
9 import org.jfox.ioc.exception.ComponentException;
10 import org.jfox.ioc.util.Classes;
11
12 /**
13  * 该参数类型表明对一个实例对象的直接引用。
14  *
15  *
16  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
17  */

18
19 public class ObjectDependency implements Dependency {
20
21     private String JavaDoc literalValue = null;
22     private Object JavaDoc value = null;
23     private Class JavaDoc type = null;
24     private String JavaDoc stype = null;
25
26     public ObjectDependency(Object JavaDoc obj) {
27         this.value = obj;
28         this.type = obj.getClass();
29     }
30
31     /**
32      * type 有可能是 value 的超类,也有可能 type 是原生类型。
33      *
34      * @param value
35      * @param type
36      */

37     public ObjectDependency(Object JavaDoc value, Class JavaDoc type) {
38         this.value = value;
39         this.type = type;
40     }
41
42     public ObjectDependency(String JavaDoc literalValue, String JavaDoc type) {
43         this.literalValue = literalValue;
44         this.stype = type;
45     }
46
47     public ObjectDependency(String JavaDoc literalValue, Class JavaDoc type) {
48         this.literalValue = literalValue;
49         this.type = type;
50     }
51
52    /**
53     * 实例的类型
54     */

55     public Class JavaDoc getType() {
56         return type;
57     }
58
59     public String JavaDoc getLiteralValue() {
60         if(literalValue == null) {
61             literalValue = value.toString();
62         }
63         return literalValue;
64
65     }
66
67     /**
68      * 解析 ObjectParameter 的值。
69      * 如果 ObjectParameter 是以 (String literalValue, String type) 构造的,
70      * 会尝试先装载该类,然后以一个String参数的构造器来构造该对象。
71      *
72      * 类装载器的问题:
73      * 如果是程序方式注册的,会使用 ClassLoaderRepository 装载参数类型,
74      * 如果该参数类型之前没有被LocalClassLoader装载过,可能出现无法装载问题;
75      * 如果是使用部署器部署的,部署器会先使用 Module 的 LocalClassLoader 装载好参数类,
76      * 然后再以(Object value, Class type)构造 ObjectParameter 对象,所以不存在该问题。
77      *
78      * @param registry
79      * @return
80      * @throws org.jfox.ioc.exception.ComponentException
81      */

82     public Object JavaDoc resolveValue(Registry registry) throws ComponentException {
83         if(type == null && stype != null) {
84             //检查原生类型
85
if(Classes.isPrimitiveClass(stype)) {
86                 type = Classes.loadPrimitiveClass(stype);
87             }
88             else {
89                 try {
90                     // 使用 ClassLoaderRepository 装载类
91
type = registry.loadClass(stype);
92                 }
93                 catch(Exception JavaDoc e) {
94                     throw new ComponentException("load type class " + stype + " error", e);
95                 }
96             }
97         }
98         /**
99          * 如果没有值,使用 String 参数的构造构造一个对象
100          */

101         if(value == null) {
102             try {
103                 value = Classes.newObject(type,literalValue);
104                 value = type.getConstructor(new Class JavaDoc[]{String JavaDoc.class}).newInstance(new Object JavaDoc[]{literalValue});
105             }
106             catch(Exception JavaDoc e) {
107                 throw new ComponentException("resolve component parameter value error", e);
108             }
109         }
110         return value;
111     }
112
113     public String JavaDoc toString() {
114         return "ComponentInstanceParameter{" +
115                 "type=" + type +
116                 ", value=" + (value == null ? "NOT RESOVLED" : value.toString()) +
117                 "}";
118     }
119
120     public static void main(String JavaDoc[] args) {
121
122     }
123
124 }
125
126
Popular Tags