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 java.io.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.internal.localization.*; 32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 33 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 34 35 43 public class DefaultConnector implements Connector { 44 protected String driverClassName; 45 protected String driverURLHeader; 46 protected String databaseURL; 47 48 53 public DefaultConnector() { 54 this("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:", ""); 55 } 56 57 61 public DefaultConnector(String driverClassName, String driverURLHeader, String databaseURL) { 62 this.initialize(driverClassName, driverURLHeader, databaseURL); 63 } 64 65 69 public Object clone() { 70 try { 71 return super.clone(); 72 } catch (Exception exception) { 73 throw new InternalError ("Clone failed"); 74 } 75 } 76 77 82 public Connection connect(Properties properties) throws DatabaseException { 83 this.loadDriver(); try { 85 return DriverManager.getConnection(this.getConnectionString(), properties); 86 } catch (SQLException exception) { 87 throw DatabaseException.sqlException(exception); 88 } 89 } 90 91 96 public String getConnectionString() { 97 return this.getDriverURLHeader() + this.getDatabaseURL(); 98 } 99 100 105 public String getConnectionDetails() { 106 return this.getConnectionString(); 107 } 108 109 115 public String getDatabaseURL() { 116 return databaseURL; 117 } 118 119 124 public String getDriverClassName() { 125 return driverClassName; 126 } 127 128 134 public String getDriverURLHeader() { 135 return driverURLHeader; 136 } 137 138 142 protected void initialize(String driverClassName, String driverURLHeader, String databaseURL) { 143 this.setDriverClassName(driverClassName); 144 this.setDriverURLHeader(driverURLHeader); 145 this.setDatabaseURL(databaseURL); 146 } 147 148 155 protected Class loadDriver() throws DatabaseException { 156 try { 159 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 160 try { 161 return (Class )AccessController.doPrivileged(new PrivilegedClassForName(this.getDriverClassName(), true, ConversionManager.getDefaultManager().getLoader())); 162 } catch (PrivilegedActionException exception) { 163 throw DatabaseException.configurationErrorClassNotFound(this.getDriverClassName()); } 164 } else { 165 return oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(this.getDriverClassName(), true, ConversionManager.getDefaultManager().getLoader()); 166 } 167 } catch (ClassNotFoundException exception) { 168 throw DatabaseException.configurationErrorClassNotFound(this.getDriverClassName()); 169 } 170 } 171 172 178 public void setDatabaseURL(String databaseURL) { 179 this.databaseURL = databaseURL; 180 } 181 182 187 public void setDriverClassName(String driverClassName) { 188 this.driverClassName = driverClassName; 189 } 190 191 197 public void setDriverURLHeader(String driverURLHeader) { 198 this.driverURLHeader = driverURLHeader; 199 } 200 201 205 public String toString() { 206 return oracle.toplink.essentials.internal.helper.Helper.getShortClassName(getClass()) + "(" + getConnectionString() + ")"; 207 } 208 209 213 public void toString(PrintWriter writer) { 214 writer.println(ToStringLocalization.buildMessage("datasource_URL", (Object [])null) + "=> \"" + this.getConnectionString() + "\""); 215 } 216 } 217 | Popular Tags |