User: slaboure
  Date: 01/11/18 04:40:07

  Modified:    src/main/org/jnp/interfaces NamingContext.java
  Log:
  JNDI JNP client no more requires PROVIDER_URL setting when used with HA-JNDI on the 
server.
  If no configuration is set (or given configuration is not able to contact a server), 
a multicast message is sent over the network.
  Any server can answer to this multicast message. The client will only pick the first 
answer and download the HA stub from this location.
  
  Revision  Changes    Path
  1.15      +470 -370  jnp/src/main/org/jnp/interfaces/NamingContext.java
  
  Index: NamingContext.java
  ===================================================================
  RCS file: /cvsroot/jboss/jnp/src/main/org/jnp/interfaces/NamingContext.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- NamingContext.java        2001/09/28 20:20:10     1.14
  +++ NamingContext.java        2001/11/18 12:40:06     1.15
  @@ -12,6 +12,9 @@
   import java.io.IOException;
   import java.lang.ref.WeakReference;
   import java.net.Socket;
  +import java.net.DatagramPacket;
  +import java.net.DatagramSocket;
  +import java.net.InetAddress;
   import java.rmi.MarshalledObject;
   import java.rmi.RemoteException;
   import java.rmi.server.UnicastRemoteObject;
  @@ -28,16 +31,16 @@
   import javax.naming.spi.ResolveResult;
   
   /** This class provides the jnp provider Context implementation. It is a
  -Context interface wrapper for a RMI Naming instance that is obtained from
  -either the local server instance or by locating the server given by the
  -Context.PROVIDER_URL value.
  -
  -This class also serves as the jnp url resolution context. jnp style urls
  -passed to the 
  -
  + * Context interface wrapper for a RMI Naming instance that is obtained from
  + * either the local server instance or by locating the server given by the
  + * Context.PROVIDER_URL value.
  + *
  + * This class also serves as the jnp url resolution context. jnp style urls
  + * passed to the
  + *
    *   @author oberg
    *   @author [EMAIL PROTECTED]
  - *   @version $Revision: 1.14 $
  + *   @version $Revision: 1.15 $
    *
    * <p><b>Revisions:</b><br>
    * <p><b>2001/09/14: billb</b>
  @@ -46,111 +49,115 @@
    * </ol>
    */
   public class NamingContext
  -   implements Context, java.io.Serializable
  +implements Context, java.io.Serializable
   {
      // Constants -----------------------------------------------------
      /** @since 1.7 */
      static final long serialVersionUID = 8906455608484282128L;
  -
  +   
      // billb - FIXME - localServer is public for HAJNDI.  Need to find
      // a better workaround for this.
      public static Naming localServer;
  +   
  +   public final static String DEFAULT_DISCOVERY_GROUP_ADDRESS = "230.0.0.4";
  +   public final static int DEFAULT_DISCOVERY_GROUP_PORT = 1102;
  +   public final static int DEFAULT_DISCOVERY_TIMEOUT = 5000;
   
  -    // Attributes ----------------------------------------------------
  +   // Attributes ----------------------------------------------------
      Naming naming;
      Hashtable env;
      Name prefix;
  -
  -   NameParser parser = new NamingParser();
  -
  -    // Static --------------------------------------------------------
  -     
  -    // Cache of naming server stubs
  -    // This is a critical optimization in the case where new InitialContext
  -    // is performed often. The server stub will be shared between all those
  -    // calls, which will improve performance.
  -    // Weak references are used so if no contexts use a particular server
  -    // it will be removed from the cache.
  -   static HashMap cachedServers = new HashMap();
  -
  -   static void addServer(String name, Naming server)
  +   
  +   NameParser parser = new NamingParser ();
  +   
  +   // Static --------------------------------------------------------
  +   
  +   // Cache of naming server stubs
  +   // This is a critical optimization in the case where new InitialContext
  +   // is performed often. The server stub will be shared between all those
  +   // calls, which will improve performance.
  +   // Weak references are used so if no contexts use a particular server
  +   // it will be removed from the cache.
  +   static HashMap cachedServers = new HashMap ();
  +   
  +   static void addServer (String name, Naming server)
      {
         // Add server to map
         // Clone and synchronize to minimize delay for readers of the map
         synchronized (NamingContext.class)
         {
  -         HashMap newServers = (HashMap)cachedServers.clone();
  -         newServers.put(name, new WeakReference(server));
  +         HashMap newServers = (HashMap)cachedServers.clone ();
  +         newServers.put (name, new WeakReference (server));
            cachedServers = newServers;
         }
      }
      
  -   static Naming getServer(String host, int port)
  -      throws NamingException
  +   static Naming getServer (String host, int port)
  +   throws NamingException
      {
  -      WeakReference ref = (WeakReference)cachedServers.get(host+":"+port);
  -
  +      WeakReference ref = (WeakReference)cachedServers.get (host+":"+port);
  +      
         Naming server;
         if (ref != null)
         {
  -         server = (Naming)ref.get();
  +         server = (Naming)ref.get ();
            if (server != null)
            {
               //DEBUG             System.out.println("Using cached naming server");
               return server;
            }
         }
  -
  +      
         // Server not found; add it to cache
         try
         {
            Socket s;
  -
  +         
            try
            {
  -            s = new Socket(host,port);
  +            s = new Socket (host,port);
            } catch (IOException e2)
            {
  -            NamingException ex = new ServiceUnavailableException(e2.getMessage());
  -            ex.setRootCause(e2);
  +            NamingException ex = new ServiceUnavailableException (e2.getMessage ());
  +            ex.setRootCause (e2);
               throw ex;
            }
  -
  +         
            // Get stub from naming server
  -         ObjectInputStream in = new ObjectInputStream(new 
BufferedInputStream(s.getInputStream()));
  -         server = ((Naming)((MarshalledObject)in.readObject()).get());
  -         s.close();
  -
  +         ObjectInputStream in = new ObjectInputStream (new BufferedInputStream 
(s.getInputStream ()));
  +         server = ((Naming)((MarshalledObject)in.readObject ()).get ());
  +         s.close ();
  +         
            // Add it to cache
  -         addServer(host+":"+port, server);
  -
  +         addServer (host+":"+port, server);
  +         
            return server;
         } catch (IOException e)
         {
  -         NamingException ex = new CommunicationException(e.getMessage());
  -         ex.setRootCause(e);
  +         NamingException ex = new CommunicationException (e.getMessage ());
  +         ex.setRootCause (e);
            throw ex;
         } catch (ClassNotFoundException e)
         {
  -         NamingException ex = new CommunicationException(e.getMessage());
  -         ex.setRootCause(e);
  +         NamingException ex = new CommunicationException (e.getMessage ());
  +         ex.setRootCause (e);
            throw ex;
         }
      }
  -     
  -   static void removeServer(Hashtable env)
  +   
  +   static void removeServer (Hashtable env)
      {
         String host = "localhost";
         int port = 1099;
  -     
  +      
         // Locate naming service
  -      if (env.get(Context.PROVIDER_URL) != null)
  +      if (env.get (Context.PROVIDER_URL) != null)
         {
  -         StringTokenizer tkn = new 
StringTokenizer((String)env.get(Context.PROVIDER_URL),":");
  -         host = tkn.nextToken();
  +         StringTokenizer tkn = new StringTokenizer ((String)env.get 
(Context.PROVIDER_URL),":");
  +         host = tkn.nextToken ();
            try
            {
  -            port = Integer.parseInt(tkn.nextToken());
  +            port = Integer.parseInt (tkn.nextToken ());
            } catch (Exception ex)
            {
               // Use default;
  @@ -159,101 +166,101 @@
            // Clone and synchronize to minimize delay for readers of the map
            synchronized (NamingContext.class)
            {
  -            HashMap newServers = (HashMap)cachedServers.clone();
  -            newServers.remove(host+":"+port);
  +            HashMap newServers = (HashMap)cachedServers.clone ();
  +            newServers.remove (host+":"+port);
               cachedServers = newServers;
            }
         } else
         {
            // Don't do anything for local server
         }
  -             
  +      
      }
  -
  +   
      /** Called to remove any url scheme atoms and extract the naming
  -        service hostname:port information.
  -    @param n, the name component to the parsed. After returning n will
  -        have all scheme related atoms removed.
  -    @return the naming service hostname:port information string if name
  -        contained the host information.
  +    * service hostname:port information.
  +    * @param n, the name component to the parsed. After returning n will
  +    * have all scheme related atoms removed.
  +    * @return the naming service hostname:port information string if name
  +    * contained the host information.
       */
  -   static String parseNameForScheme(Name n) throws InvalidNameException
  +   static String parseNameForScheme (Name n) throws InvalidNameException
      {
         String serverInfo = null;
  -      if( n.size() > 0 )
  +      if( n.size () > 0 )
         {
  -         String scheme = n.get(0);
  +         String scheme = n.get (0);
            int schemeLength = 0;
  -         if( scheme.startsWith("java:") )
  +         if( scheme.startsWith ("java:") )
               schemeLength = 5;
  -         else if( scheme.startsWith("jnp:") )
  +         else if( scheme.startsWith ("jnp:") )
               schemeLength = 4;
            if( schemeLength > 0 )
            {
  -            String suffix = scheme.substring(schemeLength);
  -            if( suffix.length() == 0 )
  +            String suffix = scheme.substring (schemeLength);
  +            if( suffix.length () == 0 )
               {
                  // Scheme was "url:/..."
  -               n.remove(0);
  -               if( n.size() > 1 && n.get(0).equals("") )
  +               n.remove (0);
  +               if( n.size () > 1 && n.get (0).equals ("") )
                  {
                     // Scheme was "url://hostname:port/..."
                     // Get hostname:port value for the naming server
  -                  serverInfo = n.get(1);
  -                  n.remove(0);
  -                  n.remove(0);
  +                  serverInfo = n.get (1);
  +                  n.remove (0);
  +                  n.remove (0);
                     // If n is a empty atom remove it or else a '/' will result
  -                  if( n.size() == 1 && n.get(0).length() == 0 )
  -                     n.remove(0);
  +                  if( n.size () == 1 && n.get (0).length () == 0 )
  +                     n.remove (0);
                  }
               }
               else
               {
                  // Scheme was "url:foo" -> reinsert "foo"
  -               n.remove(0);
  -               n.add(0, suffix);
  +               n.remove (0);
  +               n.add (0, suffix);
               }
            }
         }
         return serverInfo;
      }
  -
  -   public static void setLocal(Naming server)
  +   
  +   public static void setLocal (Naming server)
      {
         localServer = server;
      }
  -
  +   
      // Constructors --------------------------------------------------
  -   public NamingContext(Hashtable e, Name baseName, Naming server)
  -      throws NamingException
  +   public NamingContext (Hashtable e, Name baseName, Naming server)
  +   throws NamingException
      {
         if (baseName == null)
  -         this.prefix = parser.parse("");
  +         this.prefix = parser.parse ("");
         else
            this.prefix = baseName;
  -
  +      
         if (e != null)
  -         this.env = (Hashtable)e.clone();
  +         this.env = (Hashtable)e.clone ();
         else
  -         this.env = new Hashtable();
  -         
  +         this.env = new Hashtable ();
  +      
         this.naming = server;
      }
  -
  +   
      // Public --------------------------------------------------------
  -
  +   
      // Context implementation ----------------------------------------
  -   public void rebind(String name, Object obj)
  -      throws NamingException
  +   public void rebind (String name, Object obj)
  +   throws NamingException
      {
  -      rebind(getNameParser(name).parse(name), obj);
  +      rebind (getNameParser (name).parse (name), obj);
      }
      
  -   public void rebind(Name name, Object obj) 
  -      throws NamingException 
  +   public void rebind (Name name, Object obj)
  +   throws NamingException
      {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         
         try
         {
  @@ -261,45 +268,45 @@
            
            // Referenceable
            if (obj instanceof Referenceable)
  -            obj = ((Referenceable)obj).getReference();
  -            
  +            obj = ((Referenceable)obj).getReference ();
  +         
            if (!(obj instanceof Reference))
            {
  -            className = obj.getClass().getName();
  +            className = obj.getClass ().getName ();
               // Normal object - serialize
  -            obj = new MarshalledObject(obj);
  +            obj = new MarshalledObject (obj);
            } else
            {
  -            className = ((Reference)obj).getClassName();
  +            className = ((Reference)obj).getClassName ();
            }
            
  -         naming.rebind(getAbsoluteName(name),obj, className);
  +         naming.rebind (getAbsoluteName (name),obj, className);
         } catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         cctx.rebind(cpe.getRemainingName(), obj);
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         cctx.rebind (cpe.getRemainingName (), obj);
         } catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
  -
  -   public void bind(String name, Object obj)
  -      throws NamingException
  +   
  +   public void bind (String name, Object obj)
  +   throws NamingException
      {
  -      bind(getNameParser(name).parse(name), obj);
  +      bind (getNameParser (name).parse (name), obj);
      }
      
  -   public void bind(Name name, Object obj) 
  -      throws NamingException 
  +   public void bind (Name name, Object obj)
  +   throws NamingException
      {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         
         try
         {
  @@ -307,68 +314,68 @@
            
            // Referenceable
            if (obj instanceof Referenceable)
  -            obj = ((Referenceable)obj).getReference();
  -            
  +            obj = ((Referenceable)obj).getReference ();
  +         
            if (!(obj instanceof Reference))
            {
  -            className = obj.getClass().getName();
  +            className = obj.getClass ().getName ();
               
               // Normal object - serialize
  -            obj = new MarshalledObject(obj);
  +            obj = new MarshalledObject (obj);
            } else
            {
  -            className = ((Reference)obj).getClassName();
  +            className = ((Reference)obj).getClassName ();
            }
  -         name = getAbsoluteName(name);
  -         naming.bind(name,obj, className);
  +         name = getAbsoluteName (name);
  +         naming.bind (name,obj, className);
         } catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         cctx.bind(cpe.getRemainingName(), obj);
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         cctx.bind (cpe.getRemainingName (), obj);
         } catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
  -
  -   public Object lookup(String name)
  -      throws NamingException 
  +   
  +   public Object lookup (String name)
  +   throws NamingException
      {
  -      return lookup(getNameParser(name).parse(name));
  +      return lookup (getNameParser (name).parse (name));
      }
      
  -   public Object lookup(Name name)
  -      throws NamingException
  +   public Object lookup (Name name)
  +   throws NamingException
      {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         
         // Empty?
  -      if (name.isEmpty())
  -         return new NamingContext(env, prefix, naming);
  -         
  +      if (name.isEmpty ())
  +         return new NamingContext (env, prefix, naming);
  +      
         try
         {
  -         Name n = getAbsoluteName(name);
  -         Object res = naming.lookup(n);
  +         Name n = getAbsoluteName (name);
  +         Object res = naming.lookup (n);
            
            if (res instanceof MarshalledObject)
            {
  -            return ((MarshalledObject)res).get();
  +            return ((MarshalledObject)res).get ();
            }
            else if (res instanceof Context)
            {
               // Add env
  -            Enumeration keys = env.keys();
  -            while (keys.hasMoreElements())
  +            Enumeration keys = env.keys ();
  +            while (keys.hasMoreElements ())
               {
  -               String key = (String)keys.nextElement();
  -               ((Context)res).addToEnvironment(key,env.get(key));
  +               String key = (String)keys.nextElement ();
  +               ((Context)res).addToEnvironment (key,env.get (key));
               }
               return res;
            }
  @@ -377,44 +384,44 @@
               // Dereference partial result
               try
               {
  -               Object resolveRes = ((ResolveResult)res).getResolvedObj();
  +               Object resolveRes = ((ResolveResult)res).getResolvedObj ();
                  if (resolveRes instanceof LinkRef)
                  {
  -                  String ref = ((LinkRef)resolveRes).getLinkName();
  +                  String ref = ((LinkRef)resolveRes).getLinkName ();
                     Context ctx;
                     try
                     {
  -                     if (ref.startsWith("./"))
  -                        ctx = (Context)lookup(ref.substring(2));
  +                     if (ref.startsWith ("./"))
  +                        ctx = (Context)lookup (ref.substring (2));
                        else
  -                        ctx = (Context)new InitialContext(env).lookup(ref);
  -                        
  -                     return ctx.lookup(((ResolveResult)res).getRemainingName());
  +                        ctx = (Context)new InitialContext (env).lookup (ref);
  +                     
  +                     return ctx.lookup (((ResolveResult)res).getRemainingName ());
                     } catch (ClassCastException e)
                     {
  -                     throw new NotContextException(ref + " is not a context");
  +                     throw new NotContextException (ref + " is not a context");
                     }
  -               } else 
  +               } else
                  {
                     try
                     {
  -                     Context ctx = 
(Context)NamingManager.getObjectInstance(resolveRes,
  -                                                                            
getAbsoluteName(name),
  -                                                                            this,
  -                                                                            env);
  -                     return ctx.lookup(((ResolveResult)res).getRemainingName());
  +                     Context ctx = (Context)NamingManager.getObjectInstance 
(resolveRes,
  +                     getAbsoluteName (name),
  +                     this,
  +                     env);
  +                     return ctx.lookup (((ResolveResult)res).getRemainingName ());
                     } catch (ClassCastException e)
                     {
  -                     throw new NotContextException();
  +                     throw new NotContextException ();
                     }
  -               } 
  +               }
               } catch (NamingException e)
               {
                  throw e;
               } catch (Exception e)
               {
  -               NamingException ex = new NamingException("Could not dereference 
object");
  -               ex.setRootCause(e);
  +               NamingException ex = new NamingException ("Could not dereference 
object");
  +               ex.setRootCause (e);
                  throw ex;
               }
            }
  @@ -423,18 +430,18 @@
               // Dereference link
               try
               {
  -               String ref = ((LinkRef)res).getLinkName();
  -               if (ref.startsWith("./"))
  -                  return lookup(ref.substring(2));
  +               String ref = ((LinkRef)res).getLinkName ();
  +               if (ref.startsWith ("./"))
  +                  return lookup (ref.substring (2));
                  else
  -                  return new InitialContext(env).lookup(ref);
  +                  return new InitialContext (env).lookup (ref);
               } catch (NamingException e)
               {
                  throw e;
               } catch (Exception e)
               {
  -               NamingException ex = new NamingException("Could not dereference 
object");
  -               ex.setRootCause(e);
  +               NamingException ex = new NamingException ("Could not dereference 
object");
  +               ex.setRootCause (e);
                  throw ex;
               }
            }
  @@ -443,17 +450,17 @@
               // Dereference object
               try
               {
  -               return NamingManager.getObjectInstance(res,
  -                                                      getAbsoluteName(name),
  -                                                      this,
  -                                                      env);
  +               return NamingManager.getObjectInstance (res,
  +               getAbsoluteName (name),
  +               this,
  +               env);
               } catch (NamingException e)
               {
                  throw e;
               } catch (Exception e)
               {
  -               NamingException ex = new NamingException("Could not dereference 
object");
  -               ex.setRootCause(e);
  +               NamingException ex = new NamingException ("Could not dereference 
object");
  +               ex.setRootCause (e);
                  throw ex;
               }
            }
  @@ -461,299 +468,384 @@
            return res;
         } catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         return cctx.lookup(cpe.getRemainingName());
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         return cctx.lookup (cpe.getRemainingName ());
         } catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         } catch (ClassNotFoundException e)
         {
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
      
  -   public void unbind(String name)
  -      throws NamingException 
  +   public void unbind (String name)
  +   throws NamingException
      {
  -      unbind(getNameParser(name).parse(name));
  +      unbind (getNameParser (name).parse (name));
      }
  -      
  -
  -   public void unbind(Name name)
  -      throws NamingException 
  +   
  +   
  +   public void unbind (Name name)
  +   throws NamingException
      {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         
         try
         {
  -         naming.unbind(getAbsoluteName(name));
  +         naming.unbind (getAbsoluteName (name));
         } catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         cctx.unbind(cpe.getRemainingName());
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         cctx.unbind (cpe.getRemainingName ());
         } catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
  -
  -   public void rename(String oldname, String newname)
  -      throws NamingException 
  +   
  +   public void rename (String oldname, String newname)
  +   throws NamingException
      {
  -      rename(getNameParser(oldname).parse(oldname), 
getNameParser(newname).parse(newname));
  +      rename (getNameParser (oldname).parse (oldname), getNameParser 
(newname).parse (newname));
      }
  -
  -   public void rename(Name oldName, Name newName)
  -      throws NamingException 
  +   
  +   public void rename (Name oldName, Name newName)
  +   throws NamingException
      {
  -      bind(newName,lookup(oldName));
  -      unbind(oldName);
  +      bind (newName,lookup (oldName));
  +      unbind (oldName);
      }
  -
  -   public NamingEnumeration list(String name)
  -      throws NamingException 
  +   
  +   public NamingEnumeration list (String name)
  +   throws NamingException
      {
  -      return list(getNameParser(name).parse(name));
  +      return list (getNameParser (name).parse (name));
      }
  -
  -   public NamingEnumeration list(Name name)
  -      throws NamingException 
  +   
  +   public NamingEnumeration list (Name name)
  +   throws NamingException
      {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         
         try
         {
  -         return new NamingEnumerationImpl(naming.list(getAbsoluteName(name)));
  +         return new NamingEnumerationImpl (naming.list (getAbsoluteName (name)));
         } catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         return cctx.list(cpe.getRemainingName());
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         return cctx.list (cpe.getRemainingName ());
         } catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
  -
  -   public NamingEnumeration listBindings(String name)
  -      throws NamingException 
  +   
  +   public NamingEnumeration listBindings (String name)
  +   throws NamingException
      {
  -      return listBindings(getNameParser(name).parse(name));
  +      return listBindings (getNameParser (name).parse (name));
      }
  -
  -   public NamingEnumeration listBindings(Name name)
  -      throws NamingException 
  +   
  +   public NamingEnumeration listBindings (Name name)
  +   throws NamingException
      {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         
         try
         {
            // Get list
  -         Collection bindings = naming.listBindings(getAbsoluteName(name));
  -         Collection realBindings = new ArrayList(bindings.size());
  +         Collection bindings = naming.listBindings (getAbsoluteName (name));
  +         Collection realBindings = new ArrayList (bindings.size ());
            
            // Convert marshalled objects
  -         Iterator enum = bindings.iterator();
  -         while (enum.hasNext())
  +         Iterator enum = bindings.iterator ();
  +         while (enum.hasNext ())
            {
  -            Binding binding = (Binding)enum.next();
  -            Object obj = binding.getObject();
  +            Binding binding = (Binding)enum.next ();
  +            Object obj = binding.getObject ();
               if (obj instanceof MarshalledObject)
               {
                  try
                  {
  -                  obj = ((MarshalledObject)obj).get();
  +                  obj = ((MarshalledObject)obj).get ();
                  } catch (ClassNotFoundException e)
                  {
  -                  NamingException ex = new CommunicationException();
  -                  ex.setRootCause(e);
  +                  NamingException ex = new CommunicationException ();
  +                  ex.setRootCause (e);
                     throw ex;
                  }
               }
  -            realBindings.add(new Binding(binding.getName(), binding.getClassName(), 
obj));
  +            realBindings.add (new Binding (binding.getName (), binding.getClassName 
(), obj));
            }
            
            // Return transformed list of bindings
  -         return new NamingEnumerationImpl(realBindings);
  +         return new NamingEnumerationImpl (realBindings);
         } catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         return cctx.listBindings(cpe.getRemainingName());
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         return cctx.listBindings (cpe.getRemainingName ());
         } catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
      
  -   public String composeName(String name, String prefix)
  -      throws NamingException 
  +   public String composeName (String name, String prefix)
  +   throws NamingException
      {
  -      Name result = composeName(parser.parse(name),
  -                                parser.parse(prefix));
  -      return result.toString();
  +      Name result = composeName (parser.parse (name),
  +      parser.parse (prefix));
  +      return result.toString ();
      }
  -
  -   public Name composeName(Name name, Name prefix)
  -      throws NamingException 
  +   
  +   public Name composeName (Name name, Name prefix)
  +   throws NamingException
      {
  -      Name result = (Name)(prefix.clone());
  -      result.addAll(name);
  +      Name result = (Name)(prefix.clone ());
  +      result.addAll (name);
         return result;
      }
  -
  -   public NameParser getNameParser(String name)
  -      throws NamingException 
  +   
  +   public NameParser getNameParser (String name)
  +   throws NamingException
      {
         return parser;
      }
  -
  -   public NameParser getNameParser(Name name)
  -      throws NamingException 
  +   
  +   public NameParser getNameParser (Name name)
  +   throws NamingException
      {
  -      return getNameParser(name.toString());
  +      return getNameParser (name.toString ());
      }
  -
  -   public Context createSubcontext(String name)
  -      throws NamingException 
  +   
  +   public Context createSubcontext (String name)
  +   throws NamingException
      {
  -      return createSubcontext(getNameParser(name).parse(name));
  +      return createSubcontext (getNameParser (name).parse (name));
      }
  -
  -   public Context createSubcontext(Name name)
  -      throws NamingException 
  +   
  +   public Context createSubcontext (Name name)
  +   throws NamingException
      {
  -      if( name.size() == 0 )
  -         throw new InvalidNameException("Cannot pass an empty name to 
createSubcontext");
  -
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  +      if( name.size () == 0 )
  +         throw new InvalidNameException ("Cannot pass an empty name to 
createSubcontext");
  +      
  +      Hashtable env = getEnv (name);
  +      checkRef (env);
         try
         {
  -         name = getAbsoluteName(name);
  -         return naming.createSubcontext(name);
  +         name = getAbsoluteName (name);
  +         return naming.createSubcontext (name);
         }
         catch (CannotProceedException cpe)
         {
  -         cpe.setEnvironment(env);
  -         Context cctx = NamingManager.getContinuationContext(cpe);
  -         return cctx.createSubcontext(cpe.getRemainingName());
  +         cpe.setEnvironment (env);
  +         Context cctx = NamingManager.getContinuationContext (cpe);
  +         return cctx.createSubcontext (cpe.getRemainingName ());
         }
         catch (IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
      }
  -
  -   public Object addToEnvironment(String propName, Object propVal)
  -      throws NamingException 
  +   
  +   public Object addToEnvironment (String propName, Object propVal)
  +   throws NamingException
      {
  -      Object old = env.get(propName);
  -      env.put(propName,propVal);
  +      Object old = env.get (propName);
  +      env.put (propName,propVal);
         return old;
      }
  -
  -   public Object removeFromEnvironment(String propName) 
  -      throws NamingException 
  +   
  +   public Object removeFromEnvironment (String propName)
  +   throws NamingException
      {
  -      return env.remove(propName);
  +      return env.remove (propName);
      }
  -
  -   public Hashtable getEnvironment()
  -      throws NamingException 
  +   
  +   public Hashtable getEnvironment ()
  +   throws NamingException
      {
         return env;
      }
  -
  -   public void close()
  -      throws NamingException 
  +   
  +   public void close ()
  +   throws NamingException
      {
         env = null;
         naming = null;
      }
  -
  -   public String getNameInNamespace()
  -      throws NamingException 
  +   
  +   public String getNameInNamespace ()
  +   throws NamingException
      {
  -      return prefix.toString();
  +      return prefix.toString ();
      }
  -
  -   public void destroySubcontext(String name)
  -      throws NamingException 
  +   
  +   public void destroySubcontext (String name)
  +   throws NamingException
      {
  -      throw new OperationNotSupportedException();
  +      throw new OperationNotSupportedException ();
      }
  -
  -   public void destroySubcontext(Name name)
  -      throws NamingException 
  +   
  +   public void destroySubcontext (Name name)
  +   throws NamingException
      {
  -      throw new OperationNotSupportedException();
  +      throw new OperationNotSupportedException ();
      }
  -
  -   public Object lookupLink(String name)
  -      throws NamingException 
  +   
  +   public Object lookupLink (String name)
  +   throws NamingException
      {
  -      return lookupLink(getNameParser(name).parse(name));
  +      return lookupLink (getNameParser (name).parse (name));
      }
  -
  +   
      /** Lookup the object referred to by name but don't dereferrence the final
  -    component. This really just involves returning the raw value returned by
  -    the Naming.lookup() method.
  -   @return the raw object bound under name.
  -   */
  -   public Object lookupLink(Name name)
  -      throws NamingException 
  +    * component. This really just involves returning the raw value returned by
  +    * the Naming.lookup() method.
  +    * @return the raw object bound under name.
  +    */
  +   public Object lookupLink (Name name)
  +   throws NamingException
      {
  -      if( name.isEmpty() )
  -         return lookup(name);
  -
  +      if( name.isEmpty () )
  +         return lookup (name);
  +      
         Object link = null;
         try
         {
  -         Name n = getAbsoluteName(name);
  -         link = naming.lookup(n);
  +         Name n = getAbsoluteName (name);
  +         link = naming.lookup (n);
         }
         catch(IOException e)
         {
            naming = null;
  -         removeServer(env);
  -         NamingException ex = new CommunicationException();
  -         ex.setRootCause(e);
  +         removeServer (env);
  +         NamingException ex = new CommunicationException ();
  +         ex.setRootCause (e);
            throw ex;
         }
         return link;
      }
  -
  +   
      // Private -------------------------------------------------------
  -   private void checkRef(Hashtable env)
  -      throws NamingException
  +   
  +   private Naming discoverServer (Hashtable env) throws NamingException
  +   {
  +      // This methods sends a broadcast message on the network and asks and HA-JNDI 
server to sent it
  +      // the HA-JNDI stub. Answer is received by TCP because we do not want to deal 
with fragmentation issues
  +      //
  +      
  +      // We first broadcast a HelloWorld datagram (multicast)
  +      // Any listening server will answer with its IP address:port in another 
datagram
  +      // we will then use this to make a standard "lookup"
  +      //
  +      Naming server;
  +      DatagramSocket s = null;
  +      try
  +      {
  +         
  +         String group = DEFAULT_DISCOVERY_GROUP_ADDRESS;
  +         int port = DEFAULT_DISCOVERY_GROUP_PORT;
  +         int timeout = DEFAULT_DISCOVERY_TIMEOUT;
  +         
  +         String discoveryTimeout = (String) env.get ("DISCOVERY_TIMEOUT");
  +         if (discoveryTimeout != null && !discoveryTimeout.equals (""))
  +            timeout = Integer.parseInt (discoveryTimeout);
  +         
  +         
  +         String discoveryGroupPort = (String) env.get ("DISCOVERY_GROUP");
  +         if (discoveryGroupPort != null && !discoveryGroupPort.equals (""))
  +         {
  +            int colon = discoveryGroupPort.indexOf (':');
  +            if( colon < 0 )
  +            {
  +               group = discoveryGroupPort;
  +            }
  +            else
  +            {
  +               group = discoveryGroupPort.substring (0, colon);
  +               try
  +               {
  +                  port = Integer.parseInt (discoveryGroupPort.substring (colon+1));
  +               }
  +               catch (Exception ex)
  +               {
  +                  // Use default;
  +               }
  +            }
  +         }
  +         
  +         InetAddress iaGroup = InetAddress.getByName (group);
  +         
  +         s = new DatagramSocket ();
  +         s.setSoTimeout (timeout);
  +         
  +         DatagramPacket packet;
  +         byte[] buf = "GET_ADDRESS".getBytes ();
  +         packet = new DatagramPacket (buf, buf.length, iaGroup, port);
  +         s.send (packet);
  +         buf = new byte[50]; // IP address + port number = 128.128.128.128:65535 => 
(12+3) + 1 + (5) = 21 
  +         packet = new DatagramPacket (buf, buf.length);
  +         s.receive (packet);
  +         String myServer = new String (packet.getData ());
  +         String serverHost;
  +         int serverPort;
  +         
  +         int colon = myServer.indexOf (':');
  +         if( colon < 0 )
  +            return null;
  +         else
  +         {
  +            serverHost = myServer.substring (0, colon);
  +            serverPort = Double.valueOf (myServer.substring (colon + 1)).intValue 
();
  +            return getServer (serverHost, serverPort);
  +         }
  +         
  +      } catch (IOException e)
  +      {
  +         NamingException ex = new CommunicationException (e.getMessage ());
  +         ex.setRootCause (e);
  +         throw ex;
  +      }
  +      finally
  +      {
  +         try { if (s != null) s.close (); } catch (Exception e) {}
  +      }
  +   }
  +   
  +   private void checkRef (Hashtable env)
  +   throws NamingException
      {
         if (naming == null)
         {
  @@ -761,24 +853,24 @@
            int port = 1099;
            
            // Locate naming service
  -         String urls = (String) env.get(Context.PROVIDER_URL);
  -         if (urls != null && urls.length() > 0)
  +         String urls = (String) env.get (Context.PROVIDER_URL);
  +         if (urls != null && urls.length () > 0)
            {
  -            StringTokenizer tokenizer = new StringTokenizer(urls, ",");
  -            while (tokenizer.hasMoreElements())
  +            StringTokenizer tokenizer = new StringTokenizer (urls, ",");
  +            while (tokenizer.hasMoreElements ())
               {
  -               String url = tokenizer.nextToken();
  -               int colon = url.indexOf(':');
  +               String url = tokenizer.nextToken ();
  +               int colon = url.indexOf (':');
                  if( colon < 0 )
                  {
                     host = url;
                  }
                  else
                  {
  -                  host = url.substring(0, colon);
  +                  host = url.substring (0, colon);
                     try
                     {
  -                     port = Integer.parseInt(url.substring(colon+1));
  +                     port = Integer.parseInt (url.substring (colon+1));
                     }
                     catch (Exception ex)
                     {
  @@ -788,11 +880,17 @@
                  try
                  {
                     // Get server from cache
  -                  naming = getServer(host, port);
  +                  naming = getServer (host, port);
                  }
  -               catch (Exception ignored) {}
  -            } 
  -            if (naming == null) throw new CommunicationException("Could not obtain 
connection to any of these urls: " + urls);
  +               catch (Exception ignored)
  +               {}
  +            }
  +            if (naming == null)
  +            {
  +               naming = discoverServer (env);
  +               if (naming == null)
  +                  throw new CommunicationException ("Could not obtain connection to 
any of these urls: " + urls);
  +            }
            }
            else
            {
  @@ -801,37 +899,39 @@
               
               if (naming == null)
               {
  -               // Local, but no local JNDI provider found!
  -               throw new ConfigurationException("No valid Context.PROVIDER_URL was 
found");
  +               naming = discoverServer (env);
  +               if (naming == null)
  +                  // Local, but no local JNDI provider found!
  +                  throw new ConfigurationException ("No valid Context.PROVIDER_URL 
was found");
               }
            }
         }
      }
  -
  -   private Name getAbsoluteName(Name n)
  -      throws NamingException
  +   
  +   private Name getAbsoluteName (Name n)
  +   throws NamingException
      {
  -      if (n.isEmpty())
  -         return composeName(n,prefix);
  -      else if (n.get(0).toString().equals("")) // Absolute name
  -         return n.getSuffix(1);
  +      if (n.isEmpty ())
  +         return composeName (n,prefix);
  +      else if (n.get (0).toString ().equals ("")) // Absolute name
  +         return n.getSuffix (1);
         else // Add prefix
  -         return composeName(n,prefix);
  +         return composeName (n,prefix);
      }
  -
  -   private Hashtable getEnv(Name n)
  -      throws InvalidNameException
  +   
  +   private Hashtable getEnv (Name n)
  +   throws InvalidNameException
      {
         Hashtable nameEnv = env;
  -      String serverInfo = parseNameForScheme(n);
  +      String serverInfo = parseNameForScheme (n);
         if( serverInfo != null )
         {
            // Set hostname:port value for the naming server
  -         nameEnv = (Hashtable)env.clone();
  -         nameEnv.put(Context.PROVIDER_URL, serverInfo);
  +         nameEnv = (Hashtable)env.clone ();
  +         nameEnv.put (Context.PROVIDER_URL, serverInfo);
         }
         return nameEnv;
      }
  -
  +   
      // Inner classes -------------------------------------------------
   }
  
  
  

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

Reply via email to