001: // ========================================================================
002: // $Id: NamingUtil.java 231 2006-02-19 15:09:58Z janb $
003: // Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.naming;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import javax.naming.Binding;
022: import javax.naming.Context;
023: import javax.naming.Name;
024: import javax.naming.NameNotFoundException;
025: import javax.naming.NameParser;
026: import javax.naming.NamingEnumeration;
027: import javax.naming.NamingException;
028:
029: import org.mortbay.log.Log;
030:
031: /**
032: * Util.java
033: *
034: *
035: * Created: Tue Jul 1 18:26:17 2003
036: *
037: * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
038: * @version 1.0
039: */
040: public class NamingUtil {
041:
042: /* ------------------------------------------------------------ */
043: /**
044: * Bind an object to a context ensuring all subcontexts
045: * are created if necessary
046: *
047: * @param ctx the context into which to bind
048: * @param name the name relative to context to bind
049: * @param obj the object to be bound
050: * @exception NamingException if an error occurs
051: */
052: public static void bind(Context ctx, String nameStr, Object obj)
053: throws NamingException {
054: Name name = ctx.getNameParser("").parse(nameStr);
055:
056: //no name, nothing to do
057: if (name.size() == 0)
058: return;
059:
060: Context subCtx = ctx;
061:
062: //last component of the name will be the name to bind
063: for (int i = 0; i < name.size() - 1; i++) {
064: try {
065: subCtx = (Context) subCtx.lookup(name.get(i));
066: if (Log.isDebugEnabled())
067: Log.debug("Subcontext " + name.get(i)
068: + " already exists");
069: } catch (NameNotFoundException e) {
070: subCtx = subCtx.createSubcontext(name.get(i));
071: if (Log.isDebugEnabled())
072: Log.debug("Subcontext " + name.get(i) + " created");
073: }
074: }
075:
076: subCtx.rebind(name.get(name.size() - 1), obj);
077: if (Log.isDebugEnabled())
078: Log.debug("Bound object to " + name.get(name.size() - 1));
079: }
080:
081: /**
082: * Do a deep listing of the bindings for a context.
083: * @param ctx the context containing the name for which to list the bindings
084: * @param name the name in the context to list
085: * @return map: key is fully qualified name, value is the bound object
086: * @throws NamingException
087: */
088: public static Map flattenBindings(Context ctx, String name)
089: throws NamingException {
090: HashMap map = new HashMap();
091:
092: //the context representation of name arg
093: Context c = (Context) ctx.lookup(name);
094: NameParser parser = c.getNameParser("");
095: NamingEnumeration enm = ctx.listBindings(name);
096: while (enm.hasMore()) {
097: Binding b = (Binding) enm.next();
098:
099: if (b.getObject() instanceof Context) {
100: map.putAll(flattenBindings(c, b.getName()));
101: } else {
102: Name compoundName = parser
103: .parse(c.getNameInNamespace());
104: compoundName.add(b.getName());
105: map.put(compoundName.toString(), b.getObject());
106: }
107:
108: }
109:
110: return map;
111: }
112:
113: }
|