Skip to content

Commit 2e2ebeb

Browse files
committed
Allow PORTFILE to always override the file to use
Previously, the `PORTFILE` system property was not checked if the `EmbeddedServerPortFileWriter` was created using the default constructor. This had the effect to prevent overriding of the port file when this listener is created without any file or via `META-INF/spring.factories`. Closes gh-4254
1 parent cfbac20 commit 2e2ebeb

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public class EmbeddedServerPortFileWriter
5757
* 'application.port'.
5858
*/
5959
public EmbeddedServerPortFileWriter() {
60-
this.file = new File(DEFAULT_FILE_NAME);
60+
this(new File(DEFAULT_FILE_NAME));
6161
}
6262

6363
/**

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriterTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,16 @@ public void createPortFile() throws Exception {
6666
}
6767

6868
@Test
69-
public void overridePortFile() throws Exception {
69+
public void overridePortFileWithDefault() throws Exception {
70+
System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath());
71+
EmbeddedServerPortFileWriter listener = new EmbeddedServerPortFileWriter();
72+
listener.onApplicationEvent(mockEvent("", 8080));
73+
assertThat(FileCopyUtils.copyToString(
74+
new FileReader(System.getProperty("PORTFILE"))), equalTo("8080"));
75+
}
76+
77+
@Test
78+
public void overridePortFileWithExplicitFile() throws Exception {
7079
File file = this.temporaryFolder.newFile();
7180
System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath());
7281
EmbeddedServerPortFileWriter listener = new EmbeddedServerPortFileWriter(file);

0 commit comments

Comments
 (0)