001: //$Id: NamingHelper.java 8149 2005-09-11 21:10:52Z oneovthafew $
002: package org.hibernate.util;
003:
004: import java.util.HashSet;
005: import java.util.Hashtable;
006: import java.util.Iterator;
007: import java.util.Properties;
008:
009: import javax.naming.Context;
010: import javax.naming.InitialContext;
011: import javax.naming.Name;
012: import javax.naming.NameNotFoundException;
013: import javax.naming.NamingException;
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: import org.hibernate.cfg.Environment;
018:
019: public final class NamingHelper {
020:
021: private static final Log log = LogFactory
022: .getLog(NamingHelper.class);
023:
024: public static InitialContext getInitialContext(Properties props)
025: throws NamingException {
026:
027: Hashtable hash = getJndiProperties(props);
028: log.info("JNDI InitialContext properties:" + hash);
029: try {
030: return hash.size() == 0 ? new InitialContext()
031: : new InitialContext(hash);
032: } catch (NamingException e) {
033: log.error("Could not obtain initial context", e);
034: throw e;
035: }
036: }
037:
038: /**
039: * Bind val to name in ctx, and make sure that all intermediate contexts exist.
040: *
041: * @param ctx the root context
042: * @param name the name as a string
043: * @param val the object to be bound
044: * @throws NamingException
045: */
046: public static void bind(Context ctx, String name, Object val)
047: throws NamingException {
048: try {
049: log.trace("binding: " + name);
050: ctx.rebind(name, val);
051: } catch (Exception e) {
052: Name n = ctx.getNameParser("").parse(name);
053: while (n.size() > 1) {
054: String ctxName = n.get(0);
055:
056: Context subctx = null;
057: try {
058: log.trace("lookup: " + ctxName);
059: subctx = (Context) ctx.lookup(ctxName);
060: } catch (NameNotFoundException nfe) {
061: }
062:
063: if (subctx != null) {
064: log.debug("Found subcontext: " + ctxName);
065: ctx = subctx;
066: } else {
067: log.info("Creating subcontext: " + ctxName);
068: ctx = ctx.createSubcontext(ctxName);
069: }
070: n = n.getSuffix(1);
071: }
072: log.trace("binding: " + n);
073: ctx.rebind(n, val);
074: }
075: log.debug("Bound name: " + name);
076: }
077:
078: /**
079: * Transform JNDI properties passed in the form <tt>hibernate.jndi.*</tt> to the
080: * format accepted by <tt>InitialContext</tt> by triming the leading "<tt>hibernate.jndi</tt>".
081: */
082: public static Properties getJndiProperties(Properties properties) {
083:
084: HashSet specialProps = new HashSet();
085: specialProps.add(Environment.JNDI_CLASS);
086: specialProps.add(Environment.JNDI_URL);
087:
088: Iterator iter = properties.keySet().iterator();
089: Properties result = new Properties();
090: while (iter.hasNext()) {
091: String prop = (String) iter.next();
092: if (prop.indexOf(Environment.JNDI_PREFIX) > -1
093: && !specialProps.contains(prop)) {
094: result.setProperty(
095: prop
096: .substring(Environment.JNDI_PREFIX
097: .length() + 1), properties
098: .getProperty(prop));
099: }
100: }
101:
102: String jndiClass = properties
103: .getProperty(Environment.JNDI_CLASS);
104: String jndiURL = properties.getProperty(Environment.JNDI_URL);
105: // we want to be able to just use the defaults,
106: // if JNDI environment properties are not supplied
107: // so don't put null in anywhere
108: if (jndiClass != null)
109: result.put(Context.INITIAL_CONTEXT_FACTORY, jndiClass);
110: if (jndiURL != null)
111: result.put(Context.PROVIDER_URL, jndiURL);
112:
113: return result;
114: }
115:
116: private NamingHelper() {
117: }
118:
119: }
|