2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.remoting.loading;
24
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.security.AccessController;
28
import java.security.PrivilegedAction;
29
import java.util.HashSet;
31
import org.jboss.logging.Logger;
35
* ClassUtil is a set of generic class utlities.
37
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
38
* @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
39
* @version $Revision: 4145 $
41
public class ClassUtil
43
protected final static Logger log = Logger.getLogger(ClassUtil.class);
45
public static Object deserialize(ClassBytes cb, ClassLoader cl)
46
throws IOException, ClassNotFoundException
48
if(cb.getClassBytes() == null)
52
java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(cb.getClassBytes());
53
java.io.ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bis, cl);
54
Object result = ois.readObject();
60
public static Object deserialize(byte buf[])
61
throws IOException, ClassNotFoundException
63
ClassLoader cl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
67
ClassLoader loader = Thread.currentThread().getContextClassLoader();
68
if (loader == null) loader = ClassUtil.class.getClassLoader();
73
return deserialize(buf, cl);
76
public static Object deserialize(byte buf[], ClassLoader cl)
77
throws IOException, ClassNotFoundException
83
java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(buf);
84
java.io.ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bis, cl);
85
Object result = ois.readObject();
91
public static byte[] serialize(Object obj)
92
throws java.io.IOException
94
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
95
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(bos);
99
byte buf[] = bos.toByteArray();
105
public static boolean isArrayClass(String className)
107
return (className.startsWith("[L") && className.endsWith(";"));
110
public static String getArrayClassPart(String className)
112
String cn = className;
113
int i = className.indexOf("[L");
116
cn = className.substring(i + 2, className.length() - 1);
121
public static String getPackageName(Class cl)
123
String n = cl.getName();
124
int i = n.lastIndexOf(".");
125
return (i > -1) ? n.substring(0, i) : n;
128
public static String getShortClassName(Class cl)
130
String n = cl.getName();
131
int i = n.lastIndexOf(".");
132
return (i > -1) ? n.substring(i + 1) : n;
136
* given a class, recurse its dependency graph and find all its implemented interfaces
139
* @return array of interfaces
141
public static Class[] getInterfacesFor(Class clazz)
143
// use a set to eliminate duplicates, since you'll get a
144
// java.lang.ClassFormatError: $Proxy8 (Repetitive interface name))
145
Set set = new HashSet();
146
addInterfaces(set, clazz);
147
return (Class[]) set.toArray(new Class[set.size()]);
150
private static void addInterfaces(Set list, Class clazz)
152
if(clazz != null && clazz != Object.class)
154
if(clazz.isInterface() && list.contains(clazz) == false)
158
Class interfaces[] = clazz.getInterfaces();
159
if(interfaces != null && interfaces.length > 0)
161
for(int c = 0; c < interfaces.length; c++)
163
Class interfaceClass = interfaces[c];
164
if(list.contains(interfaceClass) == false)
166
list.add(interfaceClass);
168
addInterfaces(list, interfaceClass);
171
addInterfaces(list, clazz.getSuperclass());
176
* method is called to retrieve a byte array of a Class for a given class name
181
public static byte[] getClassBytes(String className, ClassLoader classbyteloader)
184
if(isArrayClass(className))
186
// if requesting an array, of course, that would be found in our class path, so we
187
// need to strip the class data and just return the class part, the other side
188
// will properly load the class as an array
189
cn = getArrayClassPart(className).replace('.', '/') + ".class";
193
cn = className.replace('.', '/') + ".class";
195
if(log.isTraceEnabled())
197
log.trace("trying to load class: " + className + " from path: " + cn);
199
InputStream in = null;
200
ClassLoader cl = classbyteloader;
204
cl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
208
return ClassLoader.getSystemClassLoader();
216
final ClassLoader fcl = cl;
217
final String fcn = cn;
218
in = (InputStream) AccessController.doPrivileged( new PrivilegedAction()
222
return fcl.getResourceAsStream(fcn);
228
log.error("error getting resource " + cn, e);
232
if(log.isTraceEnabled())
234
log.trace("looking for classes at: " + cl);
238
byte data[] = read(in);
239
if(log.isTraceEnabled())
241
log.trace("found class at classloader: " + cl);
245
catch(IOException io)
268
* simple utility method for reading bytes from an input stream
272
* @throws IOException
274
protected static byte[] read(InputStream in)
277
ByteArrayOutputStream out = new ByteArrayOutputStream();
278
byte buf[] = new byte[4096];
281
int c = in.read(buf);
286
out.write(buf, 0, c);
288
return out.toByteArray();