FileDocCategorySizeDatePackage
IORFactory.javaAPI DocJBoss 4.2.115829Fri Jul 13 20:53:50 BST 2007org.jboss.ejb3.iiop

IORFactory

public class IORFactory extends Object implements org.jboss.ejb3.remoting.RemoteProxyFactory
Comment
author
Carlo de Wolf
version
$Revision: 61035 $

Fields Summary
private static final Logger
log
private org.jboss.ejb3.Container
container
private org.jboss.aop.Advisor
advisor
private org.jboss.annotation.ejb.RemoteBinding
binding
private String
webServiceName
private String[]
beanRepositoryIds
private String[]
homeRepositoryIds
private org.omg.CORBA.ORB
orb
private org.omg.PortableServer.POA
irPoa
private org.jboss.iiop.rmi.ir.InterfaceRepository
iri
private org.jboss.invocation.iiop.ServantRegistry
servantRegistry
private org.jboss.invocation.iiop.ServantRegistry
homeServantRegistry
private org.jboss.web.WebClassLoader
wcl
private org.jboss.invocation.iiop.ReferenceFactory
referenceFactory
private org.jboss.invocation.iiop.ReferenceFactory
homeReferenceFactory
private static final org.jboss.annotation.ejb.IIOP
defaultIIOP
Constructors Summary
Methods Summary
public java.lang.ObjectcreateHomeProxy()

   
   // TODO: do I really need this method
     
   
      try
      {
         org.omg.CORBA.Object corbaRef = homeReferenceFactory.createReference(homeRepositoryIds[0]);
         
         EJBHome corbaObj = (EJBHome) PortableRemoteObject.narrow(corbaRef, EJBHome.class);
         
         return corbaObj;
      }
      catch(Exception e)
      {
         throw new RuntimeException(e);
      }
   
public java.lang.ObjectcreateProxy()

      try
      {
         org.omg.CORBA.Object corbaRef = referenceFactory.createReference(beanRepositoryIds[0]);
         
         EJBObject corbaObj = (EJBObject) PortableRemoteObject.narrow(corbaRef, EJBObject.class);
         
         return corbaObj;
      }
      catch(Exception e)
      {
         throw new RuntimeException(e);
      }
   
public java.lang.ObjectcreateProxy(java.lang.Object id)

      try
      {
         org.omg.CORBA.Object corbaRef = referenceFactory.createReferenceWithId(id, beanRepositoryIds[0]);
         
         EJBObject corbaObj = (EJBObject) PortableRemoteObject.narrow(corbaRef, EJBObject.class);
         
         return corbaObj;
      }
      catch(Exception e)
      {
         throw new RuntimeException(e);
      }
   
private org.jboss.annotation.ejb.IIOPgetIIOP()

      IIOP iiop = (IIOP) advisor.resolveAnnotation(IIOP.class);
      if(iiop != null)
         return iiop;
      
      return defaultIIOP;
   
private java.lang.StringgetJndiName()

      return ProxyFactoryHelper.getDefaultRemoteJndiName(container);
   
private org.omg.CosNaming.NamingContextExtgetNamingContextExt()

      Context initialContext = new InitialContext();
      
      // NOTE: eclipse editor parser crashes silently on this line (because of org.jboss.iiop.CorbaNamingService) with unknown reason
      // that's why this method is at the end
      return NamingContextExtHelper.narrow((org.omg.CORBA.Object) initialContext.lookup("java:/" + org.jboss.iiop.CorbaNamingService.NAMING_NAME));
   
private java.lang.StringgetServantName()

      // TODO: is "Servant/" a good prefix for servant name
      return "Servant/" + getJndiName();
   
private org.jboss.web.WebServiceMBeangetWebServer()

      if(webServiceName == null)
         throw new IllegalStateException("iiop is not going to work without a web service");
      
      return (WebServiceMBean) MBeanProxyExt.create(WebServiceMBean.class, webServiceName);
   
private voidrebind(org.omg.CosNaming.NamingContextExt ctx, java.lang.String strName, org.omg.CORBA.Object obj)
Literal copy from org.jboss.proxy.ejb.IORFactory

      NameComponent[] name = ctx.to_name(strName);
      NamingContext intermediateCtx = ctx;

      for (int i = 0; i < name.length - 1; i++ ) {
         NameComponent[] relativeName = new NameComponent[] { name[i] };
         try {
            intermediateCtx = NamingContextHelper.narrow(
                  intermediateCtx.resolve(relativeName));
         }
         catch (NotFound e) {
            intermediateCtx = intermediateCtx.bind_new_context(relativeName);
         }
      }
      intermediateCtx.rebind(new NameComponent[] { name[name.length - 1] }, obj);
   
