1 21 22 package org.apache.derby.impl.db; 23 24 import org.apache.derby.iapi.error.PublicAPI; 25 26 import org.apache.derby.iapi.reference.Property; 27 import org.apache.derby.iapi.reference.SQLState; 28 import org.apache.derby.iapi.reference.EngineType; 29 import org.apache.derby.iapi.util.DoubleProperties; 30 import org.apache.derby.iapi.util.IdUtil; 31 import org.apache.derby.iapi.services.info.JVMInfo; 32 33 import org.apache.derby.iapi.services.property.PropertyUtil; 34 import org.apache.derby.iapi.services.loader.ClassFactory; 35 import org.apache.derby.iapi.services.loader.JarReader; 36 import org.apache.derby.iapi.services.context.ContextManager; 37 import org.apache.derby.iapi.services.context.ContextService; 38 import org.apache.derby.iapi.services.daemon.Serviceable; 39 import org.apache.derby.iapi.services.monitor.ModuleControl; 40 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 41 import org.apache.derby.iapi.services.monitor.Monitor; 42 import org.apache.derby.iapi.services.monitor.ModuleFactory; 43 import org.apache.derby.iapi.services.sanity.SanityManager; 44 import org.apache.derby.iapi.services.property.PersistentSet; 45 import org.apache.derby.iapi.db.Database; 46 import org.apache.derby.iapi.db.DatabaseContext; 47 import org.apache.derby.iapi.error.StandardException; 48 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 49 import org.apache.derby.iapi.types.DataValueFactory; 50 import org.apache.derby.iapi.sql.compile.OptimizerFactory; 51 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 52 import org.apache.derby.iapi.sql.conn.ConnectionUtil; 53 54 import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory; 55 56 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 57 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 58 import org.apache.derby.iapi.sql.dictionary.FileInfoDescriptor; 59 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 60 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor; 61 62 import org.apache.derby.iapi.sql.depend.DependencyManager; 63 import org.apache.derby.iapi.reference.SQLState; 64 import org.apache.derby.iapi.sql.LanguageFactory; 65 import org.apache.derby.iapi.sql.ResultSet; 66 import org.apache.derby.iapi.store.access.AccessFactory; 67 import org.apache.derby.iapi.store.access.FileResource; 68 import org.apache.derby.iapi.services.property.PropertyFactory; 69 import org.apache.derby.iapi.services.property.PropertySetCallback; 70 import org.apache.derby.iapi.store.access.TransactionController; 71 import org.apache.derby.iapi.jdbc.AuthenticationService; 72 import org.apache.derby.iapi.services.uuid.UUIDFactory; 73 import org.apache.derby.catalog.UUID; 74 75 import java.io.InputStream ; 76 import java.io.OutputStream ; 77 import java.io.Serializable ; 78 import java.io.File ; 79 import java.sql.Date ; 80 import java.sql.Timestamp ; 81 import java.sql.SQLException ; 82 import java.util.Properties ; 83 import java.util.Dictionary ; 84 import java.util.Enumeration ; 85 import java.util.Hashtable ; 86 import java.util.Locale ; 87 import java.lang.reflect.Method ; 88 import java.text.Collator ; 89 import java.text.RuleBasedCollator ; 90 import java.text.DateFormat ; 91 92 109 110 public class BasicDatabase implements ModuleControl, ModuleSupportable, PropertySetCallback, Database, JarReader 111 { 112 private boolean active; 113 private AuthenticationService authenticationService; 114 protected AccessFactory af; 115 protected PropertyFactory pf; 116 protected ClassFactory cfDB; protected LanguageConnectionFactory lcf; 118 protected LanguageFactory lf; 119 protected Object resourceAdapter; 122 private Locale databaseLocale; 123 private RuleBasedCollator ruleBasedCollator; 124 private int spaceInt; 125 private boolean spaceIntSet; 126 private DateFormat dateFormat; 127 private DateFormat timeFormat; 128 private DateFormat timestampFormat; 129 private UUID myUUID; 130 private boolean normalizeToUpper = true; 131 132 protected boolean lastToBoot; 134 137 138 public boolean canSupport(Properties startParams) { 139 140 return Monitor.isDesiredCreateType(startParams, org.apache.derby.iapi.reference.EngineType.NONE); 141 142 } 143 144 protected Properties allParams; 146 public void boot(boolean create, Properties startParams) 147 throws StandardException 148 { 149 ModuleFactory monitor = Monitor.getMonitor(); 150 if (create) 151 { 152 if (startParams.getProperty(Property.CREATE_WITH_NO_LOG) == null) 153 startParams.put(Property.CREATE_WITH_NO_LOG, "true"); 154 155 String localeID = startParams.getProperty(org.apache.derby.iapi.reference.Attribute.TERRITORY); 156 157 if (localeID == null) { 158 localeID = Locale.getDefault().toString(); 159 } 160 databaseLocale = monitor.setLocale(startParams, localeID); 161 162 } else { 163 databaseLocale = monitor.getLocale(this); 164 } 165 setLocale(databaseLocale); 166 167 normalizeToUpper = true; 168 169 170 bootValidation(create, startParams); 173 174 Monitor.bootServiceModule(create, this, 177 org.apache.derby.iapi.reference.ClassName.DataValueFactory, startParams); 178 179 bootStore(create, startParams); 180 181 myUUID = makeDatabaseID(create, startParams); 183 184 185 allParams = new DoubleProperties(getAllDatabaseProperties(), startParams); 186 187 if (pf != null) 188 pf.addPropertySetNotification(this); 189 190 bootClassFactory(create, allParams); 193 194 lcf = (LanguageConnectionFactory) Monitor.bootServiceModule(create, this, LanguageConnectionFactory.MODULE, allParams); 195 lf = (LanguageFactory) Monitor.bootServiceModule(create, this, LanguageFactory.MODULE, allParams); 196 197 bootResourceAdapter(create, startParams); 198 199 200 204 205 authenticationService = bootAuthenticationService(create, allParams); 210 if (SanityManager.DEBUG) { 211 SanityManager.ASSERT(authenticationService != null, 212 "Failed to set the Authentication service for the database"); 213 } 214 215 if (create && lastToBoot && 218 (startParams.getProperty(Property.CREATE_WITH_NO_LOG) != null)) 219 { 220 createFinished(); 221 } 222 223 active = true; 224 225 if (lastToBoot) 226 allParams = null; } 228 229 public void stop() { 230 active = false; 231 } 232 233 236 237 240 241 245 public int getEngineType() { return org.apache.derby.iapi.reference.EngineType.NONE; } 246 247 public boolean isReadOnly() 248 { 249 return af.isReadOnly(); 253 } 254 255 public LanguageConnectionContext setupConnection(ContextManager cm, String user, String drdaID, String dbname) 256 throws StandardException { 257 258 TransactionController tc = getConnectionTransaction(cm); 259 260 cm.setLocaleFinder(this); 261 pushDbContext(cm); 262 263 LanguageConnectionContext lctx = lcf.newLanguageConnectionContext(cm, tc, lf, this, user, drdaID, dbname); 266 267 pushClassFactoryContext(cm, lcf.getClassFactory()); 269 270 ExecutionFactory ef = lcf.getExecutionFactory(); 272 273 ef.newExecutionContext(cm); 274 lctx.initialize(true); 279 280 lctx.internalCommitNoSync( 283 TransactionController.RELEASE_LOCKS | 284 TransactionController.READONLY_TRANSACTION_INITIALIZATION); 285 286 return lctx; 287 288 } 289 290 public void pushDbContext(ContextManager cm) 291 { 292 295 DatabaseContext dc = new DatabaseContextImpl(cm, this); 296 } 297 298 public final AuthenticationService getAuthenticationService() { 299 300 303 if (SanityManager.DEBUG) 306 { 307 SanityManager.ASSERT(this.authenticationService != null, 308 "Unexpected - There is no valid authentication service for the database!"); 309 } 310 return this.authenticationService; 311 } 312 313 public void freeze() throws SQLException 314 { 315 try { 316 af.freeze(); 317 } catch (StandardException se) { 318 throw PublicAPI.wrapStandardException(se); 319 } 320 } 321 322 public void unfreeze() throws SQLException 323 { 324 try { 325 af.unfreeze(); 326 } catch (StandardException se) { 327 throw PublicAPI.wrapStandardException(se); 328 } 329 } 330 331 332 public void backup(String backupDir, boolean wait) 333 throws SQLException 334 { 335 try { 336 af.backup(backupDir, wait); 337 } catch (StandardException se) { 338 throw PublicAPI.wrapStandardException(se); 339 } 340 } 341 342 343 public void backupAndEnableLogArchiveMode(String backupDir, 344 boolean deleteOnlineArchivedLogFiles, 345 boolean wait) 346 throws SQLException 347 { 348 try { 349 af.backupAndEnableLogArchiveMode(backupDir, 350 deleteOnlineArchivedLogFiles, 351 wait); 352 } catch (StandardException se) { 353 throw PublicAPI.wrapStandardException(se); 354 } 355 } 356 357 358 public void disableLogArchiveMode(boolean deleteOnlineArchivedLogFiles) 359 throws SQLException 360 { 361 try{ 362 af.disableLogArchiveMode(deleteOnlineArchivedLogFiles); 363 }catch (StandardException se) { 364 throw PublicAPI.wrapStandardException(se); 365 } 366 } 367 368 369 public void checkpoint() throws SQLException 370 { 371 try { 372 af.checkpoint(); 373 } catch (StandardException se) { 374 throw PublicAPI.wrapStandardException(se); 375 } 376 } 377 378 379 public Locale getLocale() { 380 return databaseLocale; 381 } 382 383 384 387 public final UUID getId() { 388 return myUUID; 389 } 390 391 392 393 394 public Locale getCurrentLocale() throws StandardException { 395 if (databaseLocale != null) 396 return databaseLocale; 397 throw noLocale(); 398 } 399 400 401 public RuleBasedCollator getCollator() throws StandardException { 402 RuleBasedCollator retval = ruleBasedCollator; 403 404 if (retval == null) { 405 if (databaseLocale != null) { 406 retval = ruleBasedCollator = 407 (RuleBasedCollator ) Collator.getInstance(databaseLocale); 408 } else { 409 throw noLocale(); 410 } 411 } 412 413 return retval; 414 } 415 416 417 418 public DateFormat getDateFormat() throws StandardException { 419 if (databaseLocale != null) { 420 if (dateFormat == null) { 421 dateFormat = DateFormat.getDateInstance(DateFormat.LONG, 422 databaseLocale); 423 } 424 425 return dateFormat; 426 } 427 428 throw noLocale(); 429 } 430 431 432 public DateFormat getTimeFormat() throws StandardException { 433 if (databaseLocale != null) { 434 if (timeFormat == null) { 435 timeFormat = DateFormat.getTimeInstance(DateFormat.LONG, 436 databaseLocale); 437 } 438 439 return timeFormat; 440 } 441 442 throw noLocale(); 443 } 444 445 446 public DateFormat getTimestampFormat() throws StandardException { 447 if (databaseLocale != null) { 448 if (timestampFormat == null) { 449 timestampFormat = DateFormat.getDateTimeInstance( 450 DateFormat.LONG, 451 DateFormat.LONG, 452 databaseLocale); 453 } 454 455 return timestampFormat; 456 } 457 458 throw noLocale(); 459 } 460 461 private static StandardException noLocale() { 462 return StandardException.newException(SQLState.NO_LOCALE); 463 } 464 465 public void setLocale(Locale locale) { 466 databaseLocale = locale; 467 468 dateFormat = null; 469 timeFormat = null; 470 timestampFormat = null; 471 } 472 473 476 public boolean isActive() { 477 return active; 478 } 479 480 483 public BasicDatabase() { 484 lastToBoot = true; 485 } 486 487 488 protected UUID makeDatabaseID(boolean create, Properties startParams) 489 throws StandardException 490 { 491 492 TransactionController tc = af.getTransaction( 493 ContextService.getFactory().getCurrentContextManager()); 494 495 String upgradeID = null; 496 UUID databaseID; 497 498 if ((databaseID = (UUID) tc.getProperty(DataDictionary.DATABASE_ID)) == null) { 499 500 503 UUIDFactory uuidFactory = Monitor.getMonitor().getUUIDFactory(); 504 505 506 upgradeID = startParams.getProperty(DataDictionary.DATABASE_ID); 507 if (upgradeID == null ) 508 { 509 databaseID = uuidFactory.createUUID(); 511 } else { 512 databaseID = uuidFactory.recreateUUID(upgradeID); 513 } 514 515 tc.setProperty(DataDictionary.DATABASE_ID, databaseID, true); 516 } 517 518 if (upgradeID != null) 522 startParams.remove(DataDictionary.DATABASE_ID); 523 524 tc.commit(); 525 tc.destroy(); 526 527 return databaseID; 528 } 529 530 539 public void dropAllJDBCMetaDataSPSes() throws SQLException 540 { 541 try 542 { 543 LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); 544 545 DataDictionary dd = lcc.getDataDictionary(); 546 547 TransactionController tc = lcc.getTransactionExecute(); 548 549 553 dd.startWriting(lcc); 554 555 for (java.util.ListIterator li = dd.getAllSPSDescriptors().listIterator(); li.hasNext(); ) 556 { 557 SPSDescriptor spsd = (SPSDescriptor) li.next(); 558 559 562 if (spsd.getSchemaDescriptor().isSystemSchema() && !spsd.getSchemaDescriptor().isSYSIBM()) 563 { 564 dd.dropSPSDescriptor(spsd, tc); 565 dd.dropDependentsStoredDependencies(spsd.getUUID(), tc); 566 } 567 } 568 } catch (StandardException se) 569 { 570 throw PublicAPI.wrapStandardException(se); 571 } 572 } 573 574 579 public Object getResourceAdapter() 580 { 581 return resourceAdapter; 582 } 583 584 587 public void init(boolean dbOnly, Dictionary p) { 588 } 590 591 595 public boolean validate(String key, 596 Serializable value, 597 Dictionary p) 598 throws StandardException 599 { 600 if (key.equals(EngineType.PROPERTY)) 603 throw StandardException.newException(SQLState.PROPERTY_UNSUPPORTED_CHANGE, key, value); 604 605 if (!key.equals(Property.DATABASE_CLASSPATH)) return false; 607 608 String newClasspath = (String ) value; 609 String [][] dbcp = null; 611 if (newClasspath != null) { 612 dbcp = IdUtil.parseDbClassPath(newClasspath, normalizeToUpper); 615 } 616 617 DataDictionaryContext ddc = 618 (DataDictionaryContext)ContextService.getContext(DataDictionaryContext.CONTEXT_ID); 619 if (SanityManager.DEBUG) 620 if (ddc == null) SanityManager.THROWASSERT("Class path change with no connection"); 621 DataDictionary dd = ddc.getDataDictionary(); 622 623 if (dbcp != null) 626 { 627 for (int ix=0;ix<dbcp.length;ix++) 628 { 629 SchemaDescriptor sd = dd.getSchemaDescriptor(dbcp[ix][IdUtil.DBCP_SCHEMA_NAME], null, false); 630 631 FileInfoDescriptor fid = null; 632 if (sd != null) 633 fid = dd.getFileInfoDescriptor(sd,dbcp[ix][IdUtil.DBCP_SQL_JAR_NAME]); 634 635 if (fid == null){ 636 throw StandardException.newException(SQLState.LANG_DB_CLASS_PATH_HAS_MISSING_JAR , IdUtil.mkQualifiedName(dbcp[ix])); 637 } 638 } 639 } 640 641 return true; 642 } 643 647 public Serviceable apply(String key, Serializable value, Dictionary p) 648 throws StandardException 649 { 650 if (!key.equals(Property.DATABASE_CLASSPATH)) return null; 652 653 if (cfDB != null) { 656 DataDictionaryContext ddc = 657 (DataDictionaryContext)ContextService.getContext(DataDictionaryContext.CONTEXT_ID); 658 if (SanityManager.DEBUG) 659 if (ddc == null) SanityManager.THROWASSERT("Class path change with no connection"); 660 DataDictionary dd = ddc.getDataDictionary(); 661 662 dd.invalidateAllSPSPlans(); 665 666 String newClasspath = (String ) value; 667 if (newClasspath == null) newClasspath = ""; 668 cfDB.notifyModifyClasspath(newClasspath); 669 } 670 return null; 671 } 672 675 public Serializable map(String key,Serializable value,Dictionary p) 676 { 677 return null; 678 } 679 680 683 protected void createFinished() throws StandardException 684 { 685 af.createFinished(); 688 } 689 690 protected String getClasspath(Properties startParams) { 691 String cp = PropertyUtil.getPropertyFromSet(startParams, Property.DATABASE_CLASSPATH); 692 if (cp == null) 693 cp = PropertyUtil.getSystemProperty(Property.DATABASE_CLASSPATH, ""); 694 return cp; 695 } 696 697 698 protected void bootClassFactory(boolean create, 699 Properties startParams) 700 throws StandardException 701 { 702 String classpath = getClasspath(startParams); 703 704 boolean normalizeToUpper = true; 706 String [][] elements = 707 IdUtil.parseDbClassPath(classpath, normalizeToUpper); 708 709 startParams.put(Property.BOOT_DB_CLASSPATH, classpath); 710 cfDB = (ClassFactory) Monitor.bootServiceModule(create, this, 711 org.apache.derby.iapi.reference.Module.ClassFactory, startParams); 712 } 713 714 715 718 719 protected TransactionController getConnectionTransaction(ContextManager cm) 720 throws StandardException { 721 722 return af.getTransaction(cm); 724 } 725 726 protected AuthenticationService bootAuthenticationService(boolean create, Properties props) throws StandardException { 727 return (AuthenticationService) 728 Monitor.bootServiceModule(create, this, AuthenticationService.MODULE, props); 729 } 730 731 protected void bootValidation(boolean create, Properties startParams) 732 throws StandardException { 733 pf = (PropertyFactory) Monitor.bootServiceModule(create, this, 734 org.apache.derby.iapi.reference.Module.PropertyFactory, startParams); 735 } 736 737 protected void bootStore(boolean create, Properties startParams) 738 throws StandardException { 739 af = (AccessFactory) Monitor.bootServiceModule(create, this, AccessFactory.MODULE, startParams); 740 } 741 742 protected Properties getAllDatabaseProperties() 743 throws StandardException { 744 745 TransactionController tc = af.getTransaction( 746 ContextService.getFactory().getCurrentContextManager()); 747 Properties dbProps = tc.getProperties(); 748 tc.commit(); 749 tc.destroy(); 750 751 return dbProps; 752 } 753 754 protected void bootResourceAdapter(boolean create, Properties startParams) { 755 756 try 767 { 768 resourceAdapter = 769 Monitor.bootServiceModule(create, this, 770 org.apache.derby.iapi.reference.Module.ResourceAdapter, 771 allParams); 772 } 773 catch (StandardException mse) 774 { 775 } 777 } 778 779 protected void pushClassFactoryContext(ContextManager cm, ClassFactory cf) { 780 new StoreClassFactoryContext(cm, cf, af, this); 781 } 782 783 786 public Object readJarFile(String schemaName, String sqlName) 787 throws StandardException { 788 789 DataDictionaryContext ddc = 790 (DataDictionaryContext) ContextService.getContext(DataDictionaryContext.CONTEXT_ID); 791 DataDictionary dd = ddc.getDataDictionary(); 792 793 SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, null, true); 794 FileInfoDescriptor fid = dd.getFileInfoDescriptor(sd,sqlName); 795 if (fid == null) 796 throw StandardException.newException(SQLState.LANG_FILE_DOES_NOT_EXIST, sqlName,schemaName); 797 798 long generationId = fid.getGenerationId(); 799 800 FileResource fr = af.getTransaction(ddc.getContextManager()).getFileHandler(); 801 802 String externalName = org.apache.derby.impl.sql.execute.JarDDL.mkExternalName(schemaName, sqlName, fr.getSeparatorChar()); 803 804 Object f = fr.getAsFile(externalName, generationId); 805 if (f instanceof java.io.File ) 806 return f; 807 808 try { 809 return fr.getAsStream(externalName, generationId); 810 } catch (java.io.IOException ioe) { 811 throw StandardException.newException(SQLState.LANG_FILE_ERROR, ioe, ioe.toString()); 812 } 813 } 814 815 } 816 | Popular Tags |