001: /*
002: * $Id: JNDIContextFactory.java,v 1.1 2003/08/16 20:18:33 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030:
031: import org.ofbiz.base.config.GenericConfigException;
032: import org.ofbiz.base.config.JNDIConfigUtil;
033:
034: /**
035: * JNDIContextFactory - central source for JNDI Contexts by helper name
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
039: * @version $Revision: 1.1 $
040: * @since 2.0
041: */
042: public class JNDIContextFactory {
043:
044: public static final String module = JNDIContextFactory.class
045: .getName();
046: static UtilCache contexts = new UtilCache("entity.JNDIContexts", 0,
047: 0);
048:
049: /**
050: * Return the initial context according to the entityengine.xml parameters that correspond to the given prefix
051: * @return the JNDI initial context
052: */
053: public static InitialContext getInitialContext(String jndiServerName)
054: throws GenericConfigException {
055: InitialContext ic = (InitialContext) contexts
056: .get(jndiServerName);
057:
058: if (ic == null) {
059: synchronized (JNDIContextFactory.class) {
060: ic = (InitialContext) contexts.get(jndiServerName);
061:
062: if (ic == null) {
063: JNDIConfigUtil.JndiServerInfo jndiServerInfo = JNDIConfigUtil
064: .getJndiServerInfo(jndiServerName);
065:
066: if (jndiServerInfo == null) {
067: throw new GenericConfigException(
068: "ERROR: no jndi-server definition was found with the name "
069: + jndiServerName
070: + " in jndiservers.xml");
071: }
072:
073: try {
074: if (UtilValidate
075: .isEmpty(jndiServerInfo.contextProviderUrl)) {
076: ic = new InitialContext();
077: } else {
078: Hashtable h = new Hashtable();
079:
080: h
081: .put(
082: Context.INITIAL_CONTEXT_FACTORY,
083: jndiServerInfo.initialContextFactory);
084: h.put(Context.PROVIDER_URL,
085: jndiServerInfo.contextProviderUrl);
086: if (jndiServerInfo.urlPkgPrefixes != null
087: && jndiServerInfo.urlPkgPrefixes
088: .length() > 0)
089: h.put(Context.URL_PKG_PREFIXES,
090: jndiServerInfo.urlPkgPrefixes);
091:
092: if (jndiServerInfo.securityPrincipal != null
093: && jndiServerInfo.securityPrincipal
094: .length() > 0)
095: h
096: .put(
097: Context.SECURITY_PRINCIPAL,
098: jndiServerInfo.securityPrincipal);
099: if (jndiServerInfo.securityCredentials != null
100: && jndiServerInfo.securityCredentials
101: .length() > 0)
102: h
103: .put(
104: Context.SECURITY_CREDENTIALS,
105: jndiServerInfo.securityCredentials);
106:
107: ic = new InitialContext(h);
108: }
109: } catch (Exception e) {
110: String errorMsg = "Error getting JNDI initial context for server name "
111: + jndiServerName;
112:
113: Debug.logError(e, errorMsg, module);
114: throw new GenericConfigException(errorMsg, e);
115: }
116:
117: if (ic != null) {
118: contexts.put(jndiServerName, ic);
119: }
120: }
121: }
122: }
123:
124: return ic;
125: }
126:
127: /**
128: * Removes an entry from the JNDI cache.
129: * @param jndiServerName
130: */
131: public static void clearInitialContext(String jndiServerName) {
132: InitialContext ic = (InitialContext) contexts
133: .get(jndiServerName);
134: if (ic != null)
135: contexts.remove(jndiServerName);
136: }
137:
138: }
|