User: slaboure
  Date: 01/08/19 11:31:02

  Added:       src/main/org/jboss/ejb/plugins/jrmp13/server
                        JRMPContainerInvokerHA.java
  Log:
  First import of sources for HA support for SLSB
  
  Revision  Changes    Path
  1.1                  
jbossmx/src/main/org/jboss/ejb/plugins/jrmp13/server/JRMPContainerInvokerHA.java
  
  Index: JRMPContainerInvokerHA.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE WebOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.ejb.plugins.jrmp13.server;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Proxy;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Vector;
  
  import javax.ejb.EJBMetaData;
  import javax.ejb.EJBHome;
  import javax.ejb.EJBObject;
  
  import org.jboss.ejb.Container;
  import org.jboss.ejb.ContainerInvoker;
  import org.jboss.ejb.ContainerInvokerContainer;
  import org.jboss.metadata.EntityMetaData;
  import org.jboss.metadata.SessionMetaData;
  
  import org.jboss.ejb.plugins.jrmp.interfaces.ContainerRemote;
  import org.jboss.ejb.plugins.jrmp.interfaces.HASLSBTargetsManager;
  
  import org.jboss.ejb.plugins.jrmp13.interfaces.HomeProxy;
  import org.jboss.ejb.plugins.jrmp13.interfaces.HomeProxyHA;
  import org.jboss.ejb.plugins.jrmp13.interfaces.StatelessSessionProxy;
  import org.jboss.ejb.plugins.jrmp13.interfaces.StatelessSessionProxyHA;
  import org.jboss.ejb.plugins.jrmp13.interfaces.StatefulSessionProxy;
  import org.jboss.ejb.plugins.jrmp13.interfaces.EntityProxy;
  import org.jboss.logging.Logger;
  
  import org.jboss.ha.HAConfigNode;
  import org.jboss.ha.HAConfigEntry;
  
  import javax.naming.InitialContext;
  import javax.naming.Context;
  
  import org.jboss.ha.HAConfigNode.HAEventsCallbackable;
  
  import java.util.Hashtable;
  
  /**
   *   Based on JRMPContainerInvoker v.1.10 for HA use in SLSB
   *
   *   @see StatelessSessionProxy
   *   @author Rickard �berg ([EMAIL PROTECTED])
   *   @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
   *   @author <a href="mailto:[EMAIL PROTECTED]";>Sacha Labourey</a>
   *   @version $Revision: 1.1 $
   *
   *   <p><b>Revisions:</b>
   *
   *   <p><b>20010819 Sacha Labourey:</b>
   *   <ul>
   *   <li> First import of sources
   *   </ul>
   */
  
  public final class JRMPContainerInvokerHA
  implements ContainerInvoker, HAConfigNode.HAEventsCallbackable
  {
     EJBHome home;
     EJBObject statelessObject;
     
     Container container;
     org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker ci; // Parent invoker
     
     String _beanName = null;
     String _appName = null;
     String _nodeName = null;
     StatelessSessionProxyHA _ssp = null; // optimization
     HomeProxyHA _sshp = null; // optimization
     Hashtable localJndiProps = null;
     String haAppPathName = null;
     
     public JRMPContainerInvokerHA 
(org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker ci)
     {
        this.ci = ci;
     }
     
     public void setContainer (Container con)
     {
        this.container = con;
     }
     
     public void init ()
     {
        // Create stateless session object
        // Same instance is used for all objects
        if (!(container.getBeanMetaData () instanceof EntityMetaData) &&
        ((SessionMetaData)container.getBeanMetaData ()).isStateless ())
        {
           try
           {
              Context ctx = new InitialContext ();
              
              // names used for HA
              //
              _appName = new java.io.File (new java.net.URL 
(this.container.getApplication ().getName ()).getFile ()).getName ();
              _beanName = this.container.getBeanMetaData ().getEjbName ();
              
              // Get the HAConfigService
              //
              HAConfigNode ha = (HAConfigNode)ctx.lookup ("java:/HAConfigServer");
              _nodeName = ha.getNodeName ();
              localJndiProps = ha.getLocalJndiProps ();
              
              // Get already running replica
              //
              Vector replicateContainers = ha.getReplicateContainers (_appName , 
_beanName, HAConfigNode.REMOTE_TYPE);
              haAppPathName = ha.getHaAppPath (_appName , _beanName, 
HAConfigNode.REMOTE_TYPE);
              
              this.createStatelessSessionProxy (replicateContainers, ha.getNormalDelay 
(), ha.getDeathDelay ());
           }
           catch (Exception e)
           {
              e.printStackTrace ();
           }
        }
        else
        {
           // Create EJBHome object
           // We add the Handle methods to the Home
           Class handleClass;
           try
           {
              handleClass = Class.forName ("javax.ejb.Handle");
           } catch (Exception e)
           {
              Logger.exception (e);handleClass = null;
           }
           this.home = (EJBHome)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getHomeClass ().getClassLoader (),
           new Class[]
           { ((ContainerInvokerContainer)container).getHomeClass (), handleClass },
           new HomeProxy (ci.getJndiName (), ci.getEJBMetaData (), ci, ci.isOptimized 
()));
        }
        
        Logger.debug ("JRMP 1.3 CI initialized (HA)");
     }
     
     public void start ()
     {
        if (!(container.getBeanMetaData () instanceof EntityMetaData) &&
        ((SessionMetaData)container.getBeanMetaData ()).isStateless ())
        {
           try
           {
              Context ctx = new InitialContext ();
              HAConfigNode ha = (HAConfigNode)ctx.lookup ("java:/HAConfigServer");
              HAConfigEntry detail = new        HAConfigEntry 
((ContainerRemote)java.rmi.server.RemoteObject.toStub (ci), null, null);
              ha.registerContainer (this, _appName , _beanName, 
HAConfigNode.REMOTE_TYPE, detail);
           }
           catch (Exception e)
           { e.printStackTrace (); }
        }
     }
     
     public void stop ()
     {
        unregisterFromHA ();
     }
     
     public void destroy ()
     {
        unregisterFromHA ();
     }
     
     public EJBMetaData getEJBMetaData ()
     {
        // Ignore, never called
        return null;
     }
     
     public EJBHome getEJBHome ()
     {
        return home;
     }
     
     public EJBObject getStatelessSessionEJBObject ()
     {
        return statelessObject;
     }
     
     public EJBObject getStatefulSessionEJBObject (Object id)
     {
        return (EJBObject)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getRemoteClass ().getClassLoader (),
        new Class[]
        { ((ContainerInvokerContainer)container).getRemoteClass () },
        new StatefulSessionProxy (ci.getJndiName (), ci, id, ci.isOptimized ()));
     }
     
     public EJBObject getEntityEJBObject (Object id)
     {
        return (EJBObject)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getRemoteClass ().getClassLoader (),
        new Class[]
        { ((ContainerInvokerContainer)container).getRemoteClass () },
        new EntityProxy (ci.getJndiName (), ci, id, ci.isOptimized ()));
     }
     
     public Collection getEntityCollection (Collection ids)
     {
        ArrayList list = new ArrayList (ids.size ());
        Iterator idEnum = ids.iterator ();
        while(idEnum.hasNext ())
        {
           list.add (Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getRemoteClass ().getClassLoader (),
           new Class[]
           { ((ContainerInvokerContainer)container).getRemoteClass () },
           new EntityProxy (ci.getJndiName (), ci, idEnum.next (), ci.isOptimized 
())));
        }
        return list;
     }
     
     // private methods -----------------------------------------------------
     
     public void beanReplicaModified (java.util.Vector newValue)
     {
        System.out.println/*Logger.debug*/("Cluster structure has changed, need to 
recompute proxies");
        this.createStatelessSessionProxy (newValue, 5000, 30000); // these delays 
won't be used anyway
     }
     
     private void createStatelessSessionProxy (java.util.Vector replicateContainers, 
long normalDelay, long deathDelay)
     {
        try
        {
           // set all replicas, including myself in first position, in a vector
           //
           int nbElem = replicateContainers.size ();
           ContainerRemote[] cis = new ContainerRemote [nbElem + 1];
           Hashtable[] props = new Hashtable[nbElem + 1];
           cis[0] = this.ci;
           props[0] = this.localJndiProps;
           HAConfigEntry tmpEntry = null;
           boolean foundMeInList = false;
           
           for (int i=0, cursor=0; i < nbElem; i++)
           {
              tmpEntry = (HAConfigEntry)(replicateContainers.elementAt (i));
              if (tmpEntry.node != this._nodeName)
              {
                 ++cursor;
                 cis[cursor] = tmpEntry.targetContainer;
                 props[cursor] = tmpEntry.jndiServerProps;
              }
              foundMeInList = true;
           }
           
           // If I was in the list, it means that nbElem had the good size and that 
cis is now too big (nbElem + 1)
           // as well as props
           //
           if (foundMeInList)
           {
              System.arraycopy (cis, 0, cis, 0, nbElem);
              System.arraycopy (props, 0, props, 0, nbElem);
           }
           
           
           // create home proxy and set it all its possible targets (replicas)
           //
           if ((this._sshp == null) || (this.home == null)) // create it only the 
first time, after it is only updated
           {
              // Create EJBHome object
              // We add the Handle methods to the Home
              Class handleClass;
              try
              {
                 handleClass = Class.forName ("javax.ejb.Handle");
              } catch (Exception e)
              {
                 Logger.exception (e);handleClass = null;
              }
              this._sshp = new HomeProxyHA (ci.getJndiName (), ci.getEJBMetaData (), 
ci, ci.isOptimized (), new HASLSBTargetsManager (normalDelay, deathDelay));
              this._sshp.setMultipleContainers (cis, props, haAppPathName);
              this.home = (EJBHome)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getHomeClass ().getClassLoader (),
              new Class[]
              { ((ContainerInvokerContainer)container).getHomeClass (), handleClass } ,
              _sshp );
           }
           else
              this._sshp.setMultipleContainers (cis, props, haAppPathName);
           
           // create proxy and set it all its possible targets (replicas)
           //
           if ((this._ssp == null) || (this.statelessObject == null)) // create it 
only the first time, after it is only updated
           {
              this._ssp = new StatelessSessionProxyHA (ci.getJndiName (), ci, 
ci.isOptimized (), new HASLSBTargetsManager (normalDelay, deathDelay));
              this._ssp.setMultipleContainers (cis, props, haAppPathName);
              this.statelessObject = (EJBObject)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getRemoteClass ().getClassLoader (),
              new Class[]
              { ((ContainerInvokerContainer)container).getRemoteClass () } ,
              _ssp );
           }
           else
              this._ssp.setMultipleContainers (cis, props, haAppPathName);
        }
        catch (Exception e)
        {
           e.printStackTrace ();
           
           // Create EJBHome object
           // We add the Handle methods to the Home
           Class handleClass;
           try
           {
              handleClass = Class.forName ("javax.ejb.Handle");
           } catch (Exception eh)
           {
              Logger.exception (eh);handleClass = null;
           }
           this.home = (EJBHome)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getHomeClass ().getClassLoader (),
           new Class[]
           { ((ContainerInvokerContainer)container).getHomeClass (), handleClass },
           new HomeProxy (ci.getJndiName (), ci.getEJBMetaData (), ci, ci.isOptimized 
()));
           
           // problem? create a standard stateless proxy object...
           this.statelessObject = (EJBObject)Proxy.newProxyInstance 
(((ContainerInvokerContainer)container).getRemoteClass ().getClassLoader (),
           new Class[]
           { ((ContainerInvokerContainer)container).getRemoteClass () } ,
           new StatelessSessionProxy (ci.getJndiName (), ci, ci.isOptimized ()));
        }
     }
     
     private void unregisterFromHA ()
     {
        if (!(container.getBeanMetaData () instanceof EntityMetaData) &&
        ((SessionMetaData)container.getBeanMetaData ()).isStateless ())
        {
           try
           {
              // Get the HAConfigService
              //
              Context ctx = new InitialContext ();
              HAConfigNode ha = (HAConfigNode)ctx.lookup ("java:/HAConfigServer");
              
              try
              {
                 ha.unregisterContainer (_appName , _beanName, 
HAConfigNode.REMOTE_TYPE);
              }
              catch (Exception e)
              {
                 e.printStackTrace ();
              }
           }
           // do nothing, this may be due to a standard shutdown : HAConfigServer is 
already unbounded.
           catch (Exception e)
           {}
        }
     }
     // ********SL********END
     
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to