01: /*
02: * $Id: NamingHelper.java 544 2005-10-06 09:18:26Z nebulosu $
03: * (c) Copyright 2004 con:cern development team.
04: *
05: * This file is part of con:cern (http://concern.org).
06: *
07: * con:cern is free software; you can redistribute it and/or modify
08: * it under the terms of the GNU Lesser General Public License
09: * as published by the Free Software Foundation; either version 2.1
10: * of the License, or (at your option) any later version.
11: *
12: * Please see COPYING for the complete licence.
13: */
14: package org.concern.jboss;
15:
16: import javax.naming.Context;
17: import javax.naming.Name;
18: import javax.naming.NameNotFoundException;
19: import javax.naming.NamingException;
20:
21: public final class NamingHelper {
22:
23: private NamingHelper() {
24: }
25:
26: /**
27: * Bind val to name in ctx, and make sure that all intermediate contexts exist.
28: *
29: * @param ctx the root context
30: * @param name the name as a string
31: * @param val the object to be bound
32: * @throws javax.naming.NamingException
33: */
34: public static void bind(Context ctx, String name, Object val)
35: throws NamingException {
36: try {
37: ctx.rebind(name, val);
38: } catch (Exception e) {
39: Name n = ctx.getNameParser("").parse(name);
40: while (n.size() > 1) {
41: String ctxName = n.get(0);
42:
43: Context subctx = null;
44: try {
45: subctx = (Context) ctx.lookup(ctxName);
46: } catch (NameNotFoundException nfe) {
47: }
48:
49: if (subctx != null) {
50: ctx = subctx;
51: } else {
52: ctx = ctx.createSubcontext(ctxName);
53: }
54: n = n.getSuffix(1);
55: }
56: ctx.rebind(n, val);
57: }
58: }
59: }
|