KickJava   Java API By Example, From Geeks To Geeks.

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


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;
7
8 import org.jfox.ioc.exception.ComponentException;
9
10
11 /**
12  * 用来注册 Component 的 Key
13  * 由两部分组成,type和factor,type 为组件的 Class对象,
14  * factor为注册因子,用来区分同一个Component的不同注册
15  * 如果不指定factor,系统自动使用type的hashCode作为因子。
16  * <br><br>
17  * ComponentName的字符串形式: type@factor
18  * 该类也提供静态方法 newInstance 用来由字符串解析成对象
19  *
20  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
21  */

22
23 public class ComponentName {
24     /**
25      * Component type class name
26      * type 可能是接口,也可能是实现类
27      */

28     private Class JavaDoc type = null;
29
30     /**
31      * 注册Component时使用的因子,用来区分同一个Component的不同注册
32      */

33     private String JavaDoc factor = null;
34
35
36     /**
37      *
38      * @param clz
39      * @param factory
40      */

41     private ComponentName(Class JavaDoc clz, String JavaDoc factory) {
42         this.type = clz;
43         if(factory == null || factory.trim().equals("")) {
44             this.factor = "" + System.identityHashCode(clz);
45         }
46         else {
47             this.factor = factory;
48         }
49     }
50     /**
51      * 生成一个新的 ComponentName 对象
52      *
53      * @param type Component Class
54      * @return 用 compClass 和 factor 生成的 ComponentName 对象
55      */

56     public static ComponentName newInstance(Class JavaDoc type) {
57         return newInstance(type, null);
58     }
59
60     /**
61      * 生成一个新的 ComponentName 对象
62      * @param type Component Class
63      * @param factor Component因子,即Component ID,如果 factor 为空或者 null,则使用 type 的 hashCode
64      * @return 用 type 和 factor 生成的 ComponentName 对象
65      */

66     public static ComponentName newInstance(Class JavaDoc type, String JavaDoc factor) {
67         return new ComponentName(type, factor);
68     }
69
70     /**
71      * 从字符串解析成ComponentName,字符串格式 "className@factor",如:org.jfox.TestComponent@default
72      * 由className字符串对象解析成 Class 对象时,使用当前线程的 ClassLoader
73      * 如果不使用当前线程的 ClassLoader,请使用 {@link #parseString(String, ClassLoader)}
74      *
75      * 如果 factor 为空(className@),将使用装载之后的 Component Class对象的hashCode
76      * @param compName 字符串ComponentName
77      * @throws ComponentException
78      * @see #parseString(String, ClassLoader)
79      */

80     public static ComponentName parseString(String JavaDoc compName) throws ComponentException {
81         return parseString(compName, Thread.currentThread().getContextClassLoader());
82     }
83
84     /**
85      * 从字符串解析成ComponentName,字符串格式 "className@factor",如:org.jfox.TestComponent@default
86      * 由className字符串对象解析成 Class 对象时,使用指定的 ClassLoader
87      *
88      * @param compName 字符串ComponentName
89      * @param cl 用于装载组件类的ClassLoader
90      */

91     public static ComponentName parseString(String JavaDoc compName, ClassLoader JavaDoc cl) throws ComponentException {
92         String JavaDoc className = compName;
93         int index = compName.indexOf("@");
94         if(index > 0) {
95             className = compName.substring(0, index);
96             String JavaDoc factor = compName.substring(index + 1);
97             try {
98                 Class JavaDoc clz = cl.loadClass(className);
99                 return newInstance(clz, factor);
100             }
101             catch(ClassNotFoundException JavaDoc e) {
102                 throw new ComponentException(e);
103             }
104         }
105         else {
106             try {
107                 Class JavaDoc clz = cl.loadClass(className);
108                 return newInstance(clz);
109             }
110             catch(ClassNotFoundException JavaDoc e) {
111                 throw new ComponentException(e);
112             }
113         }
114
115     }
116
117     /**
118      * 得到 Component 的 Class
119      * @return
120      */

121     public Class JavaDoc getType() {
122         return type;
123     }
124
125     /**
126      * 得到 ComponentName 的因子
127      * @return
128      */

129     public String JavaDoc getFactor() {
130         return factor;
131     }
132
133     public String JavaDoc toString() {
134         return type.getName() + "@" + factor;
135     }
136
137     public int hashCode() {
138         return type.hashCode() + factor.hashCode();
139     }
140
141     /**
142      * type class 和 factor 都相等
143      * @param obj
144      * @return
145      */

146     public boolean equals(Object JavaDoc obj) {
147         if(!(obj instanceof ComponentName)) {
148             return false;
149         }
150         else {
151             ComponentName _name = (ComponentName) obj;
152             return type.equals(_name.type) && factor.equals(_name.factor);
153         }
154     }
155
156     public static void main(String JavaDoc[] args) {
157
158     }
159 }
160
161
Popular Tags