001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: JNDIUtility.java 1206 2006-09-23 03:51:32Z fling $
023: */
024: package com.bostechcorp.cbesb.runtime.jms;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Context;
029: import javax.naming.Name;
030: import javax.naming.directory.InitialDirContext;
031:
032: import com.bostechcorp.cbesb.common.runtime.ConfigurationException;
033: import com.bostechcorp.cbesb.common.util.ErrorUtil;
034:
035: public class JNDIUtility {
036:
037: public static InitialDirContext initJNDI(String icf, String url)
038: throws ConfigurationException {
039:
040: // the context to be returned from this routine
041: InitialDirContext ctx = null;
042:
043: // hashtable for the JNDI initialization properties
044: Hashtable<String, String> environment = new Hashtable<String, String>();
045:
046: if (url == null) {
047: // A URL must be supplied, so throw an exception if none is
048: // available
049: throw new ConfigurationException(
050: "You must specify the -url providerURL parameter");
051: }
052:
053: // Firstly, store the name of the 'initial context factory'
054: environment.put(Context.INITIAL_CONTEXT_FACTORY, icf);
055:
056: // Secondly, specify the location of the JNDI service provider
057: environment.put(Context.PROVIDER_URL, url);
058:
059: // Finally, ensure that referrals are thrown. This is required
060: // as a workaround to a problem encountered with certain comb-
061: // inations of JNDI service provider and JNDI classes
062: environment.put(Context.REFERRAL, "throw");
063:
064: try {
065: // Attempt to connect to the JNDI service provider, supply-
066: // ing the properties previously supplied
067: ctx = new InitialDirContext(environment);
068: } catch (Exception e) {
069: // return an exception to the caller
070: ErrorUtil
071: .printError("Cannot create JNDI InitialContext", e);
072: ConfigurationException je = new ConfigurationException(
073: "Cannot create JNDI InitialContext", e);
074: throw je;
075: }
076: return ctx;
077: }
078:
079: public static Object lookup(Context ctx, String name)
080: throws ConfigurationException {
081: Object result = null;
082:
083: try {
084: // Attempt to lookup from the JNDI context
085: result = ctx.lookup(name);
086:
087: } catch (Exception e) {
088: // If this has failed, try the operation again, this
089: // time prefixing the lookup key with "cn=". Such a
090: // prefix is required when using an LDAP provider
091: try {
092: result = ctx.lookup("cn=" + name);
093: } catch (Exception e2) {
094: // After a second failure pass an exception to the caller
095: ConfigurationException je = new ConfigurationException(
096: "Unable to find '" + name + "' in JNDI", e);
097: throw je;
098: }
099: }
100: return result;
101: }
102: }
|