1 21 22 package org.apache.derby.iapi.services.monitor; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.iapi.services.i18n.MessageService; 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 28 import org.apache.derby.iapi.services.info.ProductGenusNames; 29 import org.apache.derby.iapi.services.info.ProductVersionHolder; 30 import org.apache.derby.iapi.reference.EngineType; 31 import org.apache.derby.iapi.reference.Attribute; 32 import org.apache.derby.iapi.services.property.PropertyUtil; 33 34 import org.apache.derby.iapi.services.loader.InstanceGetter; 35 36 import org.apache.derby.iapi.reference.SQLState; 37 import org.apache.derby.iapi.reference.Property; 38 39 import java.util.Properties ; 40 import java.util.Locale ; 41 import java.io.PrintStream ; 42 43 246 public class Monitor { 247 248 public static final String SERVICE_TYPE_DIRECTORY = "serviceDirectory"; 249 250 public static final Object syncMe = new Object (); 251 252 255 public final static String 256 NEW_INSTANCE_FROM_ID_TRACE_DEBUG_FLAG = SanityManager.DEBUG ? "MonitorNewInstanceFromId" : null; 257 258 public static final String DEBUG_TRUE = SanityManager.DEBUG ? "derby.debug.true" : null; 259 public static final String DEBUG_FALSE = SanityManager.DEBUG ? "derby.debug.false" : null; 260 261 262 private static ModuleFactory monitor; 263 private static boolean active; 264 265 public Monitor() { 266 } 267 268 286 287 public static void startMonitor(Properties bootProperties, PrintStream logging) { 288 289 new org.apache.derby.impl.services.monitor.FileMonitor(bootProperties, logging); 290 } 291 295 public static boolean setMonitor(ModuleFactory theMonitor) { 296 297 synchronized (syncMe) { 298 if (active) 299 return false; 300 301 monitor = theMonitor; 302 active = true; 303 return true; 304 } 305 } 306 307 public static void clearMonitor() { 308 synchronized (syncMe) { 312 active = false; 313 } 314 } 315 316 319 public static ModuleFactory getMonitor() { 320 return monitor; 321 } 322 public static ModuleFactory getMonitorLite() { 323 synchronized (syncMe) { 324 if (active && monitor != null) 325 return monitor; 326 } 327 328 333 return new org.apache.derby.impl.services.monitor.FileMonitor(); 334 } 335 336 public static HeaderPrintWriter getStream() { 337 return monitor.getSystemStreams().stream(); 338 } 339 340 343 public static String getServiceName(Object serviceModule) { 344 return monitor.getServiceName(serviceModule); 345 } 346 347 348 363 public static Object startSystemModule(String factoryInterface) 364 throws StandardException { 365 366 Object module = monitor.startModule(false, (Object ) null, factoryInterface, (String ) null, (Properties ) null); 367 368 if (SanityManager.DEBUG) { 369 SanityManager.ASSERT(module != null, "module is null - " + factoryInterface); 370 } 371 372 return module; 373 } 374 375 380 public static Object findSystemModule(String factoryInterface) throws StandardException 381 { 382 Object module = getMonitor().findModule((Object ) null, 383 factoryInterface, (String ) null); 384 if (module == null) 385 throw Monitor.missingImplementation(factoryInterface); 386 387 return module; 388 } 389 390 public static Object getSystemModule(String factoryInterface) 391 { 392 Object module = getMonitor().findModule((Object ) null, 393 factoryInterface, (String ) null); 394 return module; 395 } 396 397 415 public static Object bootServiceModule(boolean create, Object serviceModule, 416 String factoryInterface, Properties properties) 417 throws StandardException { 418 419 Object module = monitor.startModule(create, serviceModule, factoryInterface, 420 (String ) null, properties); 421 422 if (SanityManager.DEBUG) { 423 SanityManager.ASSERT(module != null, "module is null - " + factoryInterface); 424 } 425 426 return module; 427 } 428 429 447 public static Object bootServiceModule(boolean create, Object serviceModule, 448 String factoryInterface, String identifier, Properties properties) 449 throws StandardException { 450 451 Object module = monitor.startModule(create, serviceModule, factoryInterface, identifier, properties); 452 453 if (SanityManager.DEBUG) { 454 SanityManager.ASSERT(module != null, "module is null - " + factoryInterface); 455 } 456 457 return module; 458 } 459 460 468 public static Object findServiceModule(Object serviceModule, String factoryInterface) 469 throws StandardException { 470 Object module = getMonitor().findModule(serviceModule, factoryInterface, (String ) null); 471 if (module == null) 472 throw Monitor.missingImplementation(factoryInterface); 473 return module; 474 } 475 public static Object getServiceModule(Object serviceModule, String factoryInterface) 476 { 477 Object module = getMonitor().findModule(serviceModule, factoryInterface, (String ) null); 478 return module; 479 } 480 481 489 493 494 500 public static Object findService(String factoryInterface, String serviceName) { 501 return monitor.findService(factoryInterface, serviceName); 502 } 503 504 533 534 public static boolean startPersistentService(String serviceName, 535 Properties properties) 536 throws StandardException { 537 538 if (SanityManager.DEBUG) { 539 SanityManager.ASSERT(serviceName != null, "serviceName is null"); 540 } 541 542 return monitor.startPersistentService(serviceName, properties); 543 } 544 545 555 public static Object startNonPersistentService(String factoryInterface, String serviceName, Properties properties) 556 throws StandardException { 557 558 if (SanityManager.DEBUG) { 559 SanityManager.ASSERT(factoryInterface != null, "serviceName is null"); 560 SanityManager.ASSERT(serviceName != null, "serviceName is null"); 561 } 562 563 return monitor.startNonPersistentService(factoryInterface, serviceName, properties); 564 } 565 566 580 public static Object createPersistentService(String factoryInterface, String serviceName, Properties properties) 581 throws StandardException { 582 583 if (SanityManager.DEBUG) { 584 SanityManager.ASSERT(factoryInterface != null, "serviceName is null"); 585 SanityManager.ASSERT(serviceName != null, "serviceName is null"); 586 } 587 588 return monitor.createPersistentService(factoryInterface, serviceName, properties); 589 } 590 public static void removePersistentService(String name) 591 throws StandardException 592 { 593 monitor.removePersistentService(name); 594 } 595 596 608 public static InstanceGetter classFromIdentifier(int identifier) 609 throws StandardException { 610 return monitor.classFromIdentifier(identifier); 611 } 612 613 625 public static Object newInstanceFromIdentifier(int identifier) 626 throws StandardException { 627 return monitor.newInstanceFromIdentifier(identifier); 628 } 629 630 631 634 641 public static StandardException missingProductVersion(String productGenusName) 642 { 643 return StandardException.newException(SQLState.MISSING_PRODUCT_VERSION, productGenusName); 644 } 645 646 654 public static StandardException missingImplementation(String implementation) 655 { 656 return StandardException.newException(SQLState.SERVICE_MISSING_IMPLEMENTATION, implementation); 657 } 658 659 667 public static StandardException exceptionStartingModule(Throwable t) 668 { 669 return StandardException.newException(SQLState.SERVICE_STARTUP_EXCEPTION, t); 670 } 671 672 public static void logMessage(String messageText) { 673 getStream().println(messageText); 674 } 675 676 public static void logTextMessage(String messageID) { 677 getStream().println(MessageService.getTextMessage(messageID)); 678 } 679 public static void logTextMessage(String messageID, Object a1) { 680 getStream().println(MessageService.getTextMessage(messageID, a1)); 681 } 682 public static void logTextMessage(String messageID, Object a1, Object a2) { 683 getStream().println(MessageService.getTextMessage(messageID, a1, a2)); 684 } 685 public static void logTextMessage(String messageID, Object a1, Object a2, Object a3) { 686 getStream().println(MessageService.getTextMessage(messageID, a1, a2, a3)); 687 } 688 public static void logTextMessage(String messageID, Object a1, Object a2, Object a3, Object a4) { 689 getStream().println(MessageService.getTextMessage(messageID, a1, a2, a3, a4)); 690 } 691 692 696 public static Locale getLocaleFromString(String localeDescription) 697 throws StandardException { 698 return monitor.getLocaleFromString(localeDescription); 699 } 700 701 702 706 public static boolean isFullUpgrade(Properties startParams, String oldVersionInfo) throws StandardException { 707 708 boolean fullUpgrade = Boolean.valueOf(startParams.getProperty(org.apache.derby.iapi.reference.Attribute.UPGRADE_ATTR)).booleanValue(); 709 710 ProductVersionHolder engineVersion = Monitor.getMonitor().getEngineVersion(); 711 712 if (engineVersion.isBeta() || engineVersion.isAlpha()) { 713 714 if (!PropertyUtil.getSystemBoolean(Property.ALPHA_BETA_ALLOW_UPGRADE)) 715 { 716 throw StandardException.newException(SQLState.UPGRADE_UNSUPPORTED, 718 oldVersionInfo, engineVersion.getSimpleVersionString()); 719 } 720 } 721 722 return fullUpgrade; 723 } 724 725 734 public static boolean isDesiredType(Properties startParams, int desiredProperty ) 735 { 736 boolean retval = false; 737 int engineType = EngineType.NONE; 738 739 if ( startParams != null ) 740 { 741 engineType = Monitor.getEngineType( startParams ); 742 } 743 744 return (engineType & desiredProperty) != 0; 745 } 746 public static boolean isDesiredType(int engineType, int desiredProperty) { 747 return (engineType & desiredProperty) != 0; 748 } 749 750 756 757 static public int getEngineType(Properties startParams) 758 { 759 if ( startParams != null ) 760 { 761 String etp = startParams.getProperty(EngineType.PROPERTY); 762 763 int engineType = etp == null ? EngineType.STANDALONE_DB : Integer.parseInt(etp.trim()); 764 765 return engineType; 766 } 767 768 return EngineType.STANDALONE_DB; 769 } 770 771 776 public static boolean isDesiredCreateType(Properties p, int type) 777 { 778 boolean plainCreate = Boolean.valueOf(p.getProperty(Attribute.CREATE_ATTR)).booleanValue(); 779 780 if (plainCreate) { 781 return (type & EngineType.NONE) != 0; 782 } 783 784 return isDesiredType(p, type); 786 } 787 } 788 | Popular Tags |