Skip to content

Commit 8d9f154

Browse files
committed
Support getting environment from other events.
1 parent 4d64c9c commit 8d9f154

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

spring-boot/src/main/java/org/springframework/boot/system/ApplicationPidFileWriter.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
2123
import java.util.ArrayList;
2224
import java.util.Collections;
2325
import java.util.List;
@@ -34,6 +36,7 @@
3436
import org.springframework.context.ApplicationListener;
3537
import org.springframework.core.Ordered;
3638
import org.springframework.core.env.Environment;
39+
import org.springframework.core.env.EnvironmentCapable;
3740
import org.springframework.util.Assert;
3841

3942
/**
@@ -49,7 +52,8 @@
4952
* <p>
5053
* Note: access to the Spring {@link Environment} is only possible when the
5154
* {@link #setTriggerEventType(Class) triggerEventType} is set to
52-
* {@link ApplicationEnvironmentPreparedEvent} or {@link ApplicationPreparedEvent}.
55+
* {@link ApplicationEnvironmentPreparedEvent} or any event that has a getApplicationContext
56+
* method.
5357
*
5458
* @author Jakub Kubrynski
5559
* @author Dave Syer
@@ -227,11 +231,19 @@ private Environment getEnvironment(SpringApplicationEvent event) {
227231
if (event instanceof ApplicationEnvironmentPreparedEvent) {
228232
return ((ApplicationEnvironmentPreparedEvent) event).getEnvironment();
229233
}
230-
if (event instanceof ApplicationPreparedEvent) {
231-
return ((ApplicationPreparedEvent) event).getApplicationContext()
232-
.getEnvironment();
234+
235+
try {
236+
Method getApplicationContextMethod = event.getClass().getMethod("getApplicationContext", (Class[]) null);
237+
return ((EnvironmentCapable) getApplicationContextMethod.invoke(event, (Object[]) null)).getEnvironment();
238+
}
239+
catch (NoSuchMethodException | IllegalAccessException e) {
240+
logger.warn("Event '" + event.getClass() + "' does not provide a 'getApplicationContext()' method. Ignoring Environment config.");
241+
return null;
242+
}
243+
catch (InvocationTargetException e) {
244+
logger.warn("Unexpected error getting application context from event. Ignoring Environment config.", e.getTargetException());
245+
return null;
233246
}
234-
return null;
235247
}
236248

237249
}

spring-boot/src/test/java/org/springframework/boot/system/ApplicationPidFileWriterTests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.SpringApplication;
3030
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
3131
import org.springframework.boot.context.event.ApplicationPreparedEvent;
32+
import org.springframework.boot.context.event.ApplicationReadyEvent;
3233
import org.springframework.boot.context.event.ApplicationStartedEvent;
3334
import org.springframework.boot.context.event.SpringApplicationEvent;
3435
import org.springframework.context.ConfigurableApplicationContext;
@@ -99,7 +100,7 @@ public void overridePidFileWithSpring() throws Exception {
99100
}
100101

101102
@Test
102-
public void differentEventTypes() throws Exception {
103+
public void tryEnvironmentPreparedEvent() throws Exception {
103104
File file = this.temporaryFolder.newFile();
104105
SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file",
105106
file.getAbsolutePath());
@@ -111,6 +112,19 @@ public void differentEventTypes() throws Exception {
111112
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
112113
}
113114

115+
@Test
116+
public void tryReadyEvent() throws Exception {
117+
File file = this.temporaryFolder.newFile();
118+
SpringApplicationEvent event = createReadyEvent("spring.pid.file",
119+
file.getAbsolutePath());
120+
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
121+
listener.onApplicationEvent(event);
122+
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
123+
listener.setTriggerEventType(ApplicationReadyEvent.class);
124+
listener.onApplicationEvent(event);
125+
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
126+
}
127+
114128
@Test
115129
public void withNoEnvironment() throws Exception {
116130
File file = this.temporaryFolder.newFile();
@@ -170,6 +184,16 @@ private SpringApplicationEvent createPreparedEvent(String propName,
170184
context);
171185
}
172186

187+
private SpringApplicationEvent createReadyEvent(String propName,
188+
String propValue) {
189+
ConfigurableEnvironment environment = createEnvironment(propName, propValue);
190+
ConfigurableApplicationContext context = mock(
191+
ConfigurableApplicationContext.class);
192+
given(context.getEnvironment()).willReturn(environment);
193+
return new ApplicationReadyEvent(new SpringApplication(), new String[] {},
194+
context);
195+
}
196+
173197
private ConfigurableEnvironment createEnvironment(String propName, String propValue) {
174198
MockPropertySource propertySource = mockPropertySource(propName, propValue);
175199
ConfigurableEnvironment environment = new StandardEnvironment();

0 commit comments

Comments
 (0)