1 9 package org.jboss.remoting.loading; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 import org.jboss.logging.Logger; 17 18 19 26 public class ClassUtil 27 { 28 protected final static Logger log = Logger.getLogger(ClassUtil.class); 29 30 public static Object deserialize(ClassBytes cb, ClassLoader cl) 31 throws IOException , ClassNotFoundException 32 { 33 if(cb.getClassBytes() == null) 34 { 35 return null; 36 } 37 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (cb.getClassBytes()); 38 java.io.ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bis, cl); 39 Object result = ois.readObject(); 40 bis = null; 41 ois = null; 42 return result; 43 } 44 45 public static Object deserialize(byte buf[]) 46 throws IOException , ClassNotFoundException 47 { 48 ClassLoader 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 deserialize(byte buf[], ClassLoader cl) 57 throws IOException , ClassNotFoundException 58 { 59 if(buf == null) 60 { 61 return null; 62 } 63 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (buf); 64 java.io.ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bis, cl); 65 Object result = ois.readObject(); 66 bis = null; 67 ois = null; 68 return result; 69 } 70 71 public static byte[] serialize(Object obj) 72 throws java.io.IOException 73 { 74 java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream (); 75 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (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 className) 86 { 87 return (className.startsWith("[L") && className.endsWith(";")); 88 } 89 90 public static String getArrayClassPart(String className) 91 { 92 String 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 getPackageName(Class cl) 102 { 103 String n = cl.getName(); 104 int i = n.lastIndexOf("."); 105 return (i > -1) ? n.substring(0, i) : n; 106 } 107 108 public static String getShortClassName(Class cl) 109 { 110 String n = cl.getName(); 111 int i = n.lastIndexOf("."); 112 return (i > -1) ? n.substring(i + 1) : n; 113 } 114 115 121 public static Class [] getInterfacesFor(Class clazz) 122 { 123 Set set = new HashSet (); 126 addInterfaces(set, clazz); 127 return (Class []) set.toArray(new Class [set.size()]); 128 } 129 130 private static void addInterfaces(Set list, Class clazz) 131 { 132 if(clazz != null && clazz != Object .class) 133 { 134 if(clazz.isInterface() && list.contains(clazz) == false) 135 { 136 list.add(clazz); 137 } 138 Class interfaces[] = clazz.getInterfaces(); 139 if(interfaces != null && interfaces.length > 0) 140 { 141 for(int c = 0; c < interfaces.length; c++) 142 { 143 Class 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 161 public static byte[] getClassBytes(String className, ClassLoader classbyteloader) 162 { 163 String cn = null; 164 if(isArrayClass(className)) 165 { 166 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 in = null; 180 ClassLoader 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 io) 205 { 206 } 207 finally 208 { 209 if(in != null) 210 { 211 try 212 { 213 in.close(); 214 } 215 catch(Exception ig) 216 { 217 } 218 in = null; 219 } 220 } 221 } 222 } 223 return null; 224 } 225 226 233 protected static byte[] read(InputStream in) 234 throws IOException 235 { 236 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 |