private voidremoveWebClassLoader()

      getWebServer().removeClassLoader(wcl);
   
public voidsetContainer(org.jboss.ejb3.Container container)

      this.container = container;
      this.advisor = (Advisor) container; // TODO: why the cast?
   
public voidsetRemoteBinding(org.jboss.annotation.ejb.RemoteBinding binding)

      this.binding = binding;
   
public voidsetWebServiceName(java.lang.String name)

      this.webServiceName = name;
   
public voidstart()

      // TODO: IORFactory only supports 1 remote interface
      Class remoteInterfaces[] = ProxyFactoryHelper.getRemoteInterfaces(container);
      if(remoteInterfaces.length > 1)
         log.warn("IIOP binding only works on 1 interface, using: " + remoteInterfaces[0].getName());
      InterfaceAnalysis interfaceAnalysis = InterfaceAnalysis.getInterfaceAnalysis(remoteInterfaces[0]);
      this.beanRepositoryIds = interfaceAnalysis.getAllTypeIds();
      
      InterfaceAnalysis homeInterfaceAnalysis = null;
      Class homeInterface = ProxyFactoryHelper.getRemoteHomeInterface(container);
      if(homeInterface != null)
      {
         if(!EJBHome.class.isAssignableFrom(homeInterface))
            throw new IllegalArgumentException("home interface " + homeInterface.getName() + " must extend javax.ejb.EJBHome (EJB3 4.6.8)");
         homeInterfaceAnalysis = InterfaceAnalysis.getInterfaceAnalysis(homeInterface);
         this.homeRepositoryIds = homeInterfaceAnalysis.getAllTypeIds();
      }
      // To allow EJB3 Stateless beans to operate we can function without a home interface.
      
      // Get orb and irPoa references
      try {
         orb = (ORB)new InitialContext().lookup("java:/" + CorbaORBService.ORB_NAME);
      }
      catch (NamingException e) {
         throw new Exception("Cannot lookup java:/" + CorbaORBService.ORB_NAME + ": " + e);
      }
      try {
         irPoa = (POA)new InitialContext().lookup("java:/" + CorbaORBService.IR_POA_NAME);
      }
      catch (NamingException e) {
         throw new Exception("Cannot lookup java:/" + CorbaORBService.IR_POA_NAME + ": " + e);
      }
      
      IIOP iiop = getIIOP();
      if(iiop.interfaceRepositorySupported())
      {
         this.iri = new InterfaceRepository(orb, irPoa, getJndiName());
         iri.mapClass(remoteInterfaces[0]);
         if(homeInterface != null)
            iri.mapClass(homeInterface);
         iri.finishBuild();
      }
      
      // TODO: obtain the iiop invoker name properly
      ObjectName invokerName = new ObjectName("jboss:service=invoker,type=iiop");
      ServantRegistries servantRegistries = (ServantRegistries) Registry.lookup(invokerName);
      if(servantRegistries == null)
         throw new Exception("can't find iiop invoker");
      ServantRegistryKind registryWithTransientPOA;
      ServantRegistryKind registryWithPersistentPOA; 
      if(iiop.poa().equals(IIOP.POA_PER_SERVANT))
      {
         registryWithTransientPOA = ServantRegistryKind.TRANSIENT_POA_PER_SERVANT;
         registryWithPersistentPOA = ServantRegistryKind.PERSISTENT_POA_PER_SERVANT;
      }
      else if(iiop.poa().equals(IIOP.POA_SHARED))
      {
         registryWithTransientPOA = ServantRegistryKind.SHARED_TRANSIENT_POA;
         registryWithPersistentPOA = ServantRegistryKind.SHARED_PERSISTENT_POA;
      }
      else
         throw new IllegalArgumentException("@IIOP.poa can only be 'per-servant' or 'shared'");
      // Only works for session container
      this.servantRegistry = servantRegistries.getServantRegistry(registryWithTransientPOA);
      this.homeServantRegistry = servantRegistries.getServantRegistry(registryWithPersistentPOA); // TODO: why is home interface in persistent poa?
      
      // Hack in a WebCL (from org.jboss.ejb.EjbModule.initializeContainer)
      // TODO:  seting up a WebClassLoader needs to be done somewhere where
      ObjectName on = container.getObjectName();
      this.wcl = new EJB3IIOPWebClassLoader(on, (RepositoryClassLoader) ((SessionContainer) container).getClassloader(), getJndiName());
      WebServiceMBean webServer = getWebServer();
      URL[] codebaseURLs = {webServer.addClassLoader(wcl)};
      wcl.setWebURLs(codebaseURLs);
      
      // setup a codebase policy, the CodebaseInterceptor will translate this to a TAG_JAVA_CODEBASE
      String codebaseString = wcl.getCodebaseString();
      log.debug("codebase = " + codebaseString);
      Any codebase = orb.create_any();
      codebase.insert_string(codebaseString);
      Policy codebasePolicy;
      codebasePolicy = orb.create_policy(CodebasePolicy.TYPE, codebase);
      
      // Create csiv2Policy for both home and remote containing
      // IorSecurityConfigMetadata
      Any secPolicy = orb.create_any();
