1 21 22 package org.apache.derby.jdbc; 23 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 import java.sql.SQLException ; 27 import org.apache.derby.client.am.Configuration; 28 import org.apache.derby.client.am.SqlException; 29 import org.apache.derby.client.am.Utils; 30 import org.apache.derby.client.am.Version; 31 import org.apache.derby.client.am.ClientJDBCObjectFactory; 32 import org.apache.derby.client.am.ClientMessageId; 33 import org.apache.derby.client.net.ClientJDBCObjectFactoryImpl; 34 import org.apache.derby.shared.common.reference.Attribute; 35 import org.apache.derby.shared.common.reference.SQLState; 36 import org.apache.derby.shared.common.reference.MessageId; 37 38 39 public class ClientDriver implements java.sql.Driver { 40 private transient int traceFileSuffixIndex_ = 0; 41 42 private final static int DERBY_REMOTE_PROTOCOL = 1; 43 44 private static ClientJDBCObjectFactory factoryObject = null; 45 46 static private SQLException exceptionsOnLoadDriver__ = null; 47 static private ClientDriver registeredDriver__ = null; 49 50 static { 51 if (Configuration.exceptionsOnLoadResources != null) { 54 exceptionsOnLoadDriver__ = 55 Utils.accumulateSQLException( 56 Configuration.exceptionsOnLoadResources.getSQLException(), 57 exceptionsOnLoadDriver__); 58 } 59 try { 60 registeredDriver__ = new ClientDriver(); 61 java.sql.DriverManager.registerDriver(registeredDriver__); 62 } catch (java.sql.SQLException e) { 63 exceptionsOnLoadDriver__ = 65 new SqlException(null, 66 new ClientMessageId(SQLState.JDBC_DRIVER_REGISTER)).getSQLException(); 67 exceptionsOnLoadDriver__.setNextException(e); 68 } 69 } 70 71 public ClientDriver() { 72 } 73 74 public java.sql.Connection connect(String url, 75 java.util.Properties properties) throws java.sql.SQLException { 76 org.apache.derby.client.net.NetConnection conn; 77 try { 78 if (exceptionsOnLoadDriver__ != null) { 79 throw exceptionsOnLoadDriver__; 80 } 81 82 if (properties == null) { 83 properties = new java.util.Properties (); 84 } 85 86 java.util.StringTokenizer urlTokenizer = 87 new java.util.StringTokenizer (url, "/:= \t\n\r\f", true); 88 89 int protocol = tokenizeProtocol(url, urlTokenizer); 90 if (protocol == 0) { 91 return null; } 93 94 String slashOrNull = null; 95 if (protocol == DERBY_REMOTE_PROTOCOL) { 96 try { 97 slashOrNull = urlTokenizer.nextToken(":/"); 98 } catch (java.util.NoSuchElementException e) { 99 throw new SqlException(null, 101 new ClientMessageId(SQLState.MALFORMED_URL), 102 url, e); 103 } 104 } 105 String server = tokenizeServerName(urlTokenizer, url); int port = tokenizeOptionalPortNumber(urlTokenizer, url); if (port == 0) { 108 port = ClientDataSource.propertyDefault_portNumber; 109 } 110 111 String database = tokenizeDatabase(urlTokenizer, url); java.util.Properties augmentedProperties = tokenizeURLProperties(url, properties); 115 database = appendDatabaseAttributes(database,augmentedProperties); 116 117 int traceLevel; 118 try { 119 traceLevel = ClientDataSource.getTraceLevel(augmentedProperties); 120 } catch (java.lang.NumberFormatException e) { 121 throw new SqlException(null, 123 new ClientMessageId(SQLState.TRACELEVEL_FORMAT_INVALID), e); 124 } 125 126 org.apache.derby.client.am.LogWriter dncLogWriter = 131 ClientDataSource.computeDncLogWriterForNewConnection(java.sql.DriverManager.getLogWriter(), 132 ClientDataSource.getTraceDirectory(augmentedProperties), 133 ClientDataSource.getTraceFile(augmentedProperties), 134 ClientDataSource.getTraceFileAppend(augmentedProperties), 135 traceLevel, 136 "_driver", 137 traceFileSuffixIndex_++); 138 139 140 conn = (org.apache.derby.client.net.NetConnection)getFactory(). 141 newNetConnection((org.apache.derby.client.net.NetLogWriter) 142 dncLogWriter, 143 java.sql.DriverManager.getLoginTimeout(), 144 server, 145 port, 146 database, 147 augmentedProperties); 148 } catch(SqlException se) { 149 throw se.getSQLException(); 150 } 151 152 if(conn.isConnectionNull()) 153 return null; 154 155 return conn; 156 } 157 158 168 private String appendDatabaseAttributes(String database, Properties augmentedProperties) { 169 170 StringBuffer longDatabase = new StringBuffer (database); 171 for (Enumeration keys = augmentedProperties.keys(); keys.hasMoreElements() ;) 172 { 173 String key = (String ) keys.nextElement(); 174 if (key.equals(Attribute.USERNAME_ATTR) || 175 key.equals(Attribute.PASSWORD_ATTR)) 176 continue; 177 longDatabase.append(";" + key + "=" + augmentedProperties.getProperty(key)); 178 } 179 return longDatabase.toString(); 180 } 181 182 public boolean acceptsURL(String url) throws java.sql.SQLException { 183 try 184 { 185 java.util.StringTokenizer urlTokenizer = 186 new java.util.StringTokenizer (url, "/:=; \t\n\r\f", true); 187 int protocol = tokenizeProtocol(url, urlTokenizer); 188 return protocol != 0; 189 } 190 catch ( SqlException se ) 191 { 192 throw se.getSQLException(); 193 } 194 } 195 196 public java.sql.DriverPropertyInfo [] getPropertyInfo(String url, 197 java.util.Properties properties) throws java.sql.SQLException { 198 java.sql.DriverPropertyInfo driverPropertyInfo[] = new java.sql.DriverPropertyInfo [2]; 199 200 if (properties == null) { 203 properties = new java.util.Properties (); 204 } 205 206 driverPropertyInfo[0] = 207 new java.sql.DriverPropertyInfo (Attribute.USERNAME_ATTR, 208 properties.getProperty(Attribute.USERNAME_ATTR, ClientDataSource.propertyDefault_user)); 209 210 driverPropertyInfo[1] = 211 new java.sql.DriverPropertyInfo (Attribute.PASSWORD_ATTR, 212 properties.getProperty(Attribute.PASSWORD_ATTR)); 213 214 driverPropertyInfo[0].description = 215 SqlException.getMessageUtil().getTextMessage( 216 MessageId.CONN_USERNAME_DESCRIPTION); 217 driverPropertyInfo[1].description = 218 SqlException.getMessageUtil().getTextMessage( 219 MessageId.CONN_PASSWORD_DESCRIPTION); 220 221 driverPropertyInfo[0].required = true; 222 driverPropertyInfo[1].required = false; 224 return driverPropertyInfo; 225 } 226 227 public int getMajorVersion() { 228 return Version.getMajorVersion(); 229 } 230 231 public int getMinorVersion() { 232 return Version.getMinorVersion(); 233 } 234 235 public boolean jdbcCompliant() { 236 return Configuration.jdbcCompliant; 237 } 238 239 241 private static int tokenizeProtocol(String url, java.util.StringTokenizer urlTokenizer) throws SqlException { 246 if (url == null) { 248 return 0; 249 } 250 251 if (urlTokenizer == null) { 252 return 0; 253 } 254 255 try { 256 String jdbc = urlTokenizer.nextToken(":"); 257 if (!jdbc.equals("jdbc")) { 258 return 0; 259 } 260 if (!urlTokenizer.nextToken(":").equals(":")) { 261 return 0; } 263 String dbname = urlTokenizer.nextToken(":"); 264 int protocol = 0; 265 if (dbname.equals("derby") && (url.indexOf("derby://") != -1)) { 266 protocol = DERBY_REMOTE_PROTOCOL; 269 } else { 270 return 0; 271 } 272 273 if (!urlTokenizer.nextToken(":").equals(":")) { 274 return 0; } 276 277 return protocol; 278 } catch (java.util.NoSuchElementException e) { 279 return 0; 280 } 281 } 282 283 private static String tokenizeServerName(java.util.StringTokenizer urlTokenizer, 286 String url) throws SqlException { 287 try { 288 if (!urlTokenizer.nextToken("/").equals("/")) 289 { 291 throw new SqlException(null, 292 new ClientMessageId(SQLState.MALFORMED_URL), url); 293 } 294 return urlTokenizer.nextToken("/:"); 295 } catch (java.util.NoSuchElementException e) { 296 throw new SqlException(null, 298 new ClientMessageId(SQLState.MALFORMED_URL), url); 299 } 300 } 301 302 private static int tokenizeOptionalPortNumber(java.util.StringTokenizer urlTokenizer, 305 String url) throws SqlException { 306 try { 307 String firstToken = urlTokenizer.nextToken(":/"); 308 if (firstToken.equals(":")) { 309 String port = urlTokenizer.nextToken("/"); 310 if (!urlTokenizer.nextToken("/").equals("/")) { 311 throw new SqlException(null, 313 new ClientMessageId(SQLState.MALFORMED_URL), url); 314 } 315 return Integer.parseInt(port); 316 } else if (firstToken.equals("/")) { 317 return 0; 318 } else { 319 throw new SqlException(null, 321 new ClientMessageId(SQLState.MALFORMED_URL), url); 322 } 323 } catch (java.util.NoSuchElementException e) { 324 throw new SqlException(null, 326 new ClientMessageId(SQLState.MALFORMED_URL), url, e); 327 } 328 } 329 330 private static String tokenizeDatabase(java.util.StringTokenizer urlTokenizer, 332 String url) throws SqlException { 333 try { 334 String databaseName = urlTokenizer.nextToken("\t\n\r\f;"); 336 return databaseName; 337 } catch (java.util.NoSuchElementException e) { 338 throw new SqlException(null, 340 new ClientMessageId(SQLState.MALFORMED_URL), url, e); 341 } 342 } 343 344 private static java.util.Properties tokenizeURLProperties(String url, 345 java.util.Properties properties) 346 throws SqlException { 347 String attributeString = null; 348 int attributeIndex = -1; 349 350 if ((url != null) && 351 ((attributeIndex = url.indexOf(";")) != -1)) { 352 attributeString = url.substring(attributeIndex); 353 } 354 return ClientDataSource.tokenizeAttributes(attributeString, properties); 355 } 356 357 366 367 public static ClientJDBCObjectFactory getFactory() { 368 if(factoryObject!=null) 369 return factoryObject; 370 if(Configuration.supportsJDBC40()) { 371 factoryObject = createJDBC40FactoryImpl(); 372 } else { 373 factoryObject = createDefaultFactoryImpl(); 374 } 375 return factoryObject; 376 } 377 378 381 private static ClientJDBCObjectFactory createDefaultFactoryImpl() { 382 return new ClientJDBCObjectFactoryImpl(); 383 } 384 385 396 397 private static ClientJDBCObjectFactory createJDBC40FactoryImpl() { 398 final String factoryName = 399 "org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40"; 400 try { 401 return (ClientJDBCObjectFactory) 402 Class.forName(factoryName).newInstance(); 403 } catch (ClassNotFoundException cnfe) { 404 return createDefaultFactoryImpl(); 405 } catch (InstantiationException ie) { 406 return createDefaultFactoryImpl(); 407 } catch (IllegalAccessException iae) { 408 return createDefaultFactoryImpl(); 409 } 410 } 411 } 412 413 414 415 | Popular Tags |