1 24 package org.ofbiz.base.util; 25 26 import java.util.Hashtable ; 27 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 31 import org.ofbiz.base.config.GenericConfigException; 32 import org.ofbiz.base.config.JNDIConfigUtil; 33 import org.ofbiz.base.util.cache.UtilCache; 34 35 43 public class JNDIContextFactory { 44 45 public static final String module = JNDIContextFactory.class.getName(); 46 static UtilCache contexts = new UtilCache("entity.JNDIContexts", 0, 0); 47 48 52 public static InitialContext getInitialContext(String jndiServerName) throws GenericConfigException { 53 InitialContext ic = (InitialContext ) contexts.get(jndiServerName); 54 55 if (ic == null) { 56 synchronized (JNDIContextFactory.class) { 57 ic = (InitialContext ) contexts.get(jndiServerName); 58 59 if (ic == null) { 60 JNDIConfigUtil.JndiServerInfo jndiServerInfo = JNDIConfigUtil.getJndiServerInfo(jndiServerName); 61 62 if (jndiServerInfo == null) { 63 throw new GenericConfigException("ERROR: no jndi-server definition was found with the name " + jndiServerName + " in jndiservers.xml"); 64 } 65 66 try { 67 if (UtilValidate.isEmpty(jndiServerInfo.contextProviderUrl)) { 68 ic = new InitialContext (); 69 } else { 70 Hashtable h = new Hashtable (); 71 72 h.put(Context.INITIAL_CONTEXT_FACTORY, jndiServerInfo.initialContextFactory); 73 h.put(Context.PROVIDER_URL, jndiServerInfo.contextProviderUrl); 74 if (jndiServerInfo.urlPkgPrefixes != null && jndiServerInfo.urlPkgPrefixes.length() > 0) 75 h.put(Context.URL_PKG_PREFIXES, jndiServerInfo.urlPkgPrefixes); 76 77 if (jndiServerInfo.securityPrincipal != null && jndiServerInfo.securityPrincipal.length() > 0) 78 h.put(Context.SECURITY_PRINCIPAL, jndiServerInfo.securityPrincipal); 79 if (jndiServerInfo.securityCredentials != null && jndiServerInfo.securityCredentials.length() > 0) 80 h.put(Context.SECURITY_CREDENTIALS, jndiServerInfo.securityCredentials); 81 82 ic = new InitialContext (h); 83 } 84 } catch (Exception e) { 85 String errorMsg = "Error getting JNDI initial context for server name " + jndiServerName; 86 87 Debug.logError(e, errorMsg, module); 88 throw new GenericConfigException(errorMsg, e); 89 } 90 91 if (ic != null) { 92 contexts.put(jndiServerName, ic); 93 } 94 } 95 } 96 } 97 98 return ic; 99 } 100 104 public static void clearInitialContext(String jndiServerName) { 105 InitialContext ic = (InitialContext ) contexts.get(jndiServerName); 106 if (ic != null) 107 contexts.remove(jndiServerName); 108 } 109 110 } 111 | Popular Tags |