1 64 65 71 package com.jcorporate.expresso.core.db.datasource; 72 73 import com.jcorporate.expresso.core.db.config.JNDIConfig; 74 75 import javax.naming.Context ; 76 import javax.naming.InitialContext ; 77 import javax.naming.NamingException ; 78 import javax.sql.DataSource ; 79 import java.sql.Connection ; 80 import java.sql.SQLException ; 81 import java.util.Hashtable ; 82 83 84 93 public class JndiDataSource implements DataSourceInterface { 94 private static final String thisClass = JndiDataSource.class.getName() + "."; 95 96 97 100 protected DataSource ds = null; 101 102 105 protected JNDIConfig myJndi = null; 106 107 110 protected String dbURL = null; 111 112 116 public JndiDataSource() { 117 } 118 119 120 126 public JndiDataSource(JNDIConfig newJndi, String newURL) { 127 myJndi = newJndi; 128 dbURL = newURL; 129 } 130 131 139 140 public void setupContext() throws DSException { 141 String myName = (thisClass + "setupContext()"); 142 Context envContext = null; 143 Hashtable envTable = null; 144 145 try { 146 envTable = new Hashtable (); 147 if (!"".equals(myJndi.getInitialContextFactory())) { 148 envTable.put(Context.INITIAL_CONTEXT_FACTORY, myJndi.getInitialContextFactory()); 149 } 150 if (!"".equals(myJndi.getProviderURL())) { 151 envTable.put(Context.PROVIDER_URL, myJndi.getProviderURL()); 152 } 153 if (!"".equals(myJndi.getSecurityPrincipal())) { 154 envTable.put(Context.SECURITY_PRINCIPAL, myJndi.getSecurityPrincipal()); 155 } 156 if (!"".equals(myJndi.getSecurityCredentials())) { 157 envTable.put(Context.SECURITY_CREDENTIALS, myJndi.getSecurityCredentials()); 158 } 159 if (!"".equals(myJndi.getSecurityAuthentication())) { 160 envTable.put(Context.SECURITY_AUTHENTICATION, myJndi.getSecurityAuthentication()); 161 } 162 if (!"".equals(myJndi.getSecurityProtocol())) { 163 envTable.put(Context.SECURITY_PROTOCOL, myJndi.getSecurityProtocol()); 164 } 165 if (!"".equals(myJndi.getDnsURL())) { 166 envTable.put(Context.DNS_URL, myJndi.getDnsURL()); 167 } 168 envContext = new InitialContext (envTable); 169 if (envContext != null) { 170 ds = (DataSource ) envContext.lookup(getDbURL()); 171 envContext.close(); 172 } else { 173 throw new DSException(myName + ":Cannot initialize Initial Context Factory for Datasource Object via JNDI. " + 174 " DATASOURCE : '(" + myJndi.getInitialContextFactory() + ")'"); 175 } 176 } catch (NamingException ne) { 177 throw new DSException(myName + 178 ":Cannot get Naming Object via JNDI " + 179 " and INITIAL_CONTEXT_FACTORY '" + myJndi.getInitialContextFactory() + "'" + 180 " and DATASOURCE '" + getDbURL() + "'" + 181 ":JDBC returned a null connection. " + 182 " Error : " + ne.getMessage()); 183 } 184 185 } 186 187 190 public Connection getConnection() throws DSException { 191 String myName = (thisClass + "." + "getConnection()"); 192 193 try { 194 if (ds == null) { 195 throw new DSException(myName + 196 ":Cannot get a connection to database via JNDI DataSource Handle is null'"); 197 } 198 return ds.getConnection(); 199 } catch (SQLException se) { 200 throw new DSException(myName + 201 ":Cannot get a connection to database via JNDI DataSource '" + 202 getDbURL() + "' (" + ")", se.getMessage()); 203 } 204 205 } 206 207 208 211 public Connection getConnection(String Username, String Password) throws DSException { 212 String myName = (thisClass + "." + "getConnection(String, String)"); 213 214 try { 215 if (ds == null) { 216 throw new DSException(myName + 217 ":Cannot get a connection to database via JNDI DataSource Handle is null'"); 218 } 219 return ds.getConnection(Username, Password); 220 } catch (SQLException se) { 221 throw new DSException(myName + 222 ":Cannot get a connection to database via JNDI DataSource '" + 223 getDbURL() + "' (" + Username + ", " + Password + ")", se.getMessage()); 224 } 225 226 } 227 228 233 public JNDIConfig getMyJndi() { 234 return myJndi; 235 } 236 237 242 public void setMyJndi(JNDIConfig myJndi) { 243 this.myJndi = myJndi; 244 } 245 246 247 252 public String getDbURL() { 253 return dbURL; 254 } 255 256 261 public void setDbURL(String dbURL) { 262 this.dbURL = dbURL; 263 } 264 265 266 } 267 268 | Popular Tags |