KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > JNDIContextFactory


1 /*
2  * $Id: JNDIContextFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.util.Hashtable JavaDoc;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
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 /**
36  * JNDIContextFactory - central source for JNDI Contexts by helper name
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
40  * @version $Rev: 5462 $
41  * @since 2.0
42  */

43 public class JNDIContextFactory {
44     
45     public static final String JavaDoc module = JNDIContextFactory.class.getName();
46     static UtilCache contexts = new UtilCache("entity.JNDIContexts", 0, 0);
47
48     /**
49      * Return the initial context according to the entityengine.xml parameters that correspond to the given prefix
50      * @return the JNDI initial context
51      */

52     public static InitialContext JavaDoc getInitialContext(String JavaDoc jndiServerName) throws GenericConfigException {
53         InitialContext JavaDoc ic = (InitialContext JavaDoc) contexts.get(jndiServerName);
54
55         if (ic == null) {
56             synchronized (JNDIContextFactory.class) {
57                 ic = (InitialContext JavaDoc) 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 JavaDoc();
69                         } else {
70                             Hashtable JavaDoc h = new Hashtable JavaDoc();
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 JavaDoc(h);
83                         }
84                     } catch (Exception JavaDoc e) {
85                         String JavaDoc 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     /**
101      * Removes an entry from the JNDI cache.
102      * @param jndiServerName
103      */

104     public static void clearInitialContext(String JavaDoc jndiServerName) {
105         InitialContext JavaDoc ic = (InitialContext JavaDoc) contexts.get(jndiServerName);
106         if (ic != null)
107             contexts.remove(jndiServerName);
108     }
109
110 }
111
Popular Tags