From dd66f119d380f6a5095433258f2e4f4a5044bb98 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 6 Feb 2014 14:03:38 -0800 Subject: [PATCH] Revert Automatically detect 'development' profile Revert commit a97bcfe3cded186ac56b7a727f5c7cc09e3c3ba0 as adding profiles automatically may be problematic. Updates gh-296 --- .../simple/SampleSimpleApplication.java | 5 +- .../boot/DevelopmentProfileDetector.java | 95 ------------- .../springframework/boot/ProfileDetector.java | 34 ----- .../boot/SpringApplication.java | 21 --- .../builder/SpringApplicationBuilder.java | 15 +- .../boot/DevelopmentProfileDetectorTests.java | 128 ------------------ .../boot/SpringApplicationTests.java | 23 ---- 7 files changed, 3 insertions(+), 318 deletions(-) delete mode 100644 spring-boot/src/main/java/org/springframework/boot/DevelopmentProfileDetector.java delete mode 100644 spring-boot/src/main/java/org/springframework/boot/ProfileDetector.java delete mode 100644 spring-boot/src/test/java/org/springframework/boot/DevelopmentProfileDetectorTests.java diff --git a/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java b/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java index db8eb105c9..830f95f15a 100644 --- a/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java +++ b/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java @@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -44,8 +43,6 @@ public class SampleSimpleApplication implements CommandLineRunner { } public static void main(String[] args) throws Exception { - ConfigurableApplicationContext context = SpringApplication.run( - SampleSimpleApplication.class, args); - System.out.println(context.getEnvironment().acceptsProfiles("development")); + SpringApplication.run(SampleSimpleApplication.class, args); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/DevelopmentProfileDetector.java b/spring-boot/src/main/java/org/springframework/boot/DevelopmentProfileDetector.java deleted file mode 100644 index c322b32d2b..0000000000 --- a/spring-boot/src/main/java/org/springframework/boot/DevelopmentProfileDetector.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2012-2014 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot; - -import java.io.File; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; -import org.springframework.util.StringUtils; - -/** - * {@link ProfileDetector} that attempts to detect when the application is being developed - * and adds a 'development' profile. - * - * @author Phillip Webb - */ -public class DevelopmentProfileDetector implements ProfileDetector { - - private final Log logger = LogFactory.getLog(getClass()); - - private static final String DEFAULT_PROFILE_NAME = "development"; - - private static final String EXECUTABLE_JAR_CLASS = "org.springframework.boot.loader.Launcher"; - - private static final String[] DEVELOPMENT_TIME_FILES = { "pom.xml", "build.gradle", - "build.xml" }; - - private final String profileName; - - public DevelopmentProfileDetector() { - this(DEFAULT_PROFILE_NAME); - } - - public DevelopmentProfileDetector(String profileName) { - Assert.notNull(profileName, "ProfileName must not be null"); - this.profileName = profileName; - } - - @Override - public void addDetectedProfiles(ConfigurableEnvironment environment) { - if (!isPackageAsJar() && isRunningInDevelopmentDirectory()) { - environment.addActiveProfile(this.profileName); - } - } - - protected boolean isPackageAsJar() { - if (ClassUtils.isPresent(EXECUTABLE_JAR_CLASS, null)) { - this.logger.debug("Development profile not detected: " - + "running inside executable jar"); - return true; - } - String command = System.getProperty("sun.java.command"); - if (StringUtils.hasLength(command) && command.toLowerCase().contains(".jar")) { - this.logger.debug("Development profile not detected: started from a jar"); - return true; - } - return false; - } - - private boolean isRunningInDevelopmentDirectory() { - File userDir = getUserDir(); - if (userDir != null && userDir.exists()) { - for (String developementTimeFile : DEVELOPMENT_TIME_FILES) { - if (new File(userDir, developementTimeFile).exists()) { - this.logger.debug("Development profile detected: file " - + developementTimeFile + " present"); - return true; - } - } - } - return false; - } - - protected File getUserDir() { - String userDir = System.getProperty("user.dir"); - return (userDir == null ? null : new File(userDir)); - } -} diff --git a/spring-boot/src/main/java/org/springframework/boot/ProfileDetector.java b/spring-boot/src/main/java/org/springframework/boot/ProfileDetector.java deleted file mode 100644 index d40edac272..0000000000 --- a/spring-boot/src/main/java/org/springframework/boot/ProfileDetector.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2012-2014 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot; - -import org.springframework.core.env.ConfigurableEnvironment; - -/** - * Strategy interface that can be used to detect active profiles. - * - * @author Phillip Webb - */ -public interface ProfileDetector { - - /** - * Add any detected profiles to the specified environment. - * @param environment the environment - */ - void addDetectedProfiles(ConfigurableEnvironment environment); - -} diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 36d00cef9c..faba0103fe 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -150,9 +150,6 @@ public class SpringApplication { private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext" }; - private static final List DEFAULT_PROFILE_DETECTORS = Arrays - . asList(new DevelopmentProfileDetector()); - private final Log log = LogFactory.getLog(getClass()); private final Set sources = new LinkedHashSet(); @@ -187,8 +184,6 @@ public class SpringApplication { private Set profiles = new HashSet(); - private List profileDetectors = DEFAULT_PROFILE_DETECTORS; - /** * Crate a new {@link SpringApplication} instance. The application context will load * beans from the specified sources (see {@link SpringApplication class-level} @@ -423,9 +418,6 @@ public class SpringApplication { for (String profile : this.profiles) { environment.addActiveProfile(profile); } - for (ProfileDetector profileDetector : this.profileDetectors) { - profileDetector.addDetectedProfiles(environment); - } } /** @@ -739,19 +731,6 @@ public class SpringApplication { this.profiles = new LinkedHashSet(Arrays.asList(profiles)); } - /** - * Sets the {@link ProfileDetector}s that will be used to attempt to detect - * {@link Environment#acceptsProfiles(String...) active profiles}. - * {@link DevelopmentProfileDetector} is used. - * @param profileDetectors the profile detectors - * @see #setAdditionalProfiles(String...) - */ - public void setProfileDetectors(ProfileDetector... profileDetectors) { - Assert.notNull(profileDetectors, "Profile detectors must not be null"); - this.profileDetectors = new ArrayList( - Arrays.asList(profileDetectors)); - } - /** * Sets the bean name generator that should be used when generating bean names. * @param beanNameGenerator the bean name generator diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index 33f270c763..bcf331cf4f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -28,7 +28,6 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import org.springframework.beans.factory.support.BeanNameGenerator; -import org.springframework.boot.ProfileDetector; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.ParentContextApplicationContextInitializer; import org.springframework.context.ApplicationContext; @@ -408,21 +407,11 @@ public class SpringApplicationBuilder { return this; } - /** - * Set the {@link ProfileDetector}s that should be used for this app. - * @param profileDetectors the profile detectors to set - * @return the current builder - */ - public SpringApplicationBuilder profileDetectors(ProfileDetector... profileDetectors) { - this.application.setProfileDetectors(profileDetectors); - return this; - } - private SpringApplicationBuilder additionalProfiles( Collection additionalProfiles) { this.additionalProfiles = new LinkedHashSet(additionalProfiles); - this.application.setAdditionalProfiles(additionalProfiles - .toArray(new String[additionalProfiles.size()])); + this.application.setAdditionalProfiles(this.additionalProfiles + .toArray(new String[this.additionalProfiles.size()])); return this; } diff --git a/spring-boot/src/test/java/org/springframework/boot/DevelopmentProfileDetectorTests.java b/spring-boot/src/test/java/org/springframework/boot/DevelopmentProfileDetectorTests.java deleted file mode 100644 index e313eb693d..0000000000 --- a/spring-boot/src/test/java/org/springframework/boot/DevelopmentProfileDetectorTests.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2012-2014 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot; - -import java.io.File; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.springframework.core.env.ConfigurableEnvironment; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.verifyZeroInteractions; - -/** - * Tests for {@link DevelopmentProfileDetector}. - * - * @author Phillip Webb - */ -public class DevelopmentProfileDetectorTests { - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - private TestDevelopmentProfileDetector detector; - - private ConfigurableEnvironment environment; - - @Before - public void setup() { - this.detector = new TestDevelopmentProfileDetector(); - this.environment = mock(ConfigurableEnvironment.class); - } - - @Test - public void notFound() { - this.detector.addDetectedProfiles(this.environment); - verifyZeroInteractions(this.environment); - } - - @Test - public void foundDueToMavenBuild() throws Exception { - this.temporaryFolder.newFile("pom.xml").createNewFile(); - this.detector.addDetectedProfiles(this.environment); - verify(this.environment).addActiveProfile("development"); - } - - @Test - public void foundDueToGradleBuild() throws Exception { - this.temporaryFolder.newFile("build.gradle").createNewFile(); - this.detector.addDetectedProfiles(this.environment); - verify(this.environment).addActiveProfile("development"); - } - - @Test - public void foundDueToAntBuild() throws Exception { - this.temporaryFolder.newFile("build.xml").createNewFile(); - this.detector.addDetectedProfiles(this.environment); - verify(this.environment).addActiveProfile("development"); - } - - @Test - public void differentProfileName() throws Exception { - this.detector = new TestDevelopmentProfileDetector("different"); - this.temporaryFolder.newFile("pom.xml").createNewFile(); - this.detector.addDetectedProfiles(this.environment); - verify(this.environment).addActiveProfile("different"); - verifyNoMoreInteractions(this.environment); - } - - @Test - public void notFoundWhenStartedFromJar() throws Exception { - System.setProperty("sun.java.command", "something.jar"); - this.temporaryFolder.newFile("pom.xml").createNewFile(); - this.detector.setSkipPackageAsJar(false); - this.detector.addDetectedProfiles(this.environment); - verifyZeroInteractions(this.environment); - } - - private class TestDevelopmentProfileDetector extends DevelopmentProfileDetector { - - private boolean skipPackageAsJar = true; - - public TestDevelopmentProfileDetector() { - super(); - } - - public TestDevelopmentProfileDetector(String profileName) { - super(profileName); - } - - public void setSkipPackageAsJar(boolean skipPackageAsJar) { - this.skipPackageAsJar = skipPackageAsJar; - } - - @Override - protected boolean isPackageAsJar() { - if (this.skipPackageAsJar) { - // Unfortunately surefire uses a jar so we need to stub this out - return false; - } - return super.isPackageAsJar(); - } - - @Override - protected File getUserDir() { - return DevelopmentProfileDetectorTests.this.temporaryFolder.getRoot(); - } - - } -} diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 649bc44333..77aa86507e 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -421,29 +421,6 @@ public class SpringApplicationTests { assertThat(System.getProperty("java.awt.headless"), equalTo("false")); } - @Test - public void setAdditionalProfiles() throws Exception { - TestSpringApplication application = new TestSpringApplication(ExampleConfig.class); - application.setWebEnvironment(false); - application.setAdditionalProfiles("mine"); - this.context = application.run(); - assertThat(this.context.getEnvironment().acceptsProfiles("mine"), equalTo(true)); - } - - @Test - public void setProfileDetectors() throws Exception { - TestSpringApplication application = new TestSpringApplication(ExampleConfig.class); - application.setWebEnvironment(false); - application.setProfileDetectors(new ProfileDetector() { - @Override - public void addDetectedProfiles(ConfigurableEnvironment environment) { - environment.addActiveProfile("mine"); - } - }); - this.context = application.run(); - assertThat(this.context.getEnvironment().acceptsProfiles("mine"), equalTo(true)); - } - private boolean hasPropertySource(ConfigurableEnvironment environment, Class propertySourceClass, String name) { for (PropertySource source : environment.getPropertySources()) {