1 27 28 package org.objectweb.fractal.rmi.io; 29 30 import org.objectweb.jonathan.apis.binding.Identifier; 31 import org.objectweb.jonathan.apis.binding.NamingContext; 32 import org.objectweb.jonathan.apis.kernel.Context; 33 import org.objectweb.jonathan.apis.kernel.ContextFactory; 34 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.ObjectInputStream ; 38 import java.io.ObjectStreamClass ; 39 import java.rmi.server.RMIClassLoader ; 40 41 44 45 public class RmiObjectInputStream extends ObjectInputStream { 46 47 51 52 protected NamingContext domain; 53 54 58 59 protected ContextFactory contextFactory; 60 61 64 65 protected String codeBase; 66 67 77 78 public RmiObjectInputStream ( 79 final InputStream is, 80 final NamingContext domain, 81 final ContextFactory contextFactory) throws IOException 82 { 83 super(is); 84 enableResolveObject(true); 85 this.domain = domain; 86 this.contextFactory = contextFactory; 87 this.codeBase = readUTF(); 88 89 ClassLoader l; 90 if (codeBase.length() == 0) { 91 l = getClass().getClassLoader(); 92 } else { 93 Thread t = Thread.currentThread(); 94 ClassLoader current = t.getContextClassLoader(); 95 try { 96 t.setContextClassLoader(getClass().getClassLoader()); 97 l = RMIClassLoader.getClassLoader(codeBase); 98 } finally { 99 t.setContextClassLoader(current); 100 } 101 } 102 if (l != null) { 103 Thread.currentThread().setContextClassLoader(l); 104 } 105 } 106 107 121 122 protected Object resolveObject (final Object obj) throws IOException { 123 if (obj instanceof Ref) { 124 try { 125 Ref ref = (Ref)obj; 126 Identifier id = domain.decode(ref.id, 0, ref.id.length); 127 Context hints = contextFactory.newContext(); 128 hints.addElement("interface_type", String .class, ref.type, (char)0); 129 Object newObj = id.bind(new Identifier[] {id}, hints); 130 hints.release(); 131 return newObj; 132 } catch (Exception e) { 133 throw new IOException ("cannot bind to object: " + e); 134 } 135 } else { 136 return obj; 137 } 138 } 139 140 protected Class resolveClass (ObjectStreamClass desc) 141 throws IOException , ClassNotFoundException 142 { 143 try { 144 try { 145 return RMIClassLoader.loadClass((String )codeBase, desc.getName()); 146 } catch (ClassNotFoundException e) { 147 return getClass().getClassLoader().loadClass(desc.getName()); 148 } 149 } catch (ClassNotFoundException e) { 150 System.err.println("WARNING: " + e.toString()); 151 throw e; 152 } catch (IOException e) { 153 System.err.println("WARNING: " + e.toString()); 154 throw e; 155 } 156 } 157 } 158 | Popular Tags |