From 2e2ebeb9fa1431ea33c19dc04a72f1b79f4033d4 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 21 Oct 2015 13:58:48 +0200 Subject: [PATCH] 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 --- .../system/EmbeddedServerPortFileWriter.java | 4 ++-- .../system/EmbeddedServerPortFileWriterTests.java | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriter.java index a94b542b1c..b687750803 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,7 +57,7 @@ public class EmbeddedServerPortFileWriter * 'application.port'. */ public EmbeddedServerPortFileWriter() { - this.file = new File(DEFAULT_FILE_NAME); + this(new File(DEFAULT_FILE_NAME)); } /** diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriterTests.java index 8187183d22..5d5d540b0d 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/EmbeddedServerPortFileWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,7 +66,16 @@ public class EmbeddedServerPortFileWriterTests { } @Test - public void overridePortFile() throws Exception { + public void overridePortFileWithDefault() throws Exception { + System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath()); + EmbeddedServerPortFileWriter listener = new EmbeddedServerPortFileWriter(); + listener.onApplicationEvent(mockEvent("", 8080)); + assertThat(FileCopyUtils.copyToString( + new FileReader(System.getProperty("PORTFILE"))), equalTo("8080")); + } + + @Test + public void overridePortFileWithExplicitFile() throws Exception { File file = this.temporaryFolder.newFile(); System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath()); EmbeddedServerPortFileWriter listener = new EmbeddedServerPortFileWriter(file);