1 21 package oracle.toplink.essentials.sessions; 23 24 import java.util.*; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 import java.sql.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 30 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass; 31 32 40 public class DirectConnector extends DefaultConnector { 41 42 43 protected Driver cachedInstance; 44 45 51 public DirectConnector() { 52 super(); 53 } 54 55 59 public DirectConnector(String driverClassName, String driverURLHeader, String databaseURL) { 60 super(driverClassName, driverURLHeader, databaseURL); 61 } 62 63 68 public Connection connect(Properties properties) throws DatabaseException { 69 try { 70 return this.instantiateDriver(this.loadDriver()).connect(this.getConnectionString(), properties); 71 } catch (SQLException exception) { 72 throw DatabaseException.sqlException(exception); 73 } 74 } 75 76 81 protected Driver instantiateDriver(Class driverClass) throws DatabaseException { 82 if (cachedInstance != null) { 83 return cachedInstance; 84 } 85 86 try { 87 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 88 try { 89 cachedInstance = (Driver)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(driverClass)); 90 } catch (PrivilegedActionException exception) { 91 Exception throwableException = exception.getException(); 92 if (throwableException instanceof InstantiationException ) { 93 throw DatabaseException.configurationErrorNewInstanceInstantiationException((InstantiationException )throwableException, driverClass); 94 } else { 95 throw DatabaseException.configurationErrorNewInstanceIllegalAccessException((IllegalAccessException )throwableException, driverClass); 96 } 97 } 98 } else { 99 cachedInstance = (Driver)PrivilegedAccessHelper.newInstanceFromClass(driverClass); 100 } 101 return cachedInstance; 102 } catch (InstantiationException ie) { 103 throw DatabaseException.configurationErrorNewInstanceInstantiationException(ie, driverClass); 104 } catch (IllegalAccessException iae) { 105 throw DatabaseException.configurationErrorNewInstanceIllegalAccessException(iae, driverClass); 106 } 107 } 108 } 109 | Popular Tags |