//      IorSecurityConfigMetaData iorSecurityConfigMetaData =
//         container.getBeanMetaData().getIorSecurityConfigMetaData();
      IorSecurityConfigMetaData iorSecurityConfigMetaData = new IorSecurityConfigMetaData(); // TODO: make ior security configurable
      secPolicy.insert_Value(iorSecurityConfigMetaData);
      Policy csiv2Policy = orb.create_policy(CSIv2Policy.TYPE, secPolicy);
      
      Policy policies[] = { codebasePolicy, csiv2Policy };
      
      InterfaceDef interfaceDef = null;
      if(iri != null)
      {
         Repository ir = iri.getReference();
         interfaceDef = InterfaceDefHelper.narrow(ir.lookup_id(beanRepositoryIds[0]));
      }
      
      Current poaCurrent = CurrentHelper.narrow(orb.resolve_initial_references("POACurrent"));

      NamingContextExt ctx = getNamingContextExt();

      log.debug("binding servant name " + getServantName());
      
      Servant servant = new BeanCorbaServant(this, poaCurrent, container, interfaceDef, interfaceAnalysis);
      this.referenceFactory = servantRegistry.bind(getServantName(), servant, policies);
      
      EJBObject corbaObj = (EJBObject) createProxy();
      
      rebind(ctx, getJndiName(), (org.omg.CORBA.Object) corbaObj);
      
      // TODO: use iri
      if(homeInterfaceAnalysis != null)
      {
         servant = new BeanCorbaServant(this, poaCurrent, container, null, homeInterfaceAnalysis);
         this.homeReferenceFactory = homeServantRegistry.bind(getServantName() + "Home", servant, policies);
         
         Object homeObject = createHomeProxy();
         
         rebind(ctx, ProxyFactoryHelper.getHomeJndiName(container), (org.omg.CORBA.Object) homeObject);
      }
      
      // bind HandleDelegate stuff
      Context compCtx = (Context) new InitialContext().lookup("java:comp");
      NonSerializableFactory.rebind(compCtx, "ORB", orb);
      NonSerializableFactory.rebind(compCtx, "HandleDelegate", new HandleDelegateImpl());
   
public voidstop()

      if(homeReferenceFactory != null)
      {
         unbind(ProxyFactoryHelper.getHomeJndiName(container));
         unbindHomeServant();
      }
      unbind(getJndiName());
      
      unbindServant();
      
      removeWebClassLoader();
   
private voidunbind(java.lang.String strName)
Unbind the bean from CosNaming

      try
      {
         NamingContextExt corbaContext = getNamingContextExt();
         NameComponent n[] = corbaContext.to_name(strName);
         getNamingContextExt().unbind(n);
      }
      catch(Exception e)
      {
         log.warn("unable to unbind '" + strName + "'", e);
      }
   
private voidunbindHomeServant()

      try
      {
         homeServantRegistry.unbind(getServantName() + "Home");
      }
      catch(Exception e)
      {
         log.warn("unable to unbind home servant", e);
      }
   
private voidunbindServant()

      try
      {
         servantRegistry.unbind(getServantName());
      }
      catch(Exception e)
      {
         log.warn("unable to unbind servant", e);
      }