@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { onApplicationEnvironmentPreparedEvent( (ApplicationEnvironmentPreparedEvent) event); } else if (event instanceof ApplicationPreparedEvent) { onApplicationPreparedEvent((ApplicationPreparedEvent) event); } else if (event instanceof ContextClosedEvent && ((ContextClosedEvent) event) .getApplicationContext().getParent() == null) { onContextClosedEvent(); } }
@Test public void eventsOrder() { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>(); class ApplicationRunningEventListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { events.add((event)); } } application.addListeners(new ApplicationRunningEventListener()); this.context = application.run(); assertThat(events).hasSize(5); assertThat(events.get(0)).isInstanceOf(ApplicationStartedEvent.class); assertThat(events.get(1)).isInstanceOf(ApplicationEnvironmentPreparedEvent.class); assertThat(events.get(2)).isInstanceOf(ApplicationPreparedEvent.class); assertThat(events.get(3)).isInstanceOf(ContextRefreshedEvent.class); assertThat(events.get(4)).isInstanceOf(ApplicationReadyEvent.class); }
@Test public void closingChildContextDoesNotCleanUpLoggingSystem() { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName()); this.initializer.onApplicationEvent( new ApplicationStartedEvent(this.springApplication, new String[0])); TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils .getField(this.initializer, "loggingSystem"); assertThat(loggingSystem.cleanedUp).isFalse(); GenericApplicationContext childContext = new GenericApplicationContext(); childContext.setParent(this.context); this.initializer.onApplicationEvent(new ContextClosedEvent(childContext)); assertThat(loggingSystem.cleanedUp).isFalse(); this.initializer.onApplicationEvent(new ContextClosedEvent(this.context)); assertThat(loggingSystem.cleanedUp).isTrue(); childContext.close(); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } if (event instanceof ApplicationPreparedEvent) { Restarter.getInstance() .prepare(((ApplicationPreparedEvent) event).getApplicationContext()); } if (event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) { Restarter.getInstance().finish(); if (event instanceof ApplicationFailedEvent) { Restarter.getInstance().prepare(null); } } }
private void testInitialize(boolean failed) { Restarter.clearInstance(); RestartApplicationListener listener = new RestartApplicationListener(); SpringApplication application = new SpringApplication(); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS)); assertThat(Restarter.getInstance()).isNotEqualTo(nullValue()); assertThat(Restarter.getInstance().isFinished()).isFalse(); listener.onApplicationEvent( new ApplicationPreparedEvent(application, ARGS, context)); if (failed) { listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS, context, new RuntimeException())); } else { listener.onApplicationEvent( new ApplicationReadyEvent(application, ARGS, context)); } }
@Test public void eventsOrder() { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>(); class ApplicationRunningEventListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { events.add((event)); } } application.addListeners(new ApplicationRunningEventListener()); this.context = application.run(); assertThat(5, is(events.size())); assertThat(events.get(0), is(instanceOf(ApplicationStartedEvent.class))); assertThat(events.get(1), is(instanceOf(ApplicationEnvironmentPreparedEvent.class))); assertThat(events.get(2), is(instanceOf(ApplicationPreparedEvent.class))); assertThat(events.get(3), is(instanceOf(ContextRefreshedEvent.class))); assertThat(events.get(4), is(instanceOf(ApplicationReadyEvent.class))); }
private void testInitialize(boolean failed) { Restarter.clearInstance(); RestartApplicationListener listener = new RestartApplicationListener(); SpringApplication application = new SpringApplication(); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS)); assertThat(Restarter.getInstance(), not(nullValue())); assertThat(Restarter.getInstance().isFinished(), equalTo(false)); if (failed) { listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS, context, new RuntimeException())); } else { listener.onApplicationEvent( new ApplicationReadyEvent(application, ARGS, context)); } }
public void onApplicationEvent(ApplicationStartedEvent event) { try { final String certificate = propertyResolver.getSystemProperty(TRUSTED_CA_CERTIFICATE_PROPERTY_NAME); if (!"".equals(certificate)) { LOGGER.info("Following additional CA Certificate has been defined in {} system property : {}", TRUSTED_CA_CERTIFICATE_PROPERTY_NAME, certificate); final TrustStoreInfo trustStoreInfo = trustStoreAppender.append(CertificateFactory.newInstance(certificate)); System.setProperty(SSL_TRUST_STORE_SYSTEM_PROPERTY, trustStoreInfo.getTrustStorefFile().getAbsolutePath()); LOGGER.info("Setting {} system property to {}", SSL_TRUST_STORE_SYSTEM_PROPERTY, trustStoreInfo.getTrustStorefFile().getAbsolutePath()); System.setProperty(SSL_TRUST_STORE_PASSWORD_SYSTEM_PROPERTY, trustStoreInfo.getPassword()); LOGGER.info("Setting {} system property to {}", SSL_TRUST_STORE_PASSWORD_SYSTEM_PROPERTY, trustStoreInfo.getPassword()); } else { LOGGER.warn("No additional CA certificate has been defined using {} system property", TRUSTED_CA_CERTIFICATE_PROPERTY_NAME); } } catch (Exception e) { String message = "Cannot create truststore."; LOGGER.error(message); throw new IllegalStateException(message, e); } LOGGER.info("truststore generated"); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { update(); } }
@Override public void onApplicationEvent(ApplicationEvent event) { if(event instanceof ApplicationStartedEvent) { System.out.println("===== custom started event in initializer"); } else if(event instanceof ApplicationReadyEvent) { System.out.println("===== custom ready event in initializer"); } }
@Override public void onApplicationEvent(ApplicationStartedEvent event) { System.setProperty("SQL_LOG_LEVEL","INFO"); System.setProperty("SPRING_LOG_LEVEL","INFO"); System.setProperty("ORM_LOG_LEVEL","INFO"); if (System.getenv("log_dir") == null && System.getProperty("log_dir") == null) { System.setProperty("log_dir",System.getProperty("java.io.tmpdir")); } if (System.getenv("SQL_LOG_LEVEL")!= null) { System.setProperty("SQL_LOG_LEVEL",System.getenv("SQL_LOG_LEVEL")); } if (System.getenv("SPRING_LOG_LEVEL")!= null) { System.setProperty("SPRING_LOG_LEVEL",System.getenv("SPRING_LOG_LEVEL")); } if (System.getenv("ORM_LOG_LEVEL")!= null) { System.setProperty("ORM_LOG_LEVEL",System.getenv("ORM_LOG_LEVEL")); } }
@Before public void init() throws SecurityException, IOException { LogManager.getLogManager().readConfiguration( JavaLoggingSystem.class.getResourceAsStream("logging.properties")); this.initializer.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); new File("target/foo.log").delete(); new File(tmpDir() + "/spring.log").delete(); }
@Test public void parseArgsDoesntReplace() throws Exception { this.initializer.setSpringBootLogging(LogLevel.ERROR); this.initializer.setParseArgs(false); this.initializer.onApplicationEvent(new ApplicationStartedEvent( this.springApplication, new String[] { "--debug" })); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); this.logger.debug("testatdebug"); assertThat(this.outputCapture.toString()).doesNotContain("testatdebug"); }
@Test public void shutdownHookIsNotRegisteredByDefault() throws Exception { TestLoggingApplicationListener listener = new TestLoggingApplicationListener(); System.setProperty(LoggingSystem.class.getName(), TestShutdownHandlerLoggingSystem.class.getName()); listener.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(listener.shutdownHook).isNull(); }
@Test public void shutdownHookCanBeRegistered() throws Exception { TestLoggingApplicationListener listener = new TestLoggingApplicationListener(); System.setProperty(LoggingSystem.class.getName(), TestShutdownHandlerLoggingSystem.class.getName()); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.register_shutdown_hook=true"); listener.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(listener.shutdownHook).isNotNull(); listener.shutdownHook.start(); assertThat(TestShutdownHandlerLoggingSystem.shutdownLatch.await(30, TimeUnit.SECONDS)).isTrue(); }
@Test public void closingContextCleansUpLoggingSystem() { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName()); this.initializer.onApplicationEvent( new ApplicationStartedEvent(this.springApplication, new String[0])); TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils .getField(this.initializer, "loggingSystem"); assertThat(loggingSystem.cleanedUp).isFalse(); this.initializer.onApplicationEvent(new ContextClosedEvent(this.context)); assertThat(loggingSystem.cleanedUp).isTrue(); }
@Test public void withNoEnvironment() throws Exception { File file = this.temporaryFolder.newFile(); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); listener.setTriggerEventType(ApplicationStartedEvent.class); listener.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), new String[] {})); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); }
private void onApplicationStartedEvent(ApplicationStartedEvent event) { // It's too early to use the Spring environment but we should still allow // users to disable restart using a System property. String enabled = System.getProperty(ENABLED_PROPERTY); if (enabled == null || Boolean.parseBoolean(enabled)) { String[] args = event.getArgs(); DefaultRestartInitializer initializer = new DefaultRestartInitializer(); boolean restartOnInitialize = !AgentReloader.isActive(); Restarter.initialize(args, false, initializer, restartOnInitialize); } else { Restarter.disable(); } }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { onApplicationEnvironmentPreparedEvent( (ApplicationEnvironmentPreparedEvent) event); } else if (event instanceof ContextClosedEvent) { onContextClosedEvent(); } }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { if (this.logger.isDebugEnabled()) { this.logger .debug("Application started with classpath: " + getClasspath()); } } else if (event instanceof ApplicationFailedEvent) { if (this.logger.isInfoEnabled()) { this.logger.info( "Application failed to start with classpath: " + getClasspath()); } } }
@Override public boolean supportsEventType(ResolvableType resolvableType) { Class<?> type = resolvableType.getRawClass(); if (type == null) { return false; } return ApplicationStartedEvent.class.isAssignableFrom(type) || ApplicationFailedEvent.class.isAssignableFrom(type); }
@Test public void parseArgsDoesntReplace() throws Exception { this.initializer.setSpringBootLogging(LogLevel.ERROR); this.initializer.setParseArgs(false); this.initializer.onApplicationEvent(new ApplicationStartedEvent( this.springApplication, new String[] { "--debug" })); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); this.logger.debug("testatdebug"); assertThat(this.outputCapture.toString(), not(containsString("testatdebug"))); }
@Test public void shutdownHookIsNotRegisteredByDefault() throws Exception { System.setProperty(LoggingSystem.class.getName(), NullShutdownHandlerLoggingSystem.class.getName()); this.initializer.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(NullShutdownHandlerLoggingSystem.shutdownHandlerRequested, is(false)); }
@Test public void shutdownHookCanBeRegistered() throws Exception { System.setProperty(LoggingSystem.class.getName(), NullShutdownHandlerLoggingSystem.class.getName()); EnvironmentTestUtils.addEnvironment(this.context, "logging.register_shutdown_hook:true"); this.initializer.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(NullShutdownHandlerLoggingSystem.shutdownHandlerRequested, is(true)); }
@Test public void withNoEnvironment() throws Exception { File file = this.temporaryFolder.newFile(); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); listener.setTriggerEventType(ApplicationStartedEvent.class); listener.onApplicationEvent( new ApplicationStartedEvent(new SpringApplication(), new String[] {})); assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString())); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } if (event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) { Restarter.getInstance().finish(); } }