1 2 22 23 package org.apache.derby.jdbc; 24 25 import org.apache.derby.iapi.reference.Attribute; 26 import org.apache.derby.iapi.reference.SQLState; 27 import org.apache.derby.iapi.reference.MessageId; 28 import org.apache.derby.iapi.services.io.FormatableProperties; 29 30 import org.apache.derby.iapi.jdbc.ConnectionContext; 31 32 import org.apache.derby.iapi.services.monitor.ModuleControl; 33 import org.apache.derby.iapi.services.monitor.Monitor; 34 import org.apache.derby.iapi.services.context.ContextService; 35 import org.apache.derby.iapi.services.context.ContextManager; 36 import org.apache.derby.iapi.services.sanity.SanityManager; 37 import org.apache.derby.iapi.error.StandardException; 38 import org.apache.derby.iapi.services.i18n.MessageService; 39 40 import org.apache.derby.iapi.sql.ResultSet; 41 42 import org.apache.derby.iapi.jdbc.AuthenticationService; 43 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 44 45 import org.apache.derby.impl.jdbc.*; 46 47 import java.sql.Connection ; 48 import java.sql.DatabaseMetaData ; 49 import java.sql.SQLException ; 50 51 import java.util.Properties ; 52 import java.util.StringTokenizer ; 53 54 55 59 60 public abstract class InternalDriver implements ModuleControl { 61 62 private static final Object syncMe = new Object (); 63 private static InternalDriver activeDriver; 64 65 protected boolean active; 66 private ContextService contextServiceFactory; 67 private AuthenticationService authenticationService; 68 69 public static final InternalDriver activeDriver() 70 { 71 return activeDriver; 72 } 73 74 public InternalDriver() { 75 contextServiceFactory = ContextService.getFactory(); 76 } 77 78 81 82 public void boot(boolean create, Properties properties) throws StandardException { 83 84 synchronized (InternalDriver.syncMe) 85 { 86 InternalDriver.activeDriver = this; 87 } 88 89 active = true; 90 } 91 92 public void stop() { 93 94 synchronized (InternalDriver.syncMe) 95 { 96 InternalDriver.activeDriver = null; 97 } 98 99 active = false; 100 101 contextServiceFactory = null; 102 } 103 104 107 public boolean acceptsURL(String url) { 108 return active && embeddedDriverAcceptsURL( url ); 109 } 110 111 116 public static boolean embeddedDriverAcceptsURL(String url) { 117 return 118 !url.startsWith(Attribute.JCC_PROTOCOL) && !url.startsWith(Attribute.DNC_PROTOCOL) && 120 (url.startsWith(Attribute.PROTOCOL) || url.equals(Attribute.SQLJ_NESTED)); 121 122 } 123 124 public Connection connect(String url, Properties info) 125 throws SQLException 126 { 127 if (!acceptsURL(url)) { return null; } 128 129 133 if (EmbedConnection.memoryState.isLowMemory()) 134 { 135 throw EmbedConnection.NO_MEM; 136 } 137 138 144 boolean current = url.equals(Attribute.SQLJ_NESTED); 145 146 149 if (current) { 150 151 ConnectionContext connContext = getConnectionContext(); 152 153 if (connContext != null) { 154 155 return connContext.getNestedConnection(false); 156 157 } 158 return null; 161 } 162 163 FormatableProperties finfo = null; 166 167 try { 168 169 finfo = getAttributes(url, info); 170 info = null; 172 175 boolean shutdown = Boolean.valueOf(finfo.getProperty(Attribute.SHUTDOWN_ATTR)).booleanValue(); 176 177 if (shutdown) { 178 179 if (InternalDriver.getDatabaseName(url, finfo).length() == 0) { 186 if (this.getAuthenticationService() == null) 192 throw Util.generateCsSQLException( 193 SQLState.LOGIN_FAILED, 194 MessageService.getTextMessage(MessageId.AUTH_NO_SERVICE_FOR_SYSTEM)); 195 196 197 if (!this.getAuthenticationService().authenticate((String ) null, finfo)) { 198 199 throw Util.generateCsSQLException( 201 SQLState.LOGIN_FAILED, MessageService.getTextMessage(MessageId.AUTH_INVALID)); 202 } 203 204 Monitor.getMonitor().shutdown(); 205 throw Util.generateCsSQLException( 206 SQLState.CLOUDSCAPE_SYSTEM_SHUTDOWN); 207 } 208 } 209 210 EmbedConnection conn = getNewEmbedConnection(url, finfo); 211 212 if (conn.isClosed()) { 215 return null; 216 } 217 218 return conn; 219 } 220 catch (OutOfMemoryError noMemory) 221 { 222 EmbedConnection.memoryState.setLowMemory(); 223 throw EmbedConnection.NO_MEM; 224 } 225 finally { 226 if (finfo != null) 228 finfo.clearDefaults(); 229 } 230 } 231 232 public int getMajorVersion() { 233 return Monitor.getMonitor().getEngineVersion().getMajorVersion(); 234 } 235 236 public int getMinorVersion() { 237 return Monitor.getMonitor().getEngineVersion().getMinorVersion(); 238 } 239 240 public boolean jdbcCompliant() { 241 return true; 242 } 243 244 247 248 261 protected FormatableProperties getAttributes(String url, Properties info) 262 throws SQLException { 263 264 FormatableProperties finfo = new FormatableProperties(info); 267 info = null; 269 270 StringTokenizer st = new StringTokenizer (url, ";"); 271 st.nextToken(); 273 while (st.hasMoreTokens()) { 274 275 String v = st.nextToken(); 276 277 int eqPos = v.indexOf('='); 278 if (eqPos == -1) 279 throw Util.generateCsSQLException( 280 SQLState.MALFORMED_URL, url); 281 282 285 finfo.put((v.substring(0, eqPos)).trim(), 286 (v.substring(eqPos + 1)).trim() 287 ); 288 } 289 290 295 296 checkBoolean(finfo, Attribute.DATA_ENCRYPTION); 297 checkBoolean(finfo, Attribute.CREATE_ATTR); 298 checkBoolean(finfo, Attribute.SHUTDOWN_ATTR); 299 checkBoolean(finfo, Attribute.UPGRADE_ATTR); 300 301 return finfo; 302 } 303 304 private static void checkBoolean(Properties set, String attribute) throws SQLException 305 { 306 final String [] booleanChoices = {"true", "false"}; 307 checkEnumeration( set, attribute, booleanChoices); 308 } 309 310 311 private static void checkEnumeration(Properties set, String attribute, String [] choices) throws SQLException 312 { 313 String value = set.getProperty(attribute); 314 if (value == null) 315 return; 316 317 for( int i = 0; i < choices.length; i++) 318 { 319 if( value.toUpperCase(java.util.Locale.ENGLISH).equals( choices[i].toUpperCase(java.util.Locale.ENGLISH))) 320 return; 321 } 322 323 String choicesStr = "{"; 326 for( int i = 0; i < choices.length; i++) 327 { 328 if( i > 0) 329 choicesStr += "|"; 330 choicesStr += choices[i]; 331 } 332 333 throw Util.generateCsSQLException( 334 SQLState.INVALID_ATTRIBUTE, attribute, value, choicesStr + "}"); 335 } 336 337 338 353 public static String getDatabaseName(String url, Properties info) { 354 355 if (url.equals(Attribute.SQLJ_NESTED)) 356 { 357 return ""; 358 } 359 360 int attributeStart = url.indexOf(';'); 362 String dbname; 363 if (attributeStart == -1) 364 dbname = url.substring(Attribute.PROTOCOL.length()); 365 else 366 dbname = url.substring(Attribute.PROTOCOL.length(), attributeStart); 367 368 375 if (dbname.length() == 0) { 376 if (info != null) 377 dbname = info.getProperty(Attribute.DBNAME_ATTR, dbname); 378 } 379 dbname = dbname.trim(); 382 383 return dbname; 384 } 385 386 public final ContextService getContextServiceFactory() { 387 return contextServiceFactory; 388 } 389 390 public AuthenticationService getAuthenticationService() { 392 if (this.authenticationService == null) { 398 this.authenticationService = (AuthenticationService) 399 Monitor.findService(AuthenticationService.MODULE, 400 "authentication" 401 ); 402 } 403 404 if (SanityManager.DEBUG) 407 { 408 SanityManager.ASSERT(this.authenticationService != null, 409 "Unexpected - There is no valid authentication service!"); 410 } 411 return this.authenticationService; 412 } 413 414 418 protected abstract EmbedConnection getNewEmbedConnection(String url, Properties info) 419 throws SQLException ; 420 421 422 private ConnectionContext getConnectionContext() { 423 424 428 ContextManager cm = getCurrentContextManager(); 429 430 ConnectionContext localCC = null; 431 432 436 if (cm != null) { 437 localCC = (ConnectionContext) 438 (cm.getContext(ConnectionContext.CONTEXT_ID)); 439 } 440 441 return localCC; 442 } 443 444 private ContextManager getCurrentContextManager() { 445 return getContextServiceFactory().getCurrentContextManager(); 446 } 447 448 449 452 public boolean isActive() { 453 return active; 454 } 455 456 464 public abstract Connection getNewNestedConnection(EmbedConnection conn); 465 466 470 471 public java.sql.Statement newEmbedStatement( 472 EmbedConnection conn, 473 boolean forMetaData, 474 int resultSetType, 475 int resultSetConcurrency, 476 int resultSetHoldability) 477 { 478 return new EmbedStatement(conn, forMetaData, resultSetType, resultSetConcurrency, 479 resultSetHoldability); 480 } 481 484 public abstract java.sql.PreparedStatement newEmbedPreparedStatement( 485 EmbedConnection conn, 486 String stmt, 487 boolean forMetaData, 488 int resultSetType, 489 int resultSetConcurrency, 490 int resultSetHoldability, 491 int autoGeneratedKeys, 492 int[] columnIndexes, 493 String [] columnNames) 494 throws SQLException ; 495 496 499 public abstract java.sql.CallableStatement newEmbedCallableStatement( 500 EmbedConnection conn, 501 String stmt, 502 int resultSetType, 503 int resultSetConcurrency, 504 int resultSetHoldability) 505 throws SQLException ; 506 507 511 public DatabaseMetaData newEmbedDatabaseMetaData(EmbedConnection conn, 512 String dbname) throws SQLException { 513 return new EmbedDatabaseMetaData(conn,dbname); 514 } 515 516 526 public abstract EmbedResultSet 527 newEmbedResultSet(EmbedConnection conn, ResultSet results, boolean forMetaData, EmbedStatement statement, boolean isAtomic) throws SQLException ; 528 529 535 public EmbedResultSetMetaData newEmbedResultSetMetaData 536 (ResultColumnDescriptor[] columnInfo) { 537 return new EmbedResultSetMetaData(columnInfo); 538 } 539 } 540 541 542 543 | Popular Tags |