KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > ClassDescriptor


1 package org.sapia.ubik.rmi.server.transport;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInput JavaDoc;
6 import java.io.ObjectOutput JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10
11 /**
12  * Encapsulates a class name and creates a <code>Class</code> instance
13  * using that name, from a given ClassLoader. Used to transport class
14  * info over the wire, without incurring <code>ClassNotFoundException</code>s.
15  *
16  * @author Yanick Duchesne
17  *
18  * <dl>
19  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
20  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
21  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
22  * </dl>
23  */

24 public class ClassDescriptor implements Externalizable JavaDoc {
25   private static final long serialVersionUID = 6382603348796329430l;
26   private static Map JavaDoc _primitives = new HashMap JavaDoc();
27
28   static {
29     _primitives.put(boolean.class.getName(), boolean.class);
30     _primitives.put(byte.class.getName(), byte.class);
31     _primitives.put(char.class.getName(), char.class);
32     _primitives.put(short.class.getName(), short.class);
33     _primitives.put(int.class.getName(), int.class);
34     _primitives.put(long.class.getName(), long.class);
35     _primitives.put(float.class.getName(), float.class);
36     _primitives.put(double.class.getName(), double.class);
37   }
38
39   private transient Class JavaDoc _type;
40   private String JavaDoc _className;
41   private boolean _primitive;
42
43   /* Do not use; meant for deserialization. */
44   public ClassDescriptor() {
45   }
46
47   public ClassDescriptor(Class JavaDoc type) {
48     _type = type;
49     _primitive = type.isPrimitive();
50     _className = type.getName();
51   }
52
53   public void readExternal(ObjectInput JavaDoc in)
54     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
55     _className = in.readUTF();
56     _primitive = in.readBoolean();
57   }
58
59   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
60     out.writeUTF(_className);
61     out.writeBoolean(_primitive);
62   }
63
64   public Class JavaDoc resolve(ClassLoader JavaDoc loader) throws ClassNotFoundException JavaDoc {
65     if (_primitive) {
66       Class JavaDoc clazz = (Class JavaDoc) _primitives.get(_className);
67
68       if (clazz == null) {
69         throw new ClassNotFoundException JavaDoc(_className);
70       }
71
72       return clazz;
73     } else {
74       if (_type == null) {
75         _type = loader.loadClass(_className);
76         return _type;
77       }
78       else{
79         return _type;
80       }
81     }
82   }
83 }
84
Popular Tags