User: starksm 
  Date: 01/04/22 23:08:06

  Modified:    src/main/org/jnp/interfaces NamingContext.java
                        NamingContextFactory.java
  Log:
  Add support for PROVIDER_URL values that refer to subcontexts
  
  Revision  Changes    Path
  1.7       +171 -169  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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NamingContext.java        2001/04/09 20:50:27     1.6
  +++ NamingContext.java        2001/04/23 06:08:06     1.7
  @@ -33,82 +33,85 @@
    *   @see <related>
    *   @author oberg
    *   @author [EMAIL PROTECTED]
  - *   @version $Revision: 1.6 $
  + *   @version $Revision: 1.7 $
    */
   public class NamingContext
      implements Context, java.io.Serializable
   {
  -   // Constants -----------------------------------------------------
  -    
  -   // 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();
  +    // Constants -----------------------------------------------------
  +    /** @since 1.7 */
  +    static final long serialVersionUID = 8906455608484282128L;
  +    static Naming localServer;
  +
  +    // Attributes ----------------------------------------------------
  +    Naming naming;
  +    Hashtable env;
  +    Name prefix;
  +
  +    NameParser parser = new NamingParser();
  +
  +    // Static --------------------------------------------------------
        
  -     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));
  -                     cachedServers = newServers;
  -             }
  -     }
  +    // 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));
  +            cachedServers = newServers;
  +        }
  +    }
      
      static Naming getServer(String host, int port)
                throws NamingException
      {
  -             WeakReference ref = (WeakReference)cachedServers.get(host+":"+port);
  -     
  -             Naming server;
  -             if (ref != null)
  -             {
  -                     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);
  -                     } catch (IOException e2)
  -                     {
  -                        NamingException ex = new 
ServiceUnavailableException(e2.getMessage());
  -                        ex.setRootCause(e2);
  -                        throw ex;
  -                     }
  -                     
  -                     // Get stub from naming server
  +        WeakReference ref = (WeakReference)cachedServers.get(host+":"+port);
  +
  +        Naming server;
  +        if (ref != null)
  +        {
  +            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);
  +            } catch (IOException 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();
  -                     
  -                     // Add it to cache
  -                     addServer(host+":"+port, server);
  -                     
  -                     return server;
  +
  +            // Add it to cache
  +            addServer(host+":"+port, server);
  +
  +            return server;
        } catch (IOException e)
        {
           NamingException ex = new CommunicationException(e.getMessage());
  @@ -153,14 +156,60 @@
        }
                
      }
  -     
  -     static Naming localServer;
  -     
  -     public static void setLocal(Naming server)
  -     {
  -             localServer = 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.
  +    */
  +    static String parseNameForScheme(Name n) throws InvalidNameException
  +    {
  +        String serverInfo = null;
  +        if( n.size() > 0 )
  +        {
  +            String scheme = n.get(0);
  +            int schemeLength = 0;
  +            if( scheme.startsWith("java:") )
  +                schemeLength = 5;
  +            else if( scheme.startsWith("jnp:") )
  +                schemeLength = 4;
  +            if( schemeLength > 0 )
  +            {
  +                String suffix = scheme.substring(schemeLength);
  +                if( suffix.length() == 0 )
  +                {
  +                    // Scheme was "url:/..."
  +                    n.remove(0);
  +                    if( n.get(0).equals("") && n.size() > 1 )
  +                    {
  +                        // Scheme was "url://hostname:port/..."
  +                        // Get hostname:port value for the naming server
  +                        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);
  +                    }
  +                }
  +                else
  +                {
  +                    // Scheme was "url:foo" -> reinsert "foo"
  +                    n.remove(0);
  +                    n.add(0, suffix);
  +                }
  +            }
  +        }
  +        return serverInfo;
  +    }
  +
  +    public static void setLocal(Naming server)
  +    {
  +        localServer = server;
  +    }
  +
      // Constructors --------------------------------------------------
      public NamingContext(Hashtable e, Name baseName, Naming server)
         throws NamingException
  @@ -169,7 +218,7 @@
            this.prefix = parser.parse("");
         else
            this.prefix = baseName;
  -      
  +
         if (e != null)
            this.env = (Hashtable)e.clone();
         else
  @@ -260,8 +309,8 @@
            {
               className = ((Reference)obj).getClassName();
            }
  -         
  -         naming.bind(getAbsoluteName(name),obj, className);
  +         name = getAbsoluteName(name);
  +         naming.bind(name,obj, className);
         } catch (CannotProceedException cpe)
         {
            cpe.setEnvironment(env);
  @@ -295,7 +344,7 @@
            
         try
         {
  -                     Name n = getAbsoluteName(name);
  +     Name n = getAbsoluteName(name);
            Object res = naming.lookup(n);
            
            if (res instanceof MarshalledObject)
  @@ -584,29 +633,29 @@
         return createSubcontext(getNameParser(name).parse(name));
      }
   
  -   public Context createSubcontext(Name name)
  +    public Context createSubcontext(Name name)
         throws NamingException 
  -   {
  -      Hashtable env = getEnv(name);
  -      checkRef(env);
  -      
  -      try
  -      {
  -         return naming.createSubcontext(getAbsoluteName(name));
  -      } catch (CannotProceedException cpe)
  -      {
  -         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);
  -         throw ex;
  -      }
  -   }
  +    {
  +        Hashtable env = getEnv(name);
  +        checkRef(env);
  +        try
  +        {
  +            name = getAbsoluteName(name);
  +            return naming.createSubcontext(name);
  +        } catch (CannotProceedException cpe)
  +        {
  +            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);
  +            throw ex;
  +        }
  +    }
   
      public Object addToEnvironment(String propName, Object propVal)
         throws NamingException 
  @@ -687,12 +736,6 @@
          return link;
     }
   
  -   // Y overrides ---------------------------------------------------
  -
  -   // Package protected ---------------------------------------------
  -    
  -   // Protected -----------------------------------------------------
  -    
      // Private -------------------------------------------------------
      private void checkRef(Hashtable env)
         throws NamingException
  @@ -731,71 +774,30 @@
         }
      }
   
  -   private Name getAbsoluteName(Name n)
  +    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);
  -      else // Add prefix
  -         return composeName(n,prefix);
  -   }
  -   
  -   private Hashtable getEnv(Name n)
  -      throws InvalidNameException
  -   {
  -      if (n.size() == 0)
  -         return env;
  -         
  -      if (n.get(0).startsWith("java:"))
  -      {
  -         return getEnvForScheme("java:", n);
  -      } else if (n.get(0).startsWith("jnp:"))
  -      {
  -         return getEnvForScheme("jnp:", n);
  -      } else
  -      {
  -         return env;
  -      }
  -   }
  -   
  -   private Hashtable getEnvForScheme(String url, Name n)
  -      throws InvalidNameException
  -   {
  -      String scheme = n.get(0).substring(url.length());
  -      if (scheme.equals(""))
  -      {
  -         // Scheme was "url:/..."
  -         n.remove(0);
  -         if (n.size() > 0)
  -         {
  -            if (n.get(0).equals("") && n.size() > 1)
  -            {
  -               // Scheme was "url://somehost"
  -               
  -               // Get provider
  -               String provider = n.get(1);
  -               Hashtable newEnv = (Hashtable)env.clone();
  -               newEnv.put(Context.PROVIDER_URL, provider);
  -               n.remove(0);
  -               n.remove(0);
  -               return newEnv;
  -            } else
  -            {
  -               return env;
  -            }
  -         } else
  -         {
  -            return env;
  -         }
  -      } else
  -      {
  -         // Scheme was "url:foo" -> reinsert "foo"
  -         n.remove(0);
  -         n.add(0,scheme);
  -         return env;
  -      }
  -   }
  +    {
  +        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);
  +    }
  +
  +    private Hashtable getEnv(Name n)
  +        throws InvalidNameException
  +    {
  +        Hashtable nameEnv = env;
  +        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);
  +        }
  +        return nameEnv;
  +    }
  +
      // Inner classes -------------------------------------------------
   }
  
  
  
  1.3       +33 -32    jnp/src/main/org/jnp/interfaces/NamingContextFactory.java
  
  Index: NamingContextFactory.java
  ===================================================================
  RCS file: /cvsroot/jboss/jnp/src/main/org/jnp/interfaces/NamingContextFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NamingContextFactory.java 2000/12/12 10:57:09     1.2
  +++ NamingContextFactory.java 2001/04/23 06:08:06     1.3
  @@ -8,36 +8,46 @@
   package org.jnp.interfaces;
   
   import java.util.Hashtable;
  -import javax.naming.*;
  +import javax.naming.CompoundName;
  +import javax.naming.Context;
  +import javax.naming.Name;
  +import javax.naming.NamingException;
  +import javax.naming.Reference;
   import javax.naming.spi.*;
   
  -/**
  - *   <description> 
  - *      
  - *   @see <related>
  - *   @author $Author: oberg $
  - *   @version $Revision: 1.2 $
  +/** The jnp naming provider InitialContextFactory implementation.
  +
  +@see javax.naming.spi.InitialContextFactory
  +
  + *   @author $Author: starksm $
  + *   @author [EMAIL PROTECTED]
  + *   @version $Revision: 1.3 $
    */
   public class NamingContextFactory
      implements InitialContextFactory, ObjectFactory
   {
  -   // Constants -----------------------------------------------------
  -    
  -   // Attributes ----------------------------------------------------
  -    
  -   // Static --------------------------------------------------------
  -   
  -   // Constructors --------------------------------------------------
  -   
  -   // Public --------------------------------------------------------
  -
  -   // InitialContextFactory implementation --------------------------
  -   public Context getInitialContext(Hashtable env) 
  +    // InitialContextFactory implementation --------------------------
  +    public Context getInitialContext(Hashtable env) 
         throws NamingException
  -   {
  -      return new NamingContext(env, null, null);
  -   }
  -   
  +    {
  +        String providerURL = (String) env.get(Context.PROVIDER_URL);
  +        Name prefix = null;
  +        if( providerURL != null )
  +        {
  +            Name name = new CompoundName(providerURL, NamingParser.syntax);
  +            String serverInfo = NamingContext.parseNameForScheme(name);
  +            if( serverInfo != null )
  +            {
  +               // Set hostname:port value for the naming server
  +               env = (Hashtable) env.clone();
  +               env.put(Context.PROVIDER_URL, serverInfo);
  +               // Set the context prefix to name
  +               prefix = name;
  +            }
  +        }
  +        return new NamingContext(env, prefix, null);
  +    }
  +
      // ObjectFactory implementation ----------------------------------
      public Object getObjectInstance(Object obj,
                                   Name name,
  @@ -50,14 +60,5 @@
         Reference ref = (Reference)obj;
         return ctx.lookup((String)ref.get("URL").getContent());
      }
  -    
  -   // Y overrides ---------------------------------------------------
  -
  -   // Package protected ---------------------------------------------
  -    
  -   // Protected -----------------------------------------------------
  -    
  -   // Private -------------------------------------------------------
   
  -   // Inner classes -------------------------------------------------
  -}
  \ No newline at end of file
  +}
  
  
  

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

Reply via email to