KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > NamingHelper


1 //$Id: NamingHelper.java,v 1.2 2004/08/10 05:06:14 oneovthafew Exp $
2
package org.hibernate.util;
3
4 import java.util.HashSet JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.Name JavaDoc;
12 import javax.naming.NameNotFoundException JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import org.hibernate.cfg.Environment;
18
19 public final class NamingHelper {
20
21     private static final Log log = LogFactory.getLog(NamingHelper.class);
22
23     public static InitialContext JavaDoc getInitialContext(Properties JavaDoc props) throws NamingException JavaDoc {
24
25         Hashtable JavaDoc hash = getJndiProperties(props);
26         log.info("JNDI InitialContext properties:" + hash);
27         try {
28             return ( hash.size()==0 ) ?
29             new InitialContext JavaDoc() :
30             new InitialContext JavaDoc(hash);
31         }
32         catch (NamingException JavaDoc e) {
33             log.error("Could not obtain initial context", e);
34             throw e;
35         }
36     }
37
38     /**
39      * Bind val to name in ctx, and make sure that all intermediate contexts exist.
40      *
41      * @param ctx the root context
42      * @param name the name as a string
43      * @param val the object to be bound
44      * @throws NamingException
45      */

46     public static void bind(Context JavaDoc ctx, String JavaDoc name, Object JavaDoc val) throws NamingException JavaDoc {
47         try {
48             log.trace("binding: " + name);
49             ctx.rebind(name, val);
50         }
51         catch (Exception JavaDoc e) {
52             Name JavaDoc n = ctx.getNameParser("").parse(name);
53             while ( n.size() > 1 ) {
54                 String JavaDoc ctxName = n.get(0);
55
56                 Context JavaDoc subctx=null;
57                 try {
58                     log.trace("lookup: " + ctxName);
59                     subctx = (Context JavaDoc) ctx.lookup(ctxName);
60                 }
61                 catch (NameNotFoundException JavaDoc nfe) {}
62
63                 if (subctx!=null) {
64                     log.debug("Found subcontext: " + ctxName);
65                     ctx = subctx;
66                 }
67                 else {
68                     log.info("Creating subcontext: " + ctxName);
69                     ctx = ctx.createSubcontext(ctxName);
70                 }
71                 n = n.getSuffix(1);
72             }
73             log.trace("binding: " + n);
74             ctx.rebind(n, val);
75         }
76         log.debug("Bound name: " + name);
77     }
78
79     /**
80      * Transform JNDI properties passed in the form <tt>hibernate.jndi.*</tt> to the
81      * format accepted by <tt>InitialContext</tt> by triming the leading "<tt>hibernate.jndi</tt>".
82      */

83     public static Properties JavaDoc getJndiProperties(Properties JavaDoc properties) {
84
85         HashSet JavaDoc specialProps = new HashSet JavaDoc();
86         specialProps.add(Environment.JNDI_CLASS);
87         specialProps.add(Environment.JNDI_URL);
88
89         Iterator JavaDoc iter = properties.keySet().iterator();
90         Properties JavaDoc result = new Properties JavaDoc();
91         while ( iter.hasNext() ) {
92             String JavaDoc prop = (String JavaDoc) iter.next();
93             if ( prop.indexOf(Environment.JNDI_PREFIX) > -1 && !specialProps.contains(prop) ) {
94                 result.setProperty(
95                     prop.substring( Environment.JNDI_PREFIX.length()+1 ),
96                     properties.getProperty(prop)
97                 );
98             }
99         }
100
101         String JavaDoc jndiClass = properties.getProperty(Environment.JNDI_CLASS);
102         String JavaDoc jndiURL = properties.getProperty(Environment.JNDI_URL);
103         // we want to be able to just use the defaults,
104
// if JNDI environment properties are not supplied
105
// so don't put null in anywhere
106
if (jndiClass != null) result.put(Context.INITIAL_CONTEXT_FACTORY, jndiClass);
107         if (jndiURL != null) result.put(Context.PROVIDER_URL, jndiURL);
108
109         return result;
110     }
111
112     private NamingHelper() {}
113
114 }
115
116
117
118
119
120
121
122
Popular Tags