KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > naming > Util


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.naming;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.LinkRef JavaDoc;
27 import javax.naming.Name JavaDoc;
28 import javax.naming.NameNotFoundException JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.jboss.logging.Logger;
32
33 /** A static utility class for common JNDI operations.
34  *
35  * @author Scott.Stark@jboss.org
36  * @author adrian@jboss.com
37  * @version $Revision: 1958 $
38  */

39 public class Util
40 {
41    private static final Logger log = Logger.getLogger(Util.class);
42
43    /** Create a subcontext including any intermediate contexts.
44     @param ctx, the parent JNDI Context under which value will be bound
45     @param name, the name relative to ctx of the subcontext.
46     @return The new or existing JNDI subcontext
47     @throws javax.naming.NamingException on any JNDI failure
48     */

49    public static Context JavaDoc createSubcontext(Context JavaDoc ctx, String JavaDoc name) throws NamingException JavaDoc
50    {
51       Name JavaDoc n = ctx.getNameParser("").parse(name);
52       return createSubcontext(ctx, n);
53    }
54
55    /** Create a subcontext including any intermediate contexts.
56     @param ctx, the parent JNDI Context under which value will be bound
57     @param name, the name relative to ctx of the subcontext.
58     @return The new or existing JNDI subcontext
59     @throws NamingException on any JNDI failure
60     */

61    public static Context JavaDoc createSubcontext(Context JavaDoc ctx, Name JavaDoc name) throws NamingException JavaDoc
62    {
63       Context JavaDoc subctx = ctx;
64       for (int pos = 0; pos < name.size(); pos++)
65       {
66          String JavaDoc ctxName = name.get(pos);
67          try
68          {
69             subctx = (Context JavaDoc) ctx.lookup(ctxName);
70          }
71          catch (NameNotFoundException JavaDoc e)
72          {
73             subctx = ctx.createSubcontext(ctxName);
74          }
75          // The current subctx will be the ctx for the next name component
76
ctx = subctx;
77       }
78       return subctx;
79    }
80
81    /** Bind val to name in ctx, and make sure that all intermediate contexts exist
82     @param ctx, the parent JNDI Context under which value will be bound
83     @param name, the name relative to ctx where value will be bound
84     @param value, the value to bind.
85     */

86    public static void bind(Context JavaDoc ctx, String JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
87    {
88       Name JavaDoc n = ctx.getNameParser("").parse(name);
89       bind(ctx, n, value);
90    }
91
92    /** Bind val to name in ctx, and make sure that all intermediate contexts exist
93     @param ctx, the parent JNDI Context under which value will be bound
94     @param name, the name relative to ctx where value will be bound
95     @param value, the value to bind.
96     */

97    public static void bind(Context JavaDoc ctx, Name JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
98    {
99       int size = name.size();
100       String JavaDoc atom = name.get(size - 1);
101       Context JavaDoc parentCtx = createSubcontext(ctx, name.getPrefix(size - 1));
102       parentCtx.bind(atom, value);
103    }
104
105    /** Rebind val to name in ctx, and make sure that all intermediate contexts exist
106     @param ctx, the parent JNDI Context under which value will be bound
107     @param name, the name relative to ctx where value will be bound
108     @param value, the value to bind.
109     */

110    public static void rebind(Context JavaDoc ctx, String JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
111    {
112       Name JavaDoc n = ctx.getNameParser("").parse(name);
113       rebind(ctx, n, value);
114    }
115
116    /** Rebind val to name in ctx, and make sure that all intermediate contexts exist
117     @param ctx, the parent JNDI Context under which value will be bound
118     @param name, the name relative to ctx where value will be bound
119     @param value, the value to bind.
120     */

121    public static void rebind(Context JavaDoc ctx, Name JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
122    {
123       int size = name.size();
124       String JavaDoc atom = name.get(size - 1);
125       Context JavaDoc parentCtx = createSubcontext(ctx, name.getPrefix(size - 1));
126       parentCtx.rebind(atom, value);
127    }
128
129    /** Unbinds a name from ctx, and removes parents if they are empty
130     @param ctx, the parent JNDI Context under which the name will be unbound
131     @param name, The name to unbind
132     */

133    public static void unbind(Context JavaDoc ctx, String JavaDoc name) throws NamingException JavaDoc
134    {
135       unbind(ctx, ctx.getNameParser("").parse(name));
136    }
137
138    /** Unbinds a name from ctx, and removes parents if they are empty
139     @param ctx, the parent JNDI Context under which the name will be unbound
140     @param name, The name to unbind
141     */

142    public static void unbind(Context JavaDoc ctx, Name JavaDoc name) throws NamingException JavaDoc
143    {
144       ctx.unbind(name); //unbind the end node in the name
145
int sz = name.size();
146       // walk the tree backwards, stopping at the domain
147
while (--sz > 0)
148       {
149          Name JavaDoc pname = name.getPrefix(sz);
150          try
151          {
152             ctx.destroySubcontext(pname);
153          }
154          catch (NamingException JavaDoc e)
155          {
156             log.trace("Unable to remove context " + pname, e);
157             break;
158          }
159       }
160    }
161    
162    /**
163     * Lookup an object in the default initial context
164     *
165     * @param name the name to lookup
166     * @param clazz the expected type
167     * @return the object
168     * @throws Exception for any error
169     */

170    public static Object JavaDoc lookup(String JavaDoc name, Class JavaDoc clazz) throws Exception JavaDoc
171    {
172       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
173       try
174       {
175          return lookup(ctx, name, clazz);
176       }
177       finally
178       {
179          ctx.close();
180       }
181    }
182    
183    /**
184     * Lookup an object in the default initial context
185     *
186     * @param name the name to lookup
187     * @param clazz the expected type
188     * @return the object
189     * @throws Exception for any error
190     */

191    public static Object JavaDoc lookup(Name JavaDoc name, Class JavaDoc clazz) throws Exception JavaDoc
192    {
193       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
194       try
195       {
196          return lookup(ctx, name, clazz);
197       }
198       finally
199       {
200          ctx.close();
201       }
202    }
203    
204    /**
205     * Lookup an object in the given context
206     *
207     * @param context the context
208     * @param name the name to lookup
209     * @param clazz the expected type
210     * @return the object
211     * @throws Exception for any error
212     */

213    public static Object JavaDoc lookup(Context JavaDoc context, String JavaDoc name, Class JavaDoc clazz) throws Exception JavaDoc
214    {
215       Object JavaDoc result = context.lookup(name);
216       checkObject(context, name, result, clazz);
217       return result;
218    }
219    
220    /**
221     * Lookup an object in the given context
222     *
223     * @param context the context
224     * @param name the name to lookup
225     * @param clazz the expected type
226     * @return the object
227     * @throws Exception for any error
228     */

229    public static Object JavaDoc lookup(Context JavaDoc context, Name JavaDoc name, Class JavaDoc clazz) throws Exception JavaDoc
230    {
231       Object JavaDoc result = context.lookup(name);
232       checkObject(context, name.toString(), result, clazz);
233       return result;
234    }
235
236    /**
237     * Create a link
238     *
239     * @param fromName the from name
240     * @param toName the to name
241     * @throws NamingException for any error
242     */

243    public static void createLinkRef(String JavaDoc fromName, String JavaDoc toName) throws NamingException JavaDoc
244    {
245       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
246       createLinkRef(ctx, fromName, toName);
247    }
248    
249    /**
250     * Create a link
251     *
252     * @param ctx the context
253     * @param fromName the from name
254     * @param toName the to name
255     * @throws NamingException for any error
256     */

257    public static void createLinkRef(Context JavaDoc ctx, String JavaDoc fromName, String JavaDoc toName) throws NamingException JavaDoc
258    {
259       LinkRef JavaDoc link = new LinkRef JavaDoc(toName);
260       Context JavaDoc fromCtx = ctx;
261       Name JavaDoc name = ctx.getNameParser("").parse(fromName);
262       String JavaDoc atom = name.get(name.size()-1);
263       for(int n = 0; n < name.size()-1; n ++)
264       {
265          String JavaDoc comp = name.get(n);
266          try
267          {
268             fromCtx = (Context JavaDoc) fromCtx.lookup(comp);
269          }
270          catch(NameNotFoundException JavaDoc e)
271          {
272             fromCtx = fromCtx.createSubcontext(comp);
273          }
274       }
275
276       log.debug("atom: " + atom);
277       log.debug("link: " + link);
278       
279       fromCtx.rebind(atom, link);
280
281       log.debug("Bound link " + fromName + " to " + toName);
282    }
283    
284    /**
285     * Remove the link ref
286     *
287     * @param name the name of the link binding
288     * @throws NamingException for any error
289     */

290    public static void removeLinkRef(String JavaDoc name) throws NamingException JavaDoc
291    {
292       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
293       removeLinkRef(ctx, name);
294    }
295    
296    /**
297     * Remove the link ref
298     *
299     * @param ctx the context
300     * @param name the name of the link binding
301     * @throws NamingException for any error
302     */

303    public static void removeLinkRef(Context JavaDoc ctx, String JavaDoc name) throws NamingException JavaDoc
304    {
305       log.debug("Unbinding link " + name);
306       ctx.unbind(name);
307    }
308
309    
310    /**
311     * Checks an object implements the given class
312     *
313     * @param context the context
314     * @param name the name to lookup
315     * @param object the object
316     * @param clazz the expected type
317     */

318    protected static void checkObject(Context JavaDoc context, String JavaDoc name, Object JavaDoc object, Class JavaDoc clazz) throws Exception JavaDoc
319    {
320       Class JavaDoc objectClass = object.getClass();
321       if (clazz.isAssignableFrom(objectClass) == false)
322       {
323          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(100);
324          buffer.append("Object at '").append(name);
325          buffer.append("' in context ").append(context.getEnvironment());
326          buffer.append(" is not an instance of ");
327          appendClassInfo(buffer, clazz);
328          buffer.append(" object class is ");
329          appendClassInfo(buffer, object.getClass());
330          throw new ClassCastException JavaDoc(buffer.toString());
331       }
332    }
333    
334    /**
335     * Append Class Info
336     *
337     * @param buffer the buffer to append to
338     * @param clazz the class to describe
339     */

340    protected static void appendClassInfo(StringBuffer JavaDoc buffer, Class JavaDoc clazz)
341    {
342       buffer.append("[class=").append(clazz.getName());
343       buffer.append(" classloader=").append(clazz.getClassLoader());
344       buffer.append(" interfaces={");
345       Class JavaDoc[] interfaces = clazz.getInterfaces();
346       for (int i=0; i<interfaces.length; ++i)
347       {
348          if (i > 0)
349             buffer.append(", ");
350          buffer.append("interface=").append(interfaces[i].getName());
351          buffer.append(" classloader=").append(interfaces[i].getClassLoader());
352       }
353       buffer.append("}]");
354    }
355 }
356
Popular Tags