FileDocCategorySizeDatePackage
TreeHead.javaAPI DocJBoss 4.2.112159Fri Jul 13 20:52:36 BST 2007org.jboss.ha.jndi

TreeHead

public class TreeHead extends org.jnp.server.NamingServer implements Serializable, org.jnp.interfaces.Naming
This class extends the JNP JNDI implementation. binds and unbinds will be distributed to all members of the cluster that are running HAJNDI. lookups will look for Names in HAJNDI then delegate to the local InitialContext This class is fully serializable for GET_STATE
author
Sacha Labourey
author
Scott.Stark@jboss.org
version
$Revision: 57188 $

Fields Summary
private static final long
serialVersionUID
private static Logger
log
private transient org.jboss.ha.framework.interfaces.HAPartition
partition
private transient HAJNDI
father
Constructors Summary
public TreeHead()


   // Constructor --------------------------------------------------------

     
       
   
      super();
   
Methods Summary
public void_bind(javax.naming.Name name, java.lang.Object obj, java.lang.String className)

      if( log.isTraceEnabled() )
         log.trace("_bind, name="+name);
      super.bind(name, obj, className);
   
public javax.naming.Context_createSubcontext(javax.naming.Name name)

      if( log.isTraceEnabled() )
         log.trace("_createSubcontext, name="+name);
      return super.createSubcontext(name);
   
public java.lang.Object_lookupLocally(javax.naming.Name name)

      boolean trace = log.isTraceEnabled();
      if( trace )
         log.trace("_lookupLocally, name="+name);
      try
      {
         return lookupLocally(name);
      }
      catch (Exception e)
      {
         if( trace )
            log.trace("_lookupLocally failed", e);
         return e;
      }
   
public void_rebind(javax.naming.Name name, java.lang.Object obj, java.lang.String className)

      if( log.isTraceEnabled() )
         log.trace("_rebind, name="+name);
      super.rebind(name, obj, className);
   
public void_unbind(javax.naming.Name name)

      if( log.isTraceEnabled() )
         log.debug("_unbind, name="+name);
      super.unbind(name);
   
public voidbind(javax.naming.Name name, java.lang.Object obj, java.lang.String className)

      if( log.isTraceEnabled() )
         log.trace("bind, name="+name);
      super.bind(name, obj, className);
      // if we get here, this means we can do it on every node.
      Object[] args = new Object[3];
      args[0] = name;
      args[1] = obj;
      args[2] = className;
      try
      {
         partition.callMethodOnCluster("HAJNDI",
                                       "_bind",
                                       args,
                                       new Class[]{Name.class, Object.class, String.class}, true);
      }
      catch (Exception e)
      {
         CommunicationException ce = new CommunicationException("Failed to _bind on cluster");
         ce.setRootCause(e);
         throw ce;
      }
   
public javax.naming.ContextcreateSubcontext(javax.naming.Name name)

      if( log.isTraceEnabled() )
         log.trace("createSubcontext, name="+name);
      javax.naming.Context result = super.createSubcontext(name);

      // if we get here, this means we can do it on every node.
      Object[] args = new Object[1];
      args[0] = name;
      try
      {
         partition.callMethodOnCluster("HAJNDI", "_createSubcontext", args, new Class[]{Name.class}, true);
      }
      catch (Exception e)
      {
         CommunicationException ce = new CommunicationException("Failed to _createSubcontext on cluster");
         ce.setRootCause(e);
         throw ce;
      }

      return result;
   
public voiddestroy()

      log.debug("unregisterRPCHandler");
      partition.unregisterRPCHandler("HAJNDI", this);
   
protected java.util.ArrayListenum2list(javax.naming.NamingEnumeration en)

      ArrayList rtn = new ArrayList();
      try
      {
         while (en.hasMore())
         {
            rtn.add(en.next());
         }
         en.close();
      }
      catch (NamingException ignored) {}
      return rtn;
   
public org.jnp.interfaces.NaminggetRoot()

      return father.getHAStub();
   
public voidinit()

      log.debug("registerRPCHandler");
      partition.registerRPCHandler("HAJNDI", this);
   
public java.util.Collectionlist(javax.naming.Name name)

      if( log.isTraceEnabled() )
         log.trace("list, name="+name);
      Collection result = null;
      try
      {
         result = super.list(name);
      }
      catch (NameNotFoundException ex)
      {
         // not found in global jndi, look in local.
         result =  enum2list(new InitialContext().list(name));
      }
      return result;
   
public java.util.CollectionlistBindings(javax.naming.Name name)

      if( log.isTraceEnabled() )
         log.trace("listBindings, name="+name);
      Collection result = null;
      try
      {
         result = super.listBindings(name);
      }
      catch (NameNotFoundException ex)
      {
         // not found in global jndi, look in local.
         result =  enum2list(new InitialContext().listBindings(name));
      }
      return result;
   
public java.lang.Objectlookup(javax.naming.Name name)

      boolean trace = log.isTraceEnabled();
      if( trace )
         log.trace("lookup, name="+name);
      Object result = null;
      try
      {
         result = super.lookup(name);
      }
      catch (NameNotFoundException ex)
      {
         try
         {
            // not found in global jndi, look in local.
            result = lookupLocally(name);
         }
         catch (NameNotFoundException nnfe)
         {
            // if we get here, this means we can do it on every node.
            Object[] args = new Object[1];
            args[0] = name;
            ArrayList rsp = null;
            Exception cause = null;
            try
            {
               if( trace )
                  log.trace("calling lookupLocally("+name+") on cluster");
               rsp = partition.callMethodOnCluster("HAJNDI", "lookupLocally",
                  args, new Class[]{Name.class}, true);
            }
            catch (Exception ignored)
            {
               if( trace )
                  log.trace("Clusterd lookupLocally("+name+") failed", ignored);
               cause = ignored;
            }

            if( trace )
               log.trace("Returned results size: "+ (rsp != null ? rsp.size() : 0));
            if (rsp == null || rsp.size() == 0)
            {
               NameNotFoundException nnfe2 = new NameNotFoundException(name.toString());
               nnfe2.setRootCause(cause);
               throw nnfe2;
            }

            for (int i = 0; i < rsp.size(); i++)
            {
               result = rsp.get(i);
               if( trace )
               {
                  String type = (result != null ? result.getClass().getName() : "null");
                  log.trace("_lookupLocally, i="+i+", value="+result+", type="+type);
               }
               // Ignore null and Exception return values
               if ( result != null && !(result instanceof Exception) )
                  return result;
            }
            throw nnfe;
         }
      }
      return result;
   
public java.lang.ObjectlookupLocally(javax.naming.Name name)

      boolean trace = log.isTraceEnabled();
      if( trace )
         log.trace("lookupLocally, name="+name);

      // TODO: This is a really big hack here
      // We cannot do InitialContext().lookup(name) because
      // we get ClassNotFound errors and ClassLinkage errors.
      // So, what we do is cheat and get the static localServer variable
      try
      {
         if (NamingContext.localServer != null)
         {
            return NamingContext.localServer.lookup(name);
         }
         else
         {
            InitialContext ctx = new InitialContext();
            return ctx.lookup(name);
         }
      }
      catch (NamingException e)
      {
         if( trace )
            log.trace("lookupLocally failed, name=" + name, e);
         throw e;
      }
      catch (java.rmi.RemoteException e)
      {
         NamingException ne = new NamingException("unknown remote exception");
         ne.setRootCause(e);
         if( trace )
            log.trace("lookupLocally failed, name=" + name, e);
         throw ne;
      }
      catch (RuntimeException e)
      {
         if( trace )
            log.trace("lookupLocally failed, name=" + name, e);
         throw e;
      }
   
public voidrebind(javax.naming.Name name, java.lang.Object obj, java.lang.String className)

      if( log.isTraceEnabled() )
         log.trace("rebind, name="+name);
      super.rebind(name, obj, className);

      // if we get here, this means we can do it on every node.
      Object[] args = new Object[3];
      args[0] = name;
      args[1] = obj;
      args[2] = className;
      try
      {
         partition.callMethodOnCluster("HAJNDI",
                                       "_rebind",
                                       args,
                                       new Class[]{Name.class, Object.class, String.class},
                                       true);
      }
      catch (Exception e)
      {
         CommunicationException ce = new CommunicationException("Failed to _rebind on cluster");
         ce.setRootCause(e);
         throw ce;
      }
   
public voidsetHARMIHead(HAJNDI father)

      this.father = father;
   
public voidsetPartition(org.jboss.ha.framework.interfaces.HAPartition partition)

      this.partition = partition;
   
public voidstop()

      
   
public voidunbind(javax.naming.Name name)

      if( log.isTraceEnabled() )
         log.trace("unbind, name="+name);
      super.unbind(name);

      // if we get here, this means we can do it on every node.
      Object[] args = new Object[1];
      args[0] = name;
      try
      {
         partition.callMethodOnCluster("HAJNDI", "_unbind", args, new Class[]{Name.class}, true);
      }
      catch (Exception e)
      {
         CommunicationException ce = new CommunicationException("Failed to _unbind on cluster");
         ce.setRootCause(e);
         throw ce;
      }