001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.jndi;
018:
019: import java.util.HashSet;
020: import java.util.Hashtable;
021: import java.util.Iterator;
022: import java.util.Properties;
023: import javax.naming.*;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.compass.core.config.CompassEnvironment;
028: import org.compass.core.config.CompassSettings;
029:
030: public abstract class NamingHelper {
031:
032: private static final Log log = LogFactory
033: .getLog(NamingHelper.class);
034:
035: public static InitialContext getInitialContext(
036: CompassSettings settings) throws NamingException {
037: Hashtable hash = getJndiProperties(settings);
038: try {
039: return (hash.size() == 0) ? new InitialContext()
040: : new InitialContext(hash);
041: } catch (NamingException e) {
042: log.error(
043: "Could not obtain initial context with settings ["
044: + hash + "]", e);
045: throw e;
046: }
047: }
048:
049: /**
050: * Bind val to name in ctx, and make sure that all intermediate contexts
051: * exist.
052: *
053: * @param ctx
054: * the root context
055: * @param name
056: * the name as a string
057: * @param val
058: * the object to be bound
059: * @throws NamingException
060: */
061: public static void bind(Context ctx, String name, Object val)
062: throws NamingException {
063: try {
064: ctx.rebind(name, val);
065: } catch (Exception e) {
066: Name n = ctx.getNameParser("").parse(name);
067: while (n.size() > 1) {
068: String ctxName = n.get(0);
069:
070: Context subctx = null;
071: try {
072: subctx = (Context) ctx.lookup(ctxName);
073: } catch (NameNotFoundException nfe) {
074: // don't do nothing
075: }
076:
077: if (subctx != null) {
078: ctx = subctx;
079: } else {
080: ctx = ctx.createSubcontext(ctxName);
081: }
082: n = n.getSuffix(1);
083: }
084: ctx.rebind(n, val);
085: }
086: }
087:
088: /**
089: * Transform JNDI properties passed in the form
090: * <code>compass.jndi. [vaules]</code> to the format accepted by
091: * <code>InitialContext</code> by triming the leading "<code>compass.jndi</code>".
092: */
093: public static Properties getJndiProperties(CompassSettings settings) {
094: HashSet specialProps = new HashSet();
095: specialProps.add(CompassEnvironment.Jndi.CLASS);
096: specialProps.add(CompassEnvironment.Jndi.URL);
097: specialProps.add(CompassEnvironment.Jndi.ENABLE);
098:
099: Iterator iter = settings.keySet().iterator();
100: Properties result = new Properties();
101: while (iter.hasNext()) {
102: String prop = (String) iter.next();
103: if (prop.indexOf(CompassEnvironment.Jndi.PREFIX) > -1
104: && !specialProps.contains(prop)) {
105: result.setProperty(prop
106: .substring(CompassEnvironment.Jndi.PREFIX
107: .length() + 1), settings
108: .getSetting(prop));
109: }
110: }
111:
112: String jndiClass = settings
113: .getSetting(CompassEnvironment.Jndi.CLASS);
114: String jndiURL = settings
115: .getSetting(CompassEnvironment.Jndi.URL);
116: // we want to be able to just use the defaults,
117: // if JNDI environment properties are not supplied
118: // so don't put null in anywhere
119: if (jndiClass != null)
120: result.put(Context.INITIAL_CONTEXT_FACTORY, jndiClass);
121: if (jndiURL != null)
122: result.put(Context.PROVIDER_URL, jndiURL);
123:
124: return result;
125: }
126: }
|