1 25 26 package org.objectweb.easybeans.component.smartclient.spi; 27 28 import java.rmi.NoSuchObjectException ; 29 import java.rmi.Remote ; 30 import java.rmi.RemoteException ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 34 import javax.rmi.CORBA.PortableRemoteObjectDelegate ; 35 36 41 public class ProDelegate implements PortableRemoteObjectDelegate { 42 43 46 private static Logger logger = Logger.getLogger(ProDelegate.class.getName()); 47 48 51 private static final String CAROL_PRO_DELEGATE = "org.objectweb.carol.rmi.multi.MultiPRODelegate"; 52 53 57 private static ClassLoader classLoader; 58 59 62 private PortableRemoteObjectDelegate wrapped = null; 63 64 67 public ProDelegate() { 68 ClassLoader old = Thread.currentThread().getContextClassLoader(); 69 Thread.currentThread().setContextClassLoader(classLoader); 70 try { 71 Class clazz = null; 72 try { 73 clazz = classLoader.loadClass(CAROL_PRO_DELEGATE); 74 } catch (ClassNotFoundException e) { 75 throw new IllegalStateException ("Cannot find the '" + CAROL_PRO_DELEGATE + "' class.", e); 76 } 77 try { 78 wrapped = (PortableRemoteObjectDelegate ) clazz.newInstance(); 79 } catch (InstantiationException e) { 80 throw new IllegalStateException ("Cannot build an instance of the '" + CAROL_PRO_DELEGATE + "' class.", e); 81 } catch (IllegalAccessException e) { 82 throw new IllegalStateException ("Cannot build an instance of the '" + CAROL_PRO_DELEGATE + "' class.", e); 83 } 84 } finally { 85 Thread.currentThread().setContextClassLoader(old); 86 } 87 } 88 89 92 private void check() { 93 if (classLoader == null) { 94 throw new IllegalStateException ("No classloader was set previously. Invalid call."); 95 } 96 97 } 98 99 110 public void connect(final Remote target, final Remote source) throws RemoteException { 111 check(); 112 if (logger.isLoggable(Level.FINE)) { 113 logger.log(Level.FINE, "connect '" + target + "' with '" + source + "'."); 114 } 115 ClassLoader old = Thread.currentThread().getContextClassLoader(); 116 Thread.currentThread().setContextClassLoader(classLoader); 117 try { 118 wrapped.connect(target, source); 119 } finally { 120 Thread.currentThread().setContextClassLoader(old); 121 } 122 123 } 124 125 132 public void exportObject(final Remote obj) throws RemoteException { 133 check(); 134 if (logger.isLoggable(Level.FINE)) { 135 logger.log(Level.FINE, "exportObject '" + obj + "'."); 136 } 137 ClassLoader old = Thread.currentThread().getContextClassLoader(); 138 Thread.currentThread().setContextClassLoader(classLoader); 139 try { 140 wrapped.exportObject(obj); 141 } finally { 142 Thread.currentThread().setContextClassLoader(old); 143 } 144 145 } 146 147 155 public Object narrow(final Object narrowFrom, final Class narrowTo) throws ClassCastException { 156 check(); 157 if (logger.isLoggable(Level.FINE)) { 158 logger.log(Level.FINE, "Narrowing '" + narrowFrom + "' to '" + narrowTo + "'."); 159 } 160 ClassLoader old = Thread.currentThread().getContextClassLoader(); 161 Thread.currentThread().setContextClassLoader(classLoader); 162 try { 163 return wrapped.narrow(narrowFrom, narrowTo); 164 } finally { 165 Thread.currentThread().setContextClassLoader(old); 166 } 167 } 168 169 178 public Remote toStub(final Remote obj) throws NoSuchObjectException { 179 check(); 180 if (logger.isLoggable(Level.FINE)) { 181 logger.log(Level.FINE, "toStub '" + obj + "'."); 182 } 183 ClassLoader old = Thread.currentThread().getContextClassLoader(); 184 Thread.currentThread().setContextClassLoader(classLoader); 185 try { 186 return wrapped.toStub(obj); 187 } finally { 188 Thread.currentThread().setContextClassLoader(old); 189 } 190 } 191 192 199 public void unexportObject(final Remote obj) throws NoSuchObjectException { 200 check(); 201 if (logger.isLoggable(Level.FINE)) { 202 logger.log(Level.FINE, "unexportObject '" + obj + "'."); 203 } 204 ClassLoader old = Thread.currentThread().getContextClassLoader(); 205 Thread.currentThread().setContextClassLoader(classLoader); 206 try { 207 wrapped.unexportObject(obj); 208 } finally { 209 Thread.currentThread().setContextClassLoader(old); 210 } 211 } 212 213 217 public static void setClassLoader(final ClassLoader cl) { 218 classLoader = cl; 219 } 220 221 } 222 | Popular Tags |