1 28 package org.objectweb.carol.rmi.multi; 29 30 import java.lang.reflect.InvocationTargetException ; 31 import java.lang.reflect.Method ; 32 import java.rmi.NoSuchObjectException ; 33 import java.rmi.Remote ; 34 import java.rmi.RemoteException ; 35 import java.util.Properties ; 36 37 import javax.rmi.CORBA.PortableRemoteObjectDelegate ; 38 39 import org.objectweb.carol.rmi.exception.NoSuchObjectExceptionHelper; 40 import org.objectweb.carol.rmi.exception.RemoteExceptionHelper; 41 import org.objectweb.carol.rmi.util.PortNumber; 42 import org.objectweb.carol.util.configuration.CarolDefaultValues; 43 import org.objectweb.carol.util.configuration.ConfigurationRepository; 44 45 51 public class JeremiePRODelegate implements PortableRemoteObjectDelegate { 52 53 56 private static final String JEREMIE_STUB_EXTENSION = "_OWStub"; 57 58 61 private int port; 62 63 66 private static final String JEREMIE_UNICAST_CLASS = "org.objectweb.jeremie.binding.moa.UnicastRemoteObject"; 67 68 71 private static Class unicastClass = null; 72 73 76 private static int nbProtocols = 0; 77 78 82 public JeremiePRODelegate() throws ClassNotFoundException { 83 unicastClass = Thread.currentThread().getContextClassLoader().loadClass(JEREMIE_UNICAST_CLASS); 85 Properties prop = ConfigurationRepository.getProperties(); 86 if (prop != null) { 87 String propertyName = CarolDefaultValues.SERVER_JEREMIE_PORT; 88 this.port = PortNumber.strToint(prop.getProperty(propertyName, "0"), propertyName); 89 } 90 } 91 92 99 public void exportObject(Remote obj) throws RemoteException { 100 if (!containsJeremieStub(obj)) { 101 return; 102 } 103 try { 104 Method exportO = unicastClass.getMethod("exportObject", new Class [] {Remote .class, Integer.TYPE}); 105 exportO.invoke(unicastClass, (new Object [] {obj, new Integer (port)})); 106 } catch (InvocationTargetException e) { 107 throw RemoteExceptionHelper.create(e.getTargetException()); 108 } catch (Exception e) { 109 throw new RemoteException ("exportObject() method fails on object '" + obj + "'", e); 110 } 111 } 112 113 120 public void unexportObject(Remote obj) throws NoSuchObjectException { 121 if (!containsJeremieStub(obj)) { 122 return; 123 } 124 try { 125 Method unexportO = unicastClass.getMethod("unexportObject", new Class [] {Remote .class, Boolean.TYPE}); 126 unexportO.invoke(unicastClass, (new Object [] {obj, Boolean.TRUE})); 127 } catch (InvocationTargetException e) { 128 throw NoSuchObjectExceptionHelper.create(e.getTargetException()); 129 } catch (Exception e) { 130 throw NoSuchObjectExceptionHelper.create("unexportObject() method fails on object '" + obj + "'", e); 131 } 132 } 133 134 146 public void connect(Remote target, Remote source) throws RemoteException { 147 } 149 150 158 public Object narrow(Object narrowFrom, Class narrowTo) throws ClassCastException { 159 if (narrowTo.isAssignableFrom(narrowFrom.getClass())) { 160 return narrowFrom; 161 } else { 162 throw new ClassCastException ("Cannot cast '" + narrowFrom.getClass().getName() + "' in '" + narrowTo.getName() + "'."); 163 } 164 } 165 166 174 public Remote toStub(Remote obj) throws NoSuchObjectException { 175 try { 176 Method exportO = unicastClass.getMethod("toStub", new Class [] {Remote .class}); 177 return (Remote ) exportO.invoke(unicastClass, (new Object [] {obj})); 178 } catch (InvocationTargetException e) { 179 throw NoSuchObjectExceptionHelper.create(e.getTargetException()); 180 } catch (Exception e) { 181 throw NoSuchObjectExceptionHelper.create("toStub() method fails on object '" + obj + "'", e); 182 } 183 } 184 185 193 private boolean containsJeremieStub(Remote r) { 194 195 if (nbProtocols == 0) { 196 nbProtocols = ConfigurationRepository.getActiveConfigurationsNumber(); 198 } 199 200 if (nbProtocols == 1) { 202 return true; 203 } 204 205 String stubName = r.getClass().getName() + JEREMIE_STUB_EXTENSION; 207 208 String resourceName = stubName.replace('.', '/'); 210 211 resourceName += ".class"; 213 214 return (Thread.currentThread().getContextClassLoader().getResource(resourceName) != null); 216 217 } 218 219 } | Popular Tags |