@Override public void onApplicationEvent(SpringApplicationEvent event) { if (this.triggerEventType.isInstance(event)) { if (created.compareAndSet(false, true)) { try { writePidFile(event); } catch (Exception ex) { String message = String.format("Cannot create pid file %s", this.file); if (failOnWriteError(event)) { throw new IllegalStateException(message, ex); } logger.warn(message, ex); } } } }
private void writePidFile(SpringApplicationEvent event) throws IOException { File pidFile = this.file; String override = getProperty(event, FILE_PROPERTIES); if (override != null) { pidFile = new File(override); } new ApplicationPid().write(pidFile); pidFile.deleteOnExit(); }
private String getProperty(SpringApplicationEvent event, List<Property> candidates) { for (Property candidate : candidates) { String value = candidate.getValue(event); if (value != null) { return value; } } return null; }
@Override public String getValue(SpringApplicationEvent event) { Environment environment = getEnvironment(event); if (environment == null) { return null; } return new RelaxedPropertyResolver(environment, this.prefix) .getProperty(this.key); }
private Environment getEnvironment(SpringApplicationEvent event) { if (event instanceof ApplicationEnvironmentPreparedEvent) { return ((ApplicationEnvironmentPreparedEvent) event).getEnvironment(); } if (event instanceof ApplicationPreparedEvent) { return ((ApplicationPreparedEvent) event).getApplicationContext() .getEnvironment(); } return null; }
@Test public void overridePidFileWithSpring() throws Exception { File file = this.temporaryFolder.newFile(); SpringApplicationEvent event = createPreparedEvent("spring.pid.file", file.getAbsolutePath()); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); listener.onApplicationEvent(event); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); }
@Test public void differentEventTypes() throws Exception { File file = this.temporaryFolder.newFile(); SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file", file.getAbsolutePath()); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); listener.onApplicationEvent(event); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty(); listener.setTriggerEventType(ApplicationEnvironmentPreparedEvent.class); listener.onApplicationEvent(event); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); }
@Test public void throwWhenPidFileIsReadOnlyWithSpring() throws Exception { File file = this.temporaryFolder.newFile(); file.setReadOnly(); SpringApplicationEvent event = createPreparedEvent( "spring.pid.fail-on-write-error", "true"); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); this.exception.expect(IllegalStateException.class); this.exception.expectMessage("Cannot create pid file"); listener.onApplicationEvent(event); }
private SpringApplicationEvent createPreparedEvent(String propName, String propValue) { ConfigurableEnvironment environment = createEnvironment(propName, propValue); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); given(context.getEnvironment()).willReturn(environment); return new ApplicationPreparedEvent(new SpringApplication(), new String[] {}, context); }
@Test public void overridePidFileWithSpring() throws Exception { File file = this.temporaryFolder.newFile(); SpringApplicationEvent event = createPreparedEvent("spring.pid.file", file.getAbsolutePath()); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); listener.onApplicationEvent(event); assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString())); }
@Test public void differentEventTypes() throws Exception { File file = this.temporaryFolder.newFile(); SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file", file.getAbsolutePath()); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); listener.onApplicationEvent(event); assertThat(FileCopyUtils.copyToString(new FileReader(file)), isEmptyString()); listener.setTriggerEventType(ApplicationEnvironmentPreparedEvent.class); listener.onApplicationEvent(event); assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString())); }
private boolean failOnWriteError(SpringApplicationEvent event) { String value = getProperty(event, FAIL_ON_WRITE_ERROR_PROPERTIES); return (value == null ? false : Boolean.parseBoolean(value)); }
@Override public String getValue(SpringApplicationEvent event) { return SystemProperties.get(this.properties); }
private SpringApplicationEvent createEnvironmentPreparedEvent(String propName, String propValue) { ConfigurableEnvironment environment = createEnvironment(propName, propValue); return new ApplicationEnvironmentPreparedEvent(new SpringApplication(), new String[] {}, environment); }
/** * Sets the type of application event that will trigger writing of the PID file. * Defaults to {@link ApplicationPreparedEvent}. NOTE: If you use the * {@link org.springframework.boot.context.event.ApplicationStartedEvent} to trigger * the write, you will not be able to specify the PID filename in the Spring * {@link Environment}. * @param triggerEventType the trigger event type */ public void setTriggerEventType( Class<? extends SpringApplicationEvent> triggerEventType) { Assert.notNull(triggerEventType, "Trigger event type must not be null"); this.triggerEventType = triggerEventType; }
String getValue(SpringApplicationEvent event);