1 28 package org.objectweb.carol.jndi.spi; 29 30 import java.io.Serializable ; 31 import java.rmi.Remote ; 32 33 import javax.naming.Context ; 34 import javax.naming.Name ; 35 import javax.naming.NamingException ; 36 import javax.naming.Reference ; 37 import javax.naming.Referenceable ; 38 import javax.naming.spi.ObjectFactory ; 39 import javax.rmi.CORBA.PortableRemoteObjectDelegate ; 40 41 import org.omg.CORBA.ORB ; 42 import org.omg.PortableServer.POA ; 43 44 import org.objectweb.carol.jndi.ns.IIOPCosNaming; 45 import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper; 46 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource; 47 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper; 48 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 49 import org.objectweb.carol.util.configuration.ConfigurationRepository; 50 51 import com.sun.jndi.rmi.registry.RemoteReference; 52 53 57 public class IIOPContext extends AbsContext implements Context { 58 59 62 private static POA rootPOA = null; 63 64 69 public IIOPContext(Context iiopCtx) throws NamingException { 70 super(iiopCtx); 71 72 ORB orb = IIOPCosNaming.getOrb(); 74 if (rootPOA == null) { 75 try { 76 rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 77 } catch (Exception e) { 78 throw NamingExceptionHelper.create("Cannot get a single instance" + e.getMessage(), e); 79 } 80 } 81 } 82 83 84 92 protected Object unwrapObject(Object o, Name name) throws NamingException { 93 try { 94 if (o instanceof RemoteReference) { 96 Reference objRef = ((RemoteReference) o).getReference(); 98 ObjectFactory objFact = (ObjectFactory ) (Class.forName(objRef.getFactoryClassName())).newInstance(); 99 return objFact.getObjectInstance(objRef, name, this, getEnvironment()); 100 } else if (o instanceof JNDIRemoteResource) { 101 return ((JNDIRemoteResource) o).getResource(); 102 } else { 103 return o; 104 } 105 } catch (Exception e) { 106 throw NamingExceptionHelper.create("Cannot unwrap object '" + o + "' with name '" + name + "' :" + e.getMessage(), e); 107 } 108 } 109 110 121 protected Object wrapObject(Object o, Name name, boolean replace) throws NamingException { 122 try { 123 Remote wrappedObject = null; 124 125 if ((!(o instanceof Remote )) && (o instanceof Referenceable )) { 127 wrappedObject = new JNDIReferenceWrapper(((Referenceable ) o).getReference()); 128 } else if ((!(o instanceof Remote )) && (o instanceof Reference )) { 129 wrappedObject = new JNDIReferenceWrapper((Reference ) o); 130 } else if ((!(o instanceof Remote )) && (o instanceof Serializable )) { 131 wrappedObject = new JNDIResourceWrapper((Serializable ) o); 132 } else { 133 return o; 135 } 136 137 PortableRemoteObjectDelegate proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject(); 139 proDelegate.exportObject(wrappedObject); 140 Remote oldObj = (Remote ) addToExported(name, wrappedObject); 141 if (oldObj != null) { 142 if (replace) { 143 proDelegate.unexportObject(oldObj); 144 } else { 145 proDelegate.unexportObject(wrappedObject); 146 addToExported(name, oldObj); 147 throw new NamingException ("Object '" + o + "' with name '" + name + "' is already bind"); 148 } 149 } 150 return wrappedObject; 151 } catch (Exception e) { 152 throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : " + e.getMessage(), e); 153 } 154 } 155 156 } | Popular Tags |