src/share/classes/com/sun/jndi/rmi/registry/RegistryContextFactory.java

Print this page




  78         }
  79         /*
  80          * No need to clone env here.  If getObjectInstance()
  81          * returns something other than a RegistryContext (which
  82          * happens if you're looking up an object bound in the
  83          * registry, as opposed to looking up the registry itself),
  84          * then the context is GCed right away and there's no need to
  85          * clone the environment.  If getObjectInstance() returns a
  86          * RegistryContext, then it still goes through
  87          * GenericURLContext, which calls RegistryContext.lookup()
  88          * with an empty name, which clones the environment.
  89          */
  90         Object obj = URLsToObject(getURLs((Reference)ref), env);
  91         if (obj instanceof RegistryContext) {
  92             RegistryContext ctx = (RegistryContext)obj;
  93             ctx.reference = (Reference)ref;
  94         }
  95         return obj;
  96     }
  97 
  98     private static Context URLToContext(String url, Hashtable env)
  99             throws NamingException
 100     {
 101         rmiURLContextFactory factory = new rmiURLContextFactory();
 102         Object obj = factory.getObjectInstance(url, null, null, env);
 103 
 104         if (obj instanceof Context) {
 105             return (Context)obj;
 106         } else {
 107             throw (new NotContextException(url));
 108         }
 109     }
 110 
 111     private static Object URLsToObject(String[] urls, Hashtable env)
 112             throws NamingException
 113     {
 114         rmiURLContextFactory factory = new rmiURLContextFactory();
 115         return factory.getObjectInstance(urls, null, null, env);
 116     }
 117 
 118     /**
 119      * Reads environment to find URL of initial context.
 120      * The default URL is "rmi:".
 121      */
 122     private static String getInitCtxURL(Hashtable env) {
 123 
 124         final String defaultURL = "rmi:";
 125 
 126         String url = null;
 127         if (env != null) {
 128             url = (String)env.get(Context.PROVIDER_URL);
 129         }
 130         return ((url != null) ? url : defaultURL);
 131     }
 132 
 133     /**
 134      * Returns true if argument is an RMI registry reference.
 135      */
 136     private static boolean isRegistryRef(Object obj) {
 137 
 138         if (!(obj instanceof Reference)) {
 139             return false;
 140         }
 141         String thisClassName = RegistryContextFactory.class.getName();
 142         Reference ref = (Reference)obj;
 143 
 144         return thisClassName.equals(ref.getFactoryClassName());
 145     }
 146 
 147     /**
 148      * Returns the URLs contained within an RMI registry reference.
 149      */
 150     private static String[] getURLs(Reference ref) throws NamingException {
 151 
 152         int size = 0;   // number of URLs
 153         String[] urls = new String[ref.size()];
 154 
 155         Enumeration addrs = ref.getAll();
 156         while (addrs.hasMoreElements()) {
 157             RefAddr addr = (RefAddr)addrs.nextElement();
 158 
 159             if ((addr instanceof StringRefAddr) &&
 160                 addr.getType().equals(ADDRESS_TYPE)) {
 161 
 162                 urls[size++] = (String)addr.getContent();
 163             }
 164         }
 165         if (size == 0) {
 166             throw (new ConfigurationException(
 167                     "Reference contains no valid addresses"));
 168         }
 169 
 170         // Trim URL array down to size.
 171         if (size == ref.size()) {
 172             return urls;
 173         }
 174         String[] urls2 = new String[size];
 175         System.arraycopy(urls, 0, urls2, 0, size);
 176         return urls2;
 177     }


  78         }
  79         /*
  80          * No need to clone env here.  If getObjectInstance()
  81          * returns something other than a RegistryContext (which
  82          * happens if you're looking up an object bound in the
  83          * registry, as opposed to looking up the registry itself),
  84          * then the context is GCed right away and there's no need to
  85          * clone the environment.  If getObjectInstance() returns a
  86          * RegistryContext, then it still goes through
  87          * GenericURLContext, which calls RegistryContext.lookup()
  88          * with an empty name, which clones the environment.
  89          */
  90         Object obj = URLsToObject(getURLs((Reference)ref), env);
  91         if (obj instanceof RegistryContext) {
  92             RegistryContext ctx = (RegistryContext)obj;
  93             ctx.reference = (Reference)ref;
  94         }
  95         return obj;
  96     }
  97 
  98     private static Context URLToContext(String url, Hashtable<?,?> env)
  99             throws NamingException
 100     {
 101         rmiURLContextFactory factory = new rmiURLContextFactory();
 102         Object obj = factory.getObjectInstance(url, null, null, env);
 103 
 104         if (obj instanceof Context) {
 105             return (Context)obj;
 106         } else {
 107             throw (new NotContextException(url));
 108         }
 109     }
 110 
 111     private static Object URLsToObject(String[] urls, Hashtable<?,?> env)
 112             throws NamingException
 113     {
 114         rmiURLContextFactory factory = new rmiURLContextFactory();
 115         return factory.getObjectInstance(urls, null, null, env);
 116     }
 117 
 118     /**
 119      * Reads environment to find URL of initial context.
 120      * The default URL is "rmi:".
 121      */
 122     private static String getInitCtxURL(Hashtable<?,?> env) {
 123 
 124         final String defaultURL = "rmi:";
 125 
 126         String url = null;
 127         if (env != null) {
 128             url = (String)env.get(Context.PROVIDER_URL);
 129         }
 130         return ((url != null) ? url : defaultURL);
 131     }
 132 
 133     /**
 134      * Returns true if argument is an RMI registry reference.
 135      */
 136     private static boolean isRegistryRef(Object obj) {
 137 
 138         if (!(obj instanceof Reference)) {
 139             return false;
 140         }
 141         String thisClassName = RegistryContextFactory.class.getName();
 142         Reference ref = (Reference)obj;
 143 
 144         return thisClassName.equals(ref.getFactoryClassName());
 145     }
 146 
 147     /**
 148      * Returns the URLs contained within an RMI registry reference.
 149      */
 150     private static String[] getURLs(Reference ref) throws NamingException {
 151 
 152         int size = 0;   // number of URLs
 153         String[] urls = new String[ref.size()];
 154 
 155         Enumeration<RefAddr> addrs = ref.getAll();
 156         while (addrs.hasMoreElements()) {
 157             RefAddr addr = addrs.nextElement();
 158 
 159             if ((addr instanceof StringRefAddr) &&
 160                 addr.getType().equals(ADDRESS_TYPE)) {
 161 
 162                 urls[size++] = (String)addr.getContent();
 163             }
 164         }
 165         if (size == 0) {
 166             throw (new ConfigurationException(
 167                     "Reference contains no valid addresses"));
 168         }
 169 
 170         // Trim URL array down to size.
 171         if (size == ref.size()) {
 172             return urls;
 173         }
 174         String[] urls2 = new String[size];
 175         System.arraycopy(urls, 0, urls2, 0, size);
 176         return urls2;
 177     }