1 16 17 package org.springframework.remoting.rmi; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectStreamClass ; 23 import java.lang.reflect.Proxy ; 24 import java.rmi.server.RMIClassLoader ; 25 26 import org.springframework.util.ClassUtils; 27 28 55 public class CodebaseAwareObjectInputStream extends ObjectInputStream { 56 57 private final ClassLoader classLoader; 58 59 private final String codebaseUrl; 60 61 62 69 public CodebaseAwareObjectInputStream(InputStream in, String codebaseUrl) throws IOException { 70 this(in, null, codebaseUrl); 71 } 72 73 82 public CodebaseAwareObjectInputStream( 83 InputStream in, ClassLoader classLoader, String codebaseUrl) throws IOException { 84 85 super(in); 86 this.classLoader = classLoader; 87 this.codebaseUrl = codebaseUrl; 88 } 89 90 91 95 protected Class resolveClass(ObjectStreamClass classDesc) throws IOException , ClassNotFoundException { 96 try { 97 if (this.classLoader != null) { 98 return Class.forName(classDesc.getName(), false, this.classLoader); 100 } 101 else { 102 return super.resolveClass(classDesc); 104 } 105 } 106 catch (ClassNotFoundException ex) { 107 Class clazz = ClassUtils.resolvePrimitiveClassName(classDesc.getName()); 111 if (clazz != null) { 112 return clazz; 113 } 114 if (this.codebaseUrl == null) { 117 throw ex; 118 } 119 return RMIClassLoader.loadClass(this.codebaseUrl, classDesc.getName()); 120 } 121 } 122 123 127 protected Class resolveProxyClass(String [] interfaces) throws IOException , ClassNotFoundException { 128 if (this.classLoader != null) { 129 Class [] resolvedInterfaces = new Class [interfaces.length]; 131 for (int i = 0; i < interfaces.length; i++) { 132 try { 133 resolvedInterfaces[i] = Class.forName(interfaces[i], false, this.classLoader); 134 } 135 catch (ClassNotFoundException ex) { 136 if (this.codebaseUrl == null) { 137 throw ex; 138 } 139 resolvedInterfaces[i] = RMIClassLoader.loadClass(this.codebaseUrl, interfaces[i]); 140 } 141 } 142 try { 143 return Proxy.getProxyClass(this.classLoader, resolvedInterfaces); 144 } 145 catch (IllegalArgumentException ex) { 146 throw new ClassNotFoundException (null, ex); 147 } 148 } 149 else { 150 try { 152 return super.resolveProxyClass(interfaces); 153 } 154 catch (ClassNotFoundException ex) { 155 if (this.codebaseUrl == null) { 156 throw ex; 157 } 158 ClassLoader loader = RMIClassLoader.getClassLoader(this.codebaseUrl); 159 Class [] resolvedInterfaces = new Class [interfaces.length]; 160 for (int i = 0; i < interfaces.length; i++) { 161 resolvedInterfaces[i] = loader.loadClass(interfaces[i]); 162 } 163 return Proxy.getProxyClass(loader, resolvedInterfaces); 164 } 165 } 166 } 167 168 } 169 | Popular Tags |