1 25 26 package org.objectweb.carol.jndi.spi; 27 28 import java.io.Serializable ; 29 import java.rmi.Remote ; 30 31 import javax.naming.CompositeName ; 32 import javax.naming.Context ; 33 import javax.naming.Name ; 34 import javax.naming.NamingException ; 35 import javax.naming.Reference ; 36 import javax.naming.Referenceable ; 37 import javax.naming.spi.ObjectFactory ; 38 import javax.rmi.PortableRemoteObject ; 39 import javax.rmi.CORBA.PortableRemoteObjectDelegate ; 40 import javax.rmi.CORBA.Util ; 41 42 import org.omg.CORBA.ORB ; 43 import org.omg.CORBA.portable.ObjectImpl ; 44 import org.omg.PortableServer.IdAssignmentPolicyValue ; 45 import org.omg.PortableServer.LifespanPolicyValue ; 46 import org.omg.PortableServer.POA ; 47 48 import org.objectweb.carol.jndi.ns.JacORBCosNaming; 49 import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper; 50 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource; 51 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper; 52 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 53 import org.objectweb.carol.util.configuration.ConfigurationRepository; 54 import org.objectweb.carol.util.csiv2.SasComponent; 55 import org.objectweb.carol.util.csiv2.SasPolicy; 56 57 import com.sun.jndi.rmi.registry.RemoteReference; 58 59 62 public class JacORBIIOPContext extends AbsContext implements Context { 63 64 67 public static final String SAS_COMPONENT = "org.objectweb.carol.util.csiv2.SasComponent"; 68 69 72 private static final int POA_POLICIES_NUMBER = 3; 73 74 77 private static POA rootPOA = null; 78 79 82 private SasComponent sasComponent = null; 83 84 89 public JacORBIIOPContext(Context iiopCtx) throws NamingException { 90 super(iiopCtx); 91 this.sasComponent = (SasComponent) iiopCtx.getEnvironment().get(SAS_COMPONENT); 92 93 ORB orb = JacORBCosNaming.getOrb(); 94 if (rootPOA == null) { 95 try { 96 rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 97 } catch (Exception e) { 98 throw NamingExceptionHelper.create("Cannot get a single instance of rootPOA : " + e.getMessage(), e); 99 } 100 } 101 102 } 103 104 107 public static POA getRootPOA() { 108 return rootPOA; 109 } 110 111 119 protected Object unwrapObject(Object o, Name name) throws NamingException { 120 try { 121 ObjectImpl objImpl = (ObjectImpl ) PortableRemoteObject.narrow(o, ObjectImpl .class); 123 String [] ids = objImpl._ids(); 124 String itf = ids[0]; 126 127 if (itf.indexOf(":com.sun.jndi.rmi.registry.RemoteReference:") != -1) { 128 Reference objRef = ((RemoteReference) PortableRemoteObject.narrow(o, RemoteReference.class)) 130 .getReference(); 131 ObjectFactory objFact = (ObjectFactory ) (Class.forName(objRef.getFactoryClassName())).newInstance(); 132 return objFact.getObjectInstance(objRef, name, this, getEnvironment()); 133 } else if (itf.indexOf("RMI:org.objectweb.carol.jndi.wrapping.JNDIRemoteResource:") != -1) { 134 JNDIRemoteResource jndiRemoteResource = (JNDIRemoteResource) PortableRemoteObject.narrow(o, 136 JNDIRemoteResource.class); 137 return jndiRemoteResource.getResource(); 138 } else { 139 return o; 140 } 141 } catch (Exception e) { 142 throw NamingExceptionHelper.create("Cannot unwrap object '" + o + "' with name '" + name + "' :" + e.getMessage(), e); 143 } 144 } 145 146 157 protected Object wrapObject(Object o, Name name, boolean replace) throws NamingException { 158 try { 159 Remote wrappedObject = null; 160 161 if ((!(o instanceof Remote )) && (o instanceof Referenceable )) { 163 wrappedObject = new JNDIReferenceWrapper(((Referenceable ) o).getReference()); 164 } else if ((!(o instanceof Remote )) && (o instanceof Reference )) { 165 wrappedObject = new JNDIReferenceWrapper((Reference ) o); 166 } else if ((!(o instanceof Remote )) && (o instanceof Serializable )) { 167 wrappedObject = new JNDIResourceWrapper((Serializable ) o); 168 } else { 169 return o; 171 } 172 173 PortableRemoteObjectDelegate proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject(); 175 proDelegate.exportObject(wrappedObject); 176 177 Remote oldObj = (Remote ) addToExported(name, wrappedObject); 178 if (oldObj != null) { 179 if (replace) { 180 proDelegate.unexportObject(oldObj); 181 } else { 182 proDelegate.unexportObject(wrappedObject); 183 addToExported(name, oldObj); 184 throw new NamingException ("Object '" + o + "' with name '" + name + "' is already bind"); 185 } 186 } 187 return wrappedObject; 188 } catch (Exception e) { 189 throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : " + e.getMessage(), e); 190 } 191 } 192 193 194 200 public Object lookup(String name) throws NamingException { 201 try { 202 return lookup(new CompositeName (name)); 203 } catch (RuntimeException re) { 204 throw NamingExceptionHelper.create("Cannot lookup object with name '" + name + " : " + re.getMessage() , re); 205 } catch (NamingException e) { 206 String msg = e.getMessage(); 210 if (msg == null) { 211 msg = e.toString(); 212 } 213 throw NamingExceptionHelper.create("Cannot lookup object with name '" + name + " : " + msg , e); 214 } 215 } 216 217 223 public void bind(Name name, Object obj) throws NamingException { 224 try { 225 if (sasComponent != null) { 226 Remote r = (Remote ) wrapObject(obj, name, false); 227 bindWithSpecificPoa(name, r); 228 } else { 229 super.bind(name, obj); 230 } 231 } catch (Exception e) { 232 throw NamingExceptionHelper.create("Cannot bind object '" + obj + "' with name '" + name + "' :" + e.getMessage(), e); 233 } 234 235 } 236 237 238 246 public void rebind(Name name, Object obj) throws NamingException { 247 try { 248 if (sasComponent != null) { 249 Remote r = (Remote ) wrapObject(obj, name, true); 250 rebindWithSpecificPoa(name, r); 251 } else { 252 super.rebind(name, obj); 253 } 254 } catch (Exception e) { 255 throw NamingExceptionHelper.create("Cannot rebind object '" + obj + "' with name '" + name + "' :" + e.getMessage(), e); 256 } 257 } 258 259 265 private void rebindWithSpecificPoa(Name name, Remote r) throws Exception { 266 POA securedPOA = createSecurePOA(name.toString()); 267 org.omg.PortableServer.Servant servant = (org.omg.PortableServer.Servant ) Util.getTie(r); 268 securedPOA.activate_object_with_id(name.toString().getBytes(), servant); 269 getWrappedContext().rebind(name, securedPOA.servant_to_reference(servant)); 270 } 271 272 278 private void bindWithSpecificPoa(Name name, Remote r) throws Exception { 279 POA securedPOA = createSecurePOA(name.toString()); 280 org.omg.PortableServer.Servant servant = (org.omg.PortableServer.Servant ) Util.getTie(r); 281 securedPOA.activate_object_with_id(name.toString().getBytes(), servant); 282 getWrappedContext().bind(name, securedPOA.servant_to_reference(servant)); 283 } 284 285 291 private POA createSecurePOA(String nameId) throws Exception { 292 293 297 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [POA_POLICIES_NUMBER]; 299 policies[0] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); 300 policies[1] = rootPOA.create_lifespan_policy(LifespanPolicyValue.TRANSIENT); 301 policies[2] = new SasPolicy(sasComponent); 302 return rootPOA.create_POA(nameId + Math.random(), rootPOA.the_POAManager(), policies); 303 } 304 305 } | Popular Tags |