Honour existing java.awt.headless configuration

Previously, SpringApplication would set the java.awt.headless system
property even if it had already been set. This commit updates
SpringApplication to honour any existing configuration when setting
the property.

Fixes #1189
pull/1209/head
Andy Wilkinson 11 years ago
parent 53be0f8db7
commit e9c69aa46f

@ -138,6 +138,7 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
* @see #run(Object, String[]) * @see #run(Object, String[])
* @see #run(Object[], String[]) * @see #run(Object[], String[])
* @see #SpringApplication(Object...) * @see #SpringApplication(Object...)
@ -153,6 +154,8 @@ public class SpringApplication {
private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet",
"org.springframework.web.context.ConfigurableWebApplicationContext" }; "org.springframework.web.context.ConfigurableWebApplicationContext" };
private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
private final Log log = LogFactory.getLog(getClass()); private final Log log = LogFactory.getLog(getClass());
private final Set<Object> sources = new LinkedHashSet<Object>(); private final Set<Object> sources = new LinkedHashSet<Object>();
@ -262,7 +265,10 @@ public class SpringApplication {
stopWatch.start(); stopWatch.start();
ConfigurableApplicationContext context = null; ConfigurableApplicationContext context = null;
System.setProperty("java.awt.headless", Boolean.toString(this.headless)); System.setProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
Boolean.toString(this.headless)));
Collection<SpringApplicationRunListener> runListeners = getRunListeners(args); Collection<SpringApplicationRunListener> runListeners = getRunListeners(args);
for (SpringApplicationRunListener runListener : runListeners) { for (SpringApplicationRunListener runListener : runListeners) {

@ -23,6 +23,7 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
@ -81,9 +82,12 @@ import static org.mockito.Mockito.verify;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
*/ */
public class SpringApplicationTests { public class SpringApplicationTests {
private String headlessProperty;
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -96,6 +100,23 @@ public class SpringApplicationTests {
throw new IllegalStateException("Could not obtain Environment"); throw new IllegalStateException("Could not obtain Environment");
} }
@Before
public void storeAndClearHeadlessProperty() {
this.headlessProperty = System.getProperty("java.awt.headless");
System.clearProperty("java.awt.headless");
}
@After
public void reinstateHeadlessProperty() {
if (this.headlessProperty == null) {
System.clearProperty("java.awt.headless");
}
else {
System.setProperty("java.awt.headless", this.headlessProperty);
}
}
@After @After
public void close() { public void close() {
if (this.context != null) { if (this.context != null) {
@ -504,6 +525,15 @@ public class SpringApplicationTests {
assertThat(System.getProperty("java.awt.headless"), equalTo("false")); assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
} }
@Test
public void headlessSystemPropertyTakesPrecedence() throws Exception {
System.setProperty("java.awt.headless", "false");
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
application.run();
assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
}
private boolean hasPropertySource(ConfigurableEnvironment environment, private boolean hasPropertySource(ConfigurableEnvironment environment,
Class<?> propertySourceClass, String name) { Class<?> propertySourceClass, String name) {
for (PropertySource<?> source : environment.getPropertySources()) { for (PropertySource<?> source : environment.getPropertySources()) {

Loading…
Cancel
Save