NamingUtilites.java

  1. package org.itracker.util;

  2. import org.apache.log4j.Logger;

  3. import javax.naming.Context;
  4. import javax.naming.InitialContext;
  5. import javax.naming.NameNotFoundException;
  6. import javax.naming.NamingException;
  7. import java.util.Hashtable;

  8. /**
  9.  * Utilities class for naming
  10.  *
  11.  * @author ranks@rosa.com
  12.  */
  13. public class NamingUtilites {

  14.     private static final Logger log = Logger.getLogger(NamingUtilites.class
  15.             .getName());


  16.     /**
  17.      * Read a String value of any type of object
  18.      *
  19.      * @param ctx          -
  20.      *                     Context for lookup
  21.      * @param lookupName   -
  22.      *                     lookup name
  23.      * @param defaultValue -
  24.      *                     default value
  25.      */
  26.     public static final String getStringValue(Context ctx, String lookupName,
  27.                                               String defaultValue) {
  28.         String value = null;
  29.         Object val;
  30.         if (log.isDebugEnabled()) {
  31.             log.debug("getStringValue: look up " + lookupName + " in context "
  32.                     + ctx + ", default: " + defaultValue);

  33.         }


  34.         if (null == ctx) {
  35.             log.debug("getStringValue: creating new InitialContext");
  36.             try {
  37.                 ctx = new InitialContext();
  38.                 if (log.isDebugEnabled()) {
  39.                     log.debug("created new InitialContext: " + ctx);
  40.                 }
  41.             } catch (NamingException e) {
  42.                 log.warn("getStringValue: failed to create InitialContext", e);
  43.                 if (log.isDebugEnabled())
  44.                     log.debug("getStringValue: context was null, exception from new initial context caught", e);
  45.             }
  46.         }


  47.         if (null != ctx) {


  48.             val = lookup(ctx, lookupName);
  49.             if (val instanceof String) {
  50.                 value = (String) val;
  51.             } else {
  52.                 value = (null == val) ? null : String.valueOf(val);
  53.             }

  54.         }
  55.         value = (null == value) ? defaultValue : value;
  56.         if (log.isDebugEnabled()) {
  57.             log.debug("getStringValue: returning '" + value + "' for " + lookupName);

  58.         }

  59.         return value;
  60.     }

  61.     /**
  62.      * savely get object from naming context
  63.      *
  64.      * @return Object - value
  65.      * @throws IllegalArgumentException -
  66.      *                                  if any argument is null, or the lookup name was empty
  67.      */
  68.     public static Object lookup(Context ctx, String lookupName) {
  69.         if (null == ctx) {
  70.             throw new IllegalArgumentException("context must not be null");
  71.         }
  72.         if (null == lookupName || lookupName.trim().length() == 0) {
  73.             throw new IllegalArgumentException(
  74.                     "lookup name must not be empty, got: "
  75.                             + ((null == lookupName) ? "<null>" : "'"
  76.                             + lookupName + "'"));
  77.         }
  78.         try {
  79.             return ctx.lookup(lookupName);
  80.         } catch (NamingException e) {
  81.             if (e instanceof NameNotFoundException) {
  82.                 if (log.isDebugEnabled()) {
  83.                     log.debug("lookup: failed to lookup " + lookupName
  84.                             + ": name not found");
  85.                 }
  86.             } else {
  87.                 log.warn("lookup: failed to lookup " + lookupName
  88.                         + " in context " + ctx, e);
  89.             }
  90.             return null;
  91.         }
  92.     }

  93.     /**
  94.      * get a default initial context
  95.      *
  96.      * @return initial context.
  97.      * @throws IllegalStateException - if initial context was null
  98.      */
  99.     public static final Context getDefaultInitialContext(Hashtable<?, ?> environment) throws NamingException {
  100.         return new InitialContext(environment);
  101.     }

  102.     /**
  103.      * get a default initial context
  104.      *
  105.      * @return initial context.
  106.      * @throws IllegalStateException - if initial context was null
  107.      */
  108.     public static final Context getDefaultInitialContext() throws NamingException {
  109.         return new InitialContext();
  110.     }
  111. }