1 24 25 package org.ofbiz.entity.transaction; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.sql.DataSource ; 35 import javax.sql.XAConnection ; 36 import javax.sql.XADataSource ; 37 import javax.transaction.TransactionManager ; 38 import javax.transaction.UserTransaction ; 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 ; 49 50 57 public class JNDIFactory implements TransactionFactoryInterface { 58 59 public static final String module = JNDIFactory.class.getName(); 61 62 static TransactionManager transactionManager = null; 63 static UserTransaction userTransaction = null; 64 65 protected static Map dsCache = new HashMap (); 67 68 public TransactionManager getTransactionManager() { 69 if (transactionManager == null) { 70 synchronized (JNDIFactory.class) { 71 if (transactionManager == null) { 73 try { 74 String jndiName = EntityConfigUtil.getTxFactoryTxMgrJndiName(); 75 String jndiServerName = EntityConfigUtil.getTxFactoryTxMgrJndiServerName(); 76 77 if (jndiName != null && jndiName.length() > 0) { 78 80 try { 81 InitialContext ic = JNDIContextFactory.getInitialContext(jndiServerName); 82 83 if (ic != null) { 84 transactionManager = (TransactionManager ) ic.lookup(jndiName); 85 } 86 } catch (NamingException 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 getUserTransaction() { 105 if (userTransaction == null) { 106 synchronized (JNDIFactory.class) { 107 if (userTransaction == null) { 109 try { 110 String jndiName = EntityConfigUtil.getTxFactoryUserTxJndiName(); 111 String jndiServerName = EntityConfigUtil.getTxFactoryUserTxJndiServerName(); 112 113 if (jndiName != null && jndiName.length() > 0) { 114 116 try { 117 InitialContext ic = JNDIContextFactory.getInitialContext(jndiServerName); 118 119 if (ic != null) { 120 userTransaction = (UserTransaction ) ic.lookup(jndiName); 121 } 122 } catch (NamingException 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 getTxMgrName() { 141 return "jndi"; 142 } 143 144 public Connection getConnection(String helperName) throws SQLException , GenericEntityException { 145 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 146 147 if (datasourceInfo.jndiJdbcElement != null) { 148 Element jndiJdbcElement = datasourceInfo.jndiJdbcElement; 149 String jndiName = jndiJdbcElement.getAttribute("jndi-name"); 150 String jndiServerName = jndiJdbcElement.getAttribute("jndi-server-name"); 151 Connection con = getJndiConnection(jndiName, jndiServerName); 152 if (con != null) return TransactionFactory.getCursorConnection(helperName, con); 153 } else { 154 } 156 157 if (datasourceInfo.inlineJdbcElement != null) { 158 Connection otherCon = ConnectionFactory.tryGenericConnectionSources(helperName, datasourceInfo.inlineJdbcElement); 159 return TransactionFactory.getCursorConnection(helperName, otherCon); 160 } else { 161 return null; 163 } 164 } 165 166 public static Connection getJndiConnection(String jndiName, String jndiServerName) throws SQLException , GenericEntityException { 167 Object ds; 169 170 ds = dsCache.get(jndiName); 171 if (ds != null) { 172 if (ds instanceof XADataSource ) { 173 XADataSource xads = (XADataSource ) ds; 174 175 return TransactionUtil.enlistConnection(xads.getXAConnection()); 176 } else { 177 DataSource nds = (DataSource ) ds; 178 179 return nds.getConnection(); 180 } 181 } 182 183 synchronized (ConnectionFactory.class) { 184 ds = dsCache.get(jndiName); 186 if (ds != null) { 187 if (ds instanceof XADataSource ) { 188 XADataSource xads = (XADataSource ) ds; 189 190 return TransactionUtil.enlistConnection(xads.getXAConnection()); 191 } else { 192 DataSource nds = (DataSource ) 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 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 con = null; 212 213 if (ds instanceof XADataSource ) { 214 if (Debug.infoOn()) Debug.logInfo("Got XADataSource for name " + jndiName, module); 215 XADataSource xads = (XADataSource ) ds; 216 XAConnection 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 nds = (DataSource ) ds; 222 223 con = nds.getConnection(); 224 } 225 226 243 244 return con; 246 } else { 247 Debug.logError("Datasource returned was NULL.", module); 248 } 249 } catch (NamingException 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 |