KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > loading > ClassUtil


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.remoting.loading;
10
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16 import org.jboss.logging.Logger;
17
18
19 /**
20  * ClassUtil is a set of generic class utlities.
21  *
22  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
23  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
24  * @version $Revision: 1.3 $
25  */

26 public class ClassUtil
27 {
28    protected final static Logger log = Logger.getLogger(ClassUtil.class);
29
30    public static Object JavaDoc deserialize(ClassBytes cb, ClassLoader JavaDoc cl)
31          throws IOException JavaDoc, ClassNotFoundException JavaDoc
32    {
33       if(cb.getClassBytes() == null)
34       {
35          return null;
36       }
37       java.io.ByteArrayInputStream JavaDoc bis = new java.io.ByteArrayInputStream JavaDoc(cb.getClassBytes());
38       java.io.ObjectInputStream JavaDoc ois = new ObjectInputStreamWithClassLoader(bis, cl);
39       Object JavaDoc result = ois.readObject();
40       bis = null;
41       ois = null;
42       return result;
43    }
44
45    public static Object JavaDoc deserialize(byte buf[])
46          throws IOException JavaDoc, ClassNotFoundException JavaDoc
47    {
48       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
49       if(cl == null)
50       {
51          cl = ClassUtil.class.getClassLoader();
52       }
53       return deserialize(buf, cl);
54    }
55
56    public static Object JavaDoc deserialize(byte buf[], ClassLoader JavaDoc cl)
57          throws IOException JavaDoc, ClassNotFoundException JavaDoc
58    {
59       if(buf == null)
60       {
61          return null;
62       }
63       java.io.ByteArrayInputStream JavaDoc bis = new java.io.ByteArrayInputStream JavaDoc(buf);
64       java.io.ObjectInputStream JavaDoc ois = new ObjectInputStreamWithClassLoader(bis, cl);
65       Object JavaDoc result = ois.readObject();
66       bis = null;
67       ois = null;
68       return result;
69    }
70
71    public static byte[] serialize(Object JavaDoc obj)
72          throws java.io.IOException JavaDoc
73    {
74       java.io.ByteArrayOutputStream JavaDoc bos = new java.io.ByteArrayOutputStream JavaDoc();
75       java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(bos);
76       oos.writeObject(obj);
77       oos.flush();
78       bos.flush();
79       byte buf[] = bos.toByteArray();
80       bos = null;
81       oos = null;
82       return buf;
83    }
84
85    public static boolean isArrayClass(String JavaDoc className)
86    {
87       return (className.startsWith("[L") && className.endsWith(";"));
88    }
89
90    public static String JavaDoc getArrayClassPart(String JavaDoc className)
91    {
92       String JavaDoc cn = className;
93       int i = className.indexOf("[L");
94       if(i > -1)
95       {
96          cn = className.substring(i + 2, className.length() - 1);
97       }
98       return cn;
99    }
100
101    public static String JavaDoc getPackageName(Class JavaDoc cl)
102    {
103       String JavaDoc n = cl.getName();
104       int i = n.lastIndexOf(".");
105       return (i > -1) ? n.substring(0, i) : n;
106    }
107
108    public static String JavaDoc getShortClassName(Class JavaDoc cl)
109    {
110       String JavaDoc n = cl.getName();
111       int i = n.lastIndexOf(".");
112       return (i > -1) ? n.substring(i + 1) : n;
113    }
114
115    /**
116     * given a class, recurse its dependency graph and find all its implemented interfaces
117     *
118     * @param clazz
119     * @return array of interfaces
120     */

121    public static Class JavaDoc[] getInterfacesFor(Class JavaDoc clazz)
122    {
123       // use a set to eliminate duplicates, since you'll get a
124
// java.lang.ClassFormatError: $Proxy8 (Repetitive interface name))
125
Set JavaDoc set = new HashSet JavaDoc();
126       addInterfaces(set, clazz);
127       return (Class JavaDoc[]) set.toArray(new Class JavaDoc[set.size()]);
128    }
129
130    private static void addInterfaces(Set JavaDoc list, Class JavaDoc clazz)
131    {
132       if(clazz != null && clazz != Object JavaDoc.class)
133       {
134          if(clazz.isInterface() && list.contains(clazz) == false)
135          {
136             list.add(clazz);
137          }
138          Class JavaDoc interfaces[] = clazz.getInterfaces();
139          if(interfaces != null && interfaces.length > 0)
140          {
141             for(int c = 0; c < interfaces.length; c++)
142             {
143                Class JavaDoc interfaceClass = interfaces[c];
144                if(list.contains(interfaceClass) == false)
145                {
146                   list.add(interfaceClass);
147                }
148                addInterfaces(list, interfaceClass);
149             }
150          }
151          addInterfaces(list, clazz.getSuperclass());
152       }
153    }
154
155    /**
156     * method is called to retrieve a byte array of a Class for a given class name
157     *
158     * @param className
159     * @return
160     */

161    public static byte[] getClassBytes(String JavaDoc className, ClassLoader JavaDoc classbyteloader)
162    {
163       String JavaDoc cn = null;
164       if(isArrayClass(className))
165       {
166          // if requesting an array, of course, that would be found in our class path, so we
167
// need to strip the class data and just return the class part, the other side
168
// will properly load the class as an array
169
cn = getArrayClassPart(className).replace('.', '/') + ".class";
170       }
171       else
172       {
173          cn = className.replace('.', '/') + ".class";
174       }
175       if(log.isTraceEnabled())
176       {
177          log.trace("trying to load class: " + className + " from path: " + cn);
178       }
179       InputStream JavaDoc in = null;
180       ClassLoader JavaDoc cl = classbyteloader;
181
182       if(cl == null)
183       {
184          cl = ClassLoader.getSystemClassLoader();
185       }
186       if(cl != null)
187       {
188          in = cl.getResourceAsStream(cn);
189          if(in != null)
190          {
191             if(log.isTraceEnabled())
192             {
193                log.trace("looking for classes at: " + cl);
194             }
195             try
196             {
197                byte data[] = read(in);
198                if(log.isTraceEnabled())
199                {
200                   log.trace("found class at classloader: " + cl);
201                }
202                return data;
203             }
204             catch(IOException JavaDoc io)
205             {
206             }
207             finally
208             {
209                if(in != null)
210                {
211                   try
212                   {
213                      in.close();
214                   }
215                   catch(Exception JavaDoc ig)
216                   {
217                   }
218                   in = null;
219                }
220             }
221          }
222       }
223       return null;
224    }
225
226    /**
227     * simple utility method for reading bytes from an input stream
228     *
229     * @param in
230     * @return
231     * @throws IOException
232     */

233    protected static byte[] read(InputStream JavaDoc in)
234          throws IOException JavaDoc
235    {
236       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
237       byte buf[] = new byte[4096];
238       while(true)
239       {
240          int c = in.read(buf);
241          if(c < 0)
242          {
243             break;
244          }
245          out.write(buf, 0, c);
246       }
247       return out.toByteArray();
248    }
249
250 }
251
Popular Tags