1 18 package org.apache.activemq.util; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectStreamClass ; 24 import java.lang.reflect.Proxy ; 25 26 27 public class ClassLoadingAwareObjectInputStream extends ObjectInputStream { 28 29 private static final ClassLoader FALLBACK_CLASS_LOADER = ClassLoadingAwareObjectInputStream.class.getClassLoader(); 30 31 public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException { 32 super(in); 33 } 34 35 protected Class resolveClass(ObjectStreamClass classDesc) throws IOException , ClassNotFoundException { 36 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 37 return load(classDesc.getName(), cl); 38 } 39 40 protected Class resolveProxyClass(String [] interfaces) throws IOException , ClassNotFoundException { 41 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 42 Class [] cinterfaces = new Class [interfaces.length]; 43 for (int i = 0; i < interfaces.length; i++) 44 cinterfaces[i] = load(interfaces[i], cl); 45 46 try { 47 return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces); 48 } catch (IllegalArgumentException e) { 49 throw new ClassNotFoundException (null, e); 50 } 51 } 52 53 private Class load(String className, ClassLoader cl) throws ClassNotFoundException { 54 try { 55 return ClassLoading.loadClass(className, cl); 56 } catch ( ClassNotFoundException e ) { 57 return ClassLoading.loadClass(className, FALLBACK_CLASS_LOADER); 58 } 59 } 60 61 } 62 | Popular Tags |