View Javadoc

    1   // ========================================================================
    2   // $Id: NamingUtil.java 3680 2008-09-21 10:37:13Z janb $
    3   // Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.naming;
    17
    18  import java.util.HashMap;
    19  import java.util.Map;
    20
    21  import javax.naming.Binding;
    22  import javax.naming.Context;
    23  import javax.naming.Name;
    24  import javax.naming.NameNotFoundException;
    25  import javax.naming.NameParser;
    26  import javax.naming.NamingEnumeration;
    27  import javax.naming.NamingException;
    28
    29  import org.mortbay.log.Log;
    30
    31
    32  /**
    33   * Util.java
    34   *
    35   *
    36   * Created: Tue Jul  1 18:26:17 2003
    37   *
    38   * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
    39   * @version 1.0
    40   */
    41  public class NamingUtil
    42  {
    43
    44      /* ------------------------------------------------------------ */
    45      /**
    46       * Bind an object to a context ensuring all subcontexts
    47       * are created if necessary
    48       *
    49       * @param ctx the context into which to bind
    50       * @param name the name relative to context to bind
    51       * @param obj the object to be bound
    52       * @exception NamingException if an error occurs
    53       */
    54      public static Context bind (Context ctx, String nameStr, Object obj)
    55          throws NamingException
    56      {
    57          Name name = ctx.getNameParser("").parse(nameStr);
    58
    59          //no name, nothing to do
    60          if (name.size() == 0)
    61              return null;
    62
    63          Context subCtx = ctx;
    64
    65          //last component of the name will be the name to bind
    66          for (int i=0; i < name.size() - 1; i++)
    67          {
    68              try
    69              {
    70                  subCtx = (Context)subCtx.lookup (name.get(i));
    71                  if(Log.isDebugEnabled())Log.debug("Subcontext "+name.get(i)+" already exists");
    72              }
    73              catch (NameNotFoundException e)
    74              {
    75                  subCtx = subCtx.createSubcontext(name.get(i));
    76                  if(Log.isDebugEnabled())Log.debug("Subcontext "+name.get(i)+" created");
    77              }
    78          }
    79
    80          subCtx.rebind (name.get(name.size() - 1), obj);
    81          if(Log.isDebugEnabled())Log.debug("Bound object to "+name.get(name.size() - 1));
    82          return subCtx;
    83
    84      }
    85
    86
    87      public static void unbind (Context ctx)
    88      throws NamingException
    89      {
    90          //unbind everything in the context and all of its subdirectories
    91          NamingEnumeration ne = ctx.listBindings(ctx.getNameInNamespace());
    92
    93          while (ne.hasMoreElements())
    94          {
    95              Binding b = (Binding)ne.nextElement();
    96              if (b.getObject() instanceof Context)
    97              {
    98                  unbind((Context)b.getObject());
    99              }
    100             else
    101                 ctx.unbind(b.getName());
    102         }
    103     }
    104
    105     /**
    106      * Do a deep listing of the bindings for a context.
    107      * @param ctx the context containing the name for which to list the bindings
    108      * @param name the name in the context to list
    109      * @return map: key is fully qualified name, value is the bound object
    110      * @throws NamingException
    111      */
    112     public static Map flattenBindings (Context ctx, String name)
    113     throws NamingException
    114     {
    115         HashMap map = new HashMap();
    116
    117         //the context representation of name arg
    118         Context c = (Context)ctx.lookup (name);
    119         NameParser parser = c.getNameParser("");
    120         NamingEnumeration enm = ctx.listBindings(name);
    121         while (enm.hasMore())
    122         {
    123             Binding b = (Binding)enm.next();
    124
    125             if (b.getObject() instanceof Context)
    126             {
    127                 map.putAll(flattenBindings (c, b.getName()));
    128             }
    129             else
    130             {
    131                 Name compoundName = parser.parse (c.getNameInNamespace());
    132                 compoundName.add(b.getName());
    133                 map.put (compoundName.toString(), b.getObject());
    134             }
    135
    136         }
    137
    138         return map;
    139     }
    140
    141 }