KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > transaction > JNDIFactory


1 /*
2  * $Id: JNDIFactory.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
25 package org.ofbiz.entity.transaction;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35 import javax.sql.XAConnection JavaDoc;
36 import javax.sql.XADataSource JavaDoc;
37 import javax.transaction.TransactionManager JavaDoc;
38 import javax.transaction.UserTransaction JavaDoc;
39
40 import org.ofbiz.base.config.GenericConfigException;
41 import org.ofbiz.base.util.Debug;
42 import org.ofbiz.base.util.GeneralException;
43 import org.ofbiz.base.util.JNDIContextFactory;
44 import org.ofbiz.entity.GenericEntityException;
45 import org.ofbiz.entity.config.DatasourceInfo;
46 import org.ofbiz.entity.config.EntityConfigUtil;
47 import org.ofbiz.entity.jdbc.ConnectionFactory;
48 import org.w3c.dom.Element JavaDoc;
49
50 /**
51  * Central source for Tyrex JTA objects from JNDI
52  *
53  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
54  * @version $Rev: 5462 $
55  * @since 2.0
56  */

57 public class JNDIFactory implements TransactionFactoryInterface {
58     
59     // Debug module name
60
public static final String JavaDoc module = JNDIFactory.class.getName();
61
62     static TransactionManager JavaDoc transactionManager = null;
63     static UserTransaction JavaDoc userTransaction = null;
64
65     // protected static UtilCache dsCache = new UtilCache("entity.JndiDataSources", 0, 0);
66
protected static Map JavaDoc dsCache = new HashMap JavaDoc();
67
68     public TransactionManager JavaDoc getTransactionManager() {
69         if (transactionManager == null) {
70             synchronized (JNDIFactory.class) {
71                 // try again inside the synch just in case someone when through while we were waiting
72
if (transactionManager == null) {
73                     try {
74                         String JavaDoc jndiName = EntityConfigUtil.getTxFactoryTxMgrJndiName();
75                         String JavaDoc jndiServerName = EntityConfigUtil.getTxFactoryTxMgrJndiServerName();
76
77                         if (jndiName != null && jndiName.length() > 0) {
78                             // if (Debug.verboseOn()) Debug.logVerbose("[JNDIFactory.getTransactionManager] Trying JNDI name " + jndiName, module);
79

80                             try {
81                                 InitialContext JavaDoc ic = JNDIContextFactory.getInitialContext(jndiServerName);
82
83                                 if (ic != null) {
84                                     transactionManager = (TransactionManager JavaDoc) ic.lookup(jndiName);
85                                 }
86                             } catch (NamingException JavaDoc ne) {
87                                 Debug.logWarning(ne, "NamingException while finding TransactionManager named " + jndiName + " in JNDI.", module);
88                                 transactionManager = null;
89                             }
90                             if (transactionManager == null) {
91                                 Debug.logWarning("[JNDIFactory.getTransactionManager] Failed to find TransactionManager named " + jndiName + " in JNDI.", module);
92                             }
93                         }
94                     } catch (GeneralException e) {
95                         Debug.logError(e, module);
96                         transactionManager = null;
97                     }
98                 }
99             }
100         }
101         return transactionManager;
102     }
103
104     public UserTransaction JavaDoc getUserTransaction() {
105         if (userTransaction == null) {
106             synchronized (JNDIFactory.class) {
107                 // try again inside the synch just in case someone when through while we were waiting
108
if (userTransaction == null) {
109                     try {
110                         String JavaDoc jndiName = EntityConfigUtil.getTxFactoryUserTxJndiName();
111                         String JavaDoc jndiServerName = EntityConfigUtil.getTxFactoryUserTxJndiServerName();
112
113                         if (jndiName != null && jndiName.length() > 0) {
114                             // if (Debug.verboseOn()) Debug.logVerbose("[JNDIFactory.getTransactionManager] Trying JNDI name " + jndiName, module);
115

116                             try {
117                                 InitialContext JavaDoc ic = JNDIContextFactory.getInitialContext(jndiServerName);
118
119                                 if (ic != null) {
120                                     userTransaction = (UserTransaction JavaDoc) ic.lookup(jndiName);
121                                 }
122                             } catch (NamingException JavaDoc ne) {
123                                 Debug.logWarning(ne, "NamingException while finding UserTransaction named " + jndiName + " in JNDI.", module);
124                                 userTransaction = null;
125                             }
126                             if (userTransaction == null) {
127                                 Debug.logWarning("[JNDIFactory.getUserTransaction] Failed to find UserTransaction named " + jndiName + " in JNDI.", module);
128                             }
129                         }
130                     } catch (GeneralException e) {
131                         Debug.logError(e, module);
132                         transactionManager = null;
133                     }
134                 }
135             }
136         }
137         return userTransaction;
138     }
139     
140     public String JavaDoc getTxMgrName() {
141         return "jndi";
142     }
143     
144     public Connection JavaDoc getConnection(String JavaDoc helperName) throws SQLException JavaDoc, GenericEntityException {
145         DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName);
146
147         if (datasourceInfo.jndiJdbcElement != null) {
148             Element JavaDoc jndiJdbcElement = datasourceInfo.jndiJdbcElement;
149             String JavaDoc jndiName = jndiJdbcElement.getAttribute("jndi-name");
150             String JavaDoc jndiServerName = jndiJdbcElement.getAttribute("jndi-server-name");
151             Connection JavaDoc con = getJndiConnection(jndiName, jndiServerName);
152             if (con != null) return TransactionFactory.getCursorConnection(helperName, con);
153         } else {
154            // Debug.logError("JNDI loaded is the configured transaction manager but no jndi-jdbc element was specified in the " + helperName + " datasource. Please check your configuration.", module);
155
}
156         
157         if (datasourceInfo.inlineJdbcElement != null) {
158             Connection JavaDoc otherCon = ConnectionFactory.tryGenericConnectionSources(helperName, datasourceInfo.inlineJdbcElement);
159             return TransactionFactory.getCursorConnection(helperName, otherCon);
160         } else {
161             //no real need to print an error here
162
return null;
163         }
164     }
165     
166     public static Connection JavaDoc getJndiConnection(String JavaDoc jndiName, String JavaDoc jndiServerName) throws SQLException JavaDoc, GenericEntityException {
167         // if (Debug.verboseOn()) Debug.logVerbose("Trying JNDI name " + jndiName, module);
168
Object JavaDoc ds;
169
170         ds = dsCache.get(jndiName);
171         if (ds != null) {
172             if (ds instanceof XADataSource JavaDoc) {
173                 XADataSource JavaDoc xads = (XADataSource JavaDoc) ds;
174
175                 return TransactionUtil.enlistConnection(xads.getXAConnection());
176             } else {
177                 DataSource JavaDoc nds = (DataSource JavaDoc) ds;
178
179                 return nds.getConnection();
180             }
181         }
182
183         synchronized (ConnectionFactory.class) {
184             // try again inside the synch just in case someone when through while we were waiting
185
ds = dsCache.get(jndiName);
186             if (ds != null) {
187                 if (ds instanceof XADataSource JavaDoc) {
188                     XADataSource JavaDoc xads = (XADataSource JavaDoc) ds;
189
190                     return TransactionUtil.enlistConnection(xads.getXAConnection());
191                 } else {
192                     DataSource JavaDoc nds = (DataSource JavaDoc) ds;
193
194                     return nds.getConnection();
195                 }
196             }
197
198             try {
199                 if (Debug.infoOn()) Debug.logInfo("Doing JNDI lookup for name " + jndiName, module);
200                 InitialContext JavaDoc ic = JNDIContextFactory.getInitialContext(jndiServerName);
201
202                 if (ic != null) {
203                     ds = ic.lookup(jndiName);
204                 } else {
205                     Debug.logWarning("Initial Context returned was NULL for server name " + jndiServerName, module);
206                 }
207
208                 if (ds != null) {
209                     if (Debug.verboseOn()) Debug.logVerbose("Got a Datasource object.", module);
210                     dsCache.put(jndiName, ds);
211                     Connection JavaDoc con = null;
212
213                     if (ds instanceof XADataSource JavaDoc) {
214                         if (Debug.infoOn()) Debug.logInfo("Got XADataSource for name " + jndiName, module);
215                         XADataSource JavaDoc xads = (XADataSource JavaDoc) ds;
216                         XAConnection JavaDoc xac = xads.getXAConnection();
217
218                         con = TransactionUtil.enlistConnection(xac);
219                     } else {
220                         if (Debug.infoOn()) Debug.logInfo("Got DataSource for name " + jndiName, module);
221                         DataSource JavaDoc nds = (DataSource JavaDoc) ds;
222
223                         con = nds.getConnection();
224                     }
225
226                     /* NOTE: This code causes problems because settting the transaction isolation level after a transaction has started is a no-no
227                      * The question is: how should we do this?
228                      String isolationLevel = jndiJdbcElement.getAttribute("isolation-level");
229                      if (con != null && isolationLevel != null && isolationLevel.length() > 0) {
230                      if ("Serializable".equals(isolationLevel)) {
231                      con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
232                      } else if ("RepeatableRead".equals(isolationLevel)) {
233                      con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
234                      } else if ("ReadUncommitted".equals(isolationLevel)) {
235                      con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
236                      } else if ("ReadCommitted".equals(isolationLevel)) {
237                      con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
238                      } else if ("None".equals(isolationLevel)) {
239                      con.setTransactionIsolation(Connection.TRANSACTION_NONE);
240                      }
241                      }
242                      */

243
244                     // if (con != null) if (Debug.infoOn()) Debug.logInfo("[ConnectionFactory.getConnection] Got JNDI connection with catalog: " + con.getCatalog(), module);
245
return con;
246                 } else {
247                     Debug.logError("Datasource returned was NULL.", module);
248                 }
249             } catch (NamingException JavaDoc ne) {
250                 Debug.logWarning(ne, "[ConnectionFactory.getConnection] Failed to find DataSource named " + jndiName + " in JNDI server with name " + jndiServerName + ". Trying normal database.", module);
251             } catch (GenericConfigException gce) {
252                 throw new GenericEntityException("Problems with the JNDI configuration.", gce.getNested());
253             }
254         }
255         return null;
256     }
257     
258     public void shutdown() {}
259 }
260
Popular Tags