1 28 29 package net.sf.jguard.ext.database; 30 31 import java.sql.Connection ; 32 import java.sql.DriverManager ; 33 import java.sql.SQLException ; 34 import java.util.Arrays ; 35 import java.util.HashMap ; 36 import java.util.Hashtable ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 import javax.naming.InitialContext ; 43 import javax.naming.NamingException ; 44 import javax.sql.DataSource ; 45 46 import net.sf.jguard.core.CoreConstants; 47 import net.sf.jguard.ext.SecurityConstants; 48 49 50 55 public final class ConnectionFactory { 56 57 private static final Logger logger = Logger.getLogger(ConnectionFactory.class.getName()); 58 59 private Class clazz = null; 60 private String url = null; 61 private String login = null; 62 private String password = null; 63 64 68 private String key = null; 69 73 private boolean JNDI = false; 74 private Map connMap = new HashMap (); 77 private DataSource datasource = null; 78 79 83 public ConnectionFactory(Map opts){ 84 85 logger.finest(" ConnectionFactoryOptions="+opts); 86 87 String applicationName= (String )opts.get(CoreConstants.APPLICATION_NAME); 88 String applicationPassword = (String )opts.get(SecurityConstants.APPLICATION_PASSWORD); 89 if((String )opts.get(SecurityConstants.SECURED)!=null &&((String )opts.get(SecurityConstants.SECURED)).equals("true")){ 90 key = new StringBuffer (applicationName).append("|").append(applicationPassword).toString(); 91 }else{ 92 key = new String (applicationName); 93 } 94 95 String jndiRef = (String )opts.get("JNDI"); 96 if (jndiRef!= null){ 97 98 try { 99 Hashtable env = new Hashtable (opts); 100 InitialContext initCtx = new InitialContext (env); 101 Object object = (DataSource ) initCtx.lookup(jndiRef); 102 if (object instanceof DataSource ) { 103 datasource= (DataSource ) object; 104 }else{ 105 throw new IllegalArgumentException (" JNDI lookup "+jndiRef+" must return an object of type javax.sql.DataSource "); 106 } 107 connMap.put(key,datasource); 108 JNDI= true; 109 } catch (NamingException e) { 110 if (logger.isLoggable(Level.FINEST)) { 111 logger.finest("init() - datasource cannot be retrieved through JNDI " 112 + e.getMessage()); 113 } 114 }catch(Throwable t){ 115 System.out.println(t.getMessage()); 116 } 117 118 }else{ 119 120 String driver = (String )opts.get(SecurityConstants.DATABASE_DRIVER); 121 String url = (String )opts.get(SecurityConstants.DATABASE_DRIVER_URL); 122 String login = (String )opts.get(SecurityConstants.DATABASE_DRIVER_LOGIN); 123 String password = (String )opts.get(SecurityConstants.DATABASE_DRIVER_PASSWORD); 124 String value = driver+"|"+url+"|"+login+"|"+password; 125 connMap.put(key,value); 126 } 127 128 } 129 130 135 public final Connection getConnection() { 136 Connection conn = null; 137 138 if(JNDI){ 140 conn = getConnectionWithJNDI(); 141 }else{ 143 conn = getConnectionWithDriver(); 144 } 145 return conn; 146 147 148 } 149 150 151 158 public final Connection getConnection(String user,String password) { 159 160 Connection conn = null; 161 try { 162 if(url == null){ 163 logger.severe(" jdbc driver url is not defined "); 164 } 165 if(this.clazz == null){ 166 logger.severe(" jdbc driver class is not defined "); 167 } 168 conn = DriverManager.getConnection(this.url,user,password); 169 } catch (SQLException e) { 170 logger.severe(" connection canot be established url="+url+" user="+user+" password="+password); 171 } 172 return conn; 173 174 175 } 176 177 182 private Connection getConnectionWithJNDI() { 183 Connection conn = null; 184 try { 185 conn = datasource.getConnection(); 186 } catch (SQLException e) { 187 if (logger.isLoggable(Level.SEVERE)) { 188 logger.severe("getConnection() - connection through JNDI cannot be established " 189 + e.getMessage()); 190 } 191 } 192 return conn; 193 } 194 195 200 private Connection getConnectionWithDriver() { 201 Connection conn = null; 202 try { 203 if(clazz==null && url==null && login==null && password==null){ 204 String value = (String )connMap.get(key); 205 List strings = Arrays.asList(value.split("\\|")); 206 conn = getConnection(strings); 207 }else{ 208 conn = DriverManager.getConnection(url,login,password); 209 } 210 }catch (ClassNotFoundException e) { 211 logger.severe("getConnection() - ClassNotFoundException" + e.getMessage()); 212 logger.severe("getConnection(String, boolean)" + e); 213 }catch (SQLException e) { 214 logger.severe("getConnection() - SQLException "+ e.getMessage()); 215 logger.severe("getConnection() - SQLException state="+ e.getSQLState()); 216 logger.severe("getConnection() - SQLException error code="+ e.getErrorCode()); 217 logger.severe("getConnection() - SQLException error next exception="+ e.getNextException()); 218 logger.log(Level.SEVERE, "getConnection(String, boolean)", e); 219 } 220 return conn; 221 } 222 223 private Connection getConnection(List strings) throws ClassNotFoundException , SQLException { 224 Connection conn; 225 String driver = (String )strings.get(0); 226 url = (String )strings.get(1); 227 login = (String )strings.get(2); 228 229 switch(strings.size()){ 230 case 4: 231 password = (String )strings.get(3); 233 break; 234 case 3: 235 password =""; 239 break; 240 default: 241 throw new IllegalArgumentException (" wrong number of parameters to establish JDBC connection "+strings); 242 } 243 244 245 246 if (logger.isLoggable(Level.FINEST)) { 247 logger.finest("getConnection() - driver=" + driver); 248 logger.finest("getConnection() - url=" + url); 249 logger.finest("getConnection() - login=" + login); 250 logger.finest("getConnection() - password=" + password); 251 } 252 253 254 Class.forName(driver); 255 conn = DriverManager.getConnection(url,login,password); 256 return conn; 257 } 258 259 260 261 262 } 263 | Popular Tags |