PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/refapp-core/src/main/java/com/atlassian/plugin/refimpl/RefappLauncher.java

https://bitbucket.org/atlassian/atlassian-refapp
Java | 108 lines | 68 code | 13 blank | 27 comment | 0 complexity | ce7a977006c276dac35f46260c6e508f MD5 | raw file
  1. package com.atlassian.plugin.refimpl;
  2. import com.atlassian.plugin.PluginAccessor;
  3. import com.atlassian.plugin.SplitStartupPluginSystemLifecycle;
  4. import com.atlassian.plugin.main.AtlassianPlugins;
  5. import com.atlassian.plugin.refimpl.db.SchemaCreator;
  6. import com.atlassian.scheduler.SchedulerServiceException;
  7. import com.atlassian.scheduler.core.SchedulerServiceController;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.context.annotation.Bean;
  11. import javax.annotation.ParametersAreNonnullByDefault;
  12. import java.util.Optional;
  13. import static java.util.Objects.requireNonNull;
  14. /**
  15. * Launches Refapp.
  16. *
  17. * @since 5.4 renamed from ContainerManager and had all its object creation code moved to Spring Java configuration.
  18. */
  19. @ParametersAreNonnullByDefault
  20. public class RefappLauncher {
  21. private static final Logger LOGGER = LoggerFactory.getLogger(RefappLauncher.class);
  22. private final AtlassianPlugins pluginSystem;
  23. private final PluginAccessor pluginAccessor;
  24. private final SchedulerServiceController schedulerServiceController;
  25. private final SchemaCreator schemaCreator;
  26. private final SplitStartupPluginSystemLifecycle pluginSystemLifecycle;
  27. /**
  28. * Constructor.
  29. *
  30. * @param pluginSystem the plugin system
  31. * @param pluginAccessor the plugin accessor
  32. * @param schedulerServiceController the scheduler service controller
  33. * @param schemaCreator the schema creator
  34. * @param pluginSystemLifecycle the plugin lifecycle
  35. */
  36. public RefappLauncher(final AtlassianPlugins pluginSystem,
  37. final PluginAccessor pluginAccessor,
  38. final SchedulerServiceController schedulerServiceController,
  39. final SchemaCreator schemaCreator,
  40. final SplitStartupPluginSystemLifecycle pluginSystemLifecycle) {
  41. this.pluginAccessor = requireNonNull(pluginAccessor);
  42. this.pluginSystem = requireNonNull(pluginSystem);
  43. this.pluginSystemLifecycle = requireNonNull(pluginSystemLifecycle);
  44. this.schedulerServiceController = requireNonNull(schedulerServiceController);
  45. this.schemaCreator = requireNonNull(schemaCreator);
  46. }
  47. /**
  48. * Starts Refapp.
  49. *
  50. * @see #shutdown()
  51. */
  52. public void launch() {
  53. loadJavaxXmlTransformFactoryFinderClassBeforePluginsDo();
  54. schemaCreator.createSchema();
  55. startScheduler();
  56. startPluginSystem();
  57. logStartupMessage();
  58. }
  59. // See https://ecosystem.atlassian.net/browse/REFAPP-353
  60. private static void loadJavaxXmlTransformFactoryFinderClassBeforePluginsDo() {
  61. try {
  62. Class.forName("javax.xml.transform.FactoryFinder");
  63. } catch (final ClassNotFoundException e) {
  64. throw new IllegalStateException(e);
  65. }
  66. }
  67. private void startScheduler() {
  68. try {
  69. schedulerServiceController.start();
  70. } catch (final SchedulerServiceException e) {
  71. throw new IllegalStateException("Failed to start scheduler", e);
  72. }
  73. }
  74. private void startPluginSystem() {
  75. pluginSystemLifecycle.init();
  76. pluginAccessor.getPlugins(); // presumably called for its side effect(s)
  77. }
  78. private static void logStartupMessage() {
  79. Optional.ofNullable(System.getProperty("baseurl.display"))
  80. .ifPresent(baseUrl -> LOGGER.info("\n\n*** Refapp started on {}\n\n", baseUrl));
  81. }
  82. /**
  83. * Shuts down this instance. "shutdown" is a special method that Spring calls on @Bean instances by default.
  84. *
  85. * @see Bean#destroyMethod() for the list of these special method names
  86. */
  87. @SuppressWarnings("unused") // called by Spring, see Javadoc
  88. public void shutdown() {
  89. // Shutdown plugins before stopping the scheduler, because many plugins attempt to unregister
  90. // jobs and/or job runners when they stop
  91. pluginSystemLifecycle.shutdown();
  92. pluginSystem.destroy();
  93. schedulerServiceController.shutdown();
  94. }
  95. }