1 /*
2 * Copyright (c) 2000 Peter Antman DN <peter.antman@dn.se>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package org.backsource.utils.naming;
19
20 import javax.naming.Context;
21 import javax.naming.Name;
22 import javax.naming.NameNotFoundException;
23 import javax.naming.NamingException;
24 /***
25
26 *
27 * @author Peter Antman
28 * @version $Revision: 1.1.1.1 $
29 */
30
31 public class NamingHelper {
32
33 public NamingHelper() {
34
35 }
36
37 public static void bind(Context ctx, String name, Object val) throws NamingException
38 {
39 // Bind val to name in ctx, and make sure that all intermediate contexts exist
40 Name n = ctx.getNameParser("").parse(name);
41 while (n.size() > 1)
42 {
43 String ctxName = n.get(0);
44 try
45 {
46 ctx = (Context)ctx.lookup(ctxName);
47 } catch (NameNotFoundException e)
48 {
49 ctx = ctx.createSubcontext(ctxName);
50 }
51 n = n.getSuffix(1);
52 }
53
54 ctx.bind(n.get(0), val);
55 }
56
57 public static void main(String[] args) {
58
59 }
60
61 } // NamingHelper