diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/ApplicationPidListener.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/ApplicationPidListener.java index 3357cad2fca..75a42e39e8f 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/ApplicationPidListener.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/ApplicationPidListener.java @@ -26,10 +26,13 @@ import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.util.Assert; +import org.springframework.util.SystemPropertyUtils; /** * An {@link org.springframework.context.ApplicationListener} that saves application PID - * into file. This application listener will be triggered exactly once per JVM. + * into file. This application listener will be triggered exactly once per JVM, and the file + * name can be overridden at runtime with a System property or environment variable named + * "PIDFILE" (or "pidfile"). * * @author Jakub Kubrynski * @author Dave Syer @@ -62,8 +65,7 @@ public class ApplicationPidListener implements * @param filename the name of file containing pid */ public ApplicationPidListener(String filename) { - Assert.notNull(filename, "Filename must not be null"); - this.file = new File(filename); + this(new File(filename)); } /** @@ -72,7 +74,10 @@ public class ApplicationPidListener implements */ public ApplicationPidListener(File file) { Assert.notNull(file, "File must not be null"); - this.file = file; + String actual = SystemPropertyUtils.resolvePlaceholders("${PIDFILE}", true); + actual = !actual.contains("$") ? actual : SystemPropertyUtils.resolvePlaceholders("${pidfile}", true); + actual = !actual.contains("$") ? actual : file.getAbsolutePath(); + this.file = new File(actual); } @Override diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/ApplicationPidListenerTest.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/ApplicationPidListenerTests.java similarity index 77% rename from spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/ApplicationPidListenerTest.java rename to spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/ApplicationPidListenerTests.java index d134a494c94..eb410db9d20 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/ApplicationPidListenerTest.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/ApplicationPidListenerTests.java @@ -33,12 +33,12 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; /** - * Tests fpr {@link ApplicationPidListener}. + * Tests for {@link ApplicationPidListener}. * * @author Jakub Kubrynski * @author Dave Syer */ -public class ApplicationPidListenerTest { +public class ApplicationPidListenerTests { private static final ApplicationStartedEvent EVENT = new ApplicationStartedEvent( new SpringApplication(), new String[] {}); @@ -49,6 +49,7 @@ public class ApplicationPidListenerTest { @Before @After public void resetListener() { + System.clearProperty("PIDFILE"); ApplicationPidListener.reset(); } @@ -60,4 +61,13 @@ public class ApplicationPidListenerTest { assertThat(FileCopyUtils.copyToString(new FileReader(file)), not(isEmptyString())); } + @Test + public void overridePidFile() throws Exception { + File file = this.temporaryFolder.newFile(); + System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath()); + ApplicationPidListener listener = new ApplicationPidListener(file); + listener.onApplicationEvent(EVENT); + assertThat(FileCopyUtils.copyToString(new FileReader(System.getProperty("PIDFILE"))), not(isEmptyString())); + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java index 86c4e9410ae..d225ba773b0 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/event/ApplicationStartedEvent.java @@ -34,7 +34,7 @@ public class ApplicationStartedEvent extends SpringApplicationEvent { /** * @param application the current application - * @param args the argumemts the application is running with + * @param args the arguments the application is running with */ public ApplicationStartedEvent(SpringApplication application, String[] args) { super(application, args);