src/share/classes/com/sun/jndi/url/dns/dnsURLContextFactory.java

Print this page




  45 
  46 public class dnsURLContextFactory implements ObjectFactory {
  47 
  48     public Object getObjectInstance(Object urlInfo, Name name,
  49                                     Context nameCtx, Hashtable<?,?> env)
  50             throws NamingException {
  51 
  52         if (urlInfo == null) {
  53             return (new dnsURLContext(env));
  54         } else if (urlInfo instanceof String) {
  55             return getUsingURL((String) urlInfo, env);
  56         } else if (urlInfo instanceof String[]) {
  57             return getUsingURLs((String[]) urlInfo, env);
  58         } else {
  59             throw (new ConfigurationException(
  60                     "dnsURLContextFactory.getObjectInstance: " +
  61                     "argument must be a DNS URL String or an array of them"));
  62         }
  63     }
  64 
  65     private static Object getUsingURL(String url, Hashtable env)
  66             throws NamingException {
  67 
  68         dnsURLContext urlCtx = new dnsURLContext(env);
  69         try {
  70             return urlCtx.lookup(url);
  71         } finally {
  72             urlCtx.close();
  73         }
  74     }
  75 
  76     /*
  77      * Try each URL until lookup() succeeds for one of them.
  78      * If all URLs fail, throw one of the exceptions arbitrarily.
  79      * Not pretty, but potentially more informative than returning null.
  80      */
  81     private static Object getUsingURLs(String[] urls, Hashtable env)
  82             throws NamingException {
  83 
  84         if (urls.length == 0) {
  85             throw (new ConfigurationException(
  86                     "dnsURLContextFactory: empty URL array"));
  87         }
  88         dnsURLContext urlCtx = new dnsURLContext(env);
  89         try {
  90             NamingException ne = null;
  91             for (int i = 0; i < urls.length; i++) {
  92                 try {
  93                     return urlCtx.lookup(urls[i]);
  94                 } catch (NamingException e) {
  95                     ne = e;
  96                 }
  97             }
  98             throw ne;   // failure:  throw one of the exceptions caught
  99         } finally {
 100             urlCtx.close();
 101         }


  45 
  46 public class dnsURLContextFactory implements ObjectFactory {
  47 
  48     public Object getObjectInstance(Object urlInfo, Name name,
  49                                     Context nameCtx, Hashtable<?,?> env)
  50             throws NamingException {
  51 
  52         if (urlInfo == null) {
  53             return (new dnsURLContext(env));
  54         } else if (urlInfo instanceof String) {
  55             return getUsingURL((String) urlInfo, env);
  56         } else if (urlInfo instanceof String[]) {
  57             return getUsingURLs((String[]) urlInfo, env);
  58         } else {
  59             throw (new ConfigurationException(
  60                     "dnsURLContextFactory.getObjectInstance: " +
  61                     "argument must be a DNS URL String or an array of them"));
  62         }
  63     }
  64 
  65     private static Object getUsingURL(String url, Hashtable<?,?> env)
  66             throws NamingException {
  67 
  68         dnsURLContext urlCtx = new dnsURLContext(env);
  69         try {
  70             return urlCtx.lookup(url);
  71         } finally {
  72             urlCtx.close();
  73         }
  74     }
  75 
  76     /*
  77      * Try each URL until lookup() succeeds for one of them.
  78      * If all URLs fail, throw one of the exceptions arbitrarily.
  79      * Not pretty, but potentially more informative than returning null.
  80      */
  81     private static Object getUsingURLs(String[] urls, Hashtable<?,?> env)
  82             throws NamingException {
  83 
  84         if (urls.length == 0) {
  85             throw (new ConfigurationException(
  86                     "dnsURLContextFactory: empty URL array"));
  87         }
  88         dnsURLContext urlCtx = new dnsURLContext(env);
  89         try {
  90             NamingException ne = null;
  91             for (int i = 0; i < urls.length; i++) {
  92                 try {
  93                     return urlCtx.lookup(urls[i]);
  94                 } catch (NamingException e) {
  95                     ne = e;
  96                 }
  97             }
  98             throw ne;   // failure:  throw one of the exceptions caught
  99         } finally {
 100             urlCtx.close();
 101         }