Ensure UPPER_CASE overrides work in PropertiesLauncher
This commit is contained in:
		
							parent
							
								
									17e24fd17d
								
							
						
					
					
						commit
						114b7a5e95
					
				|  | @ -132,17 +132,12 @@ public class PropertiesLauncher extends Launcher { | |||
| 	} | ||||
| 
 | ||||
| 	private void initializeProperties(File home) throws Exception, IOException { | ||||
| 		String config = SystemPropertyUtils.resolvePlaceholders(System.getProperty( | ||||
| 				CONFIG_NAME, "application")) + ".properties"; | ||||
| 		InputStream resource = getClasspathResource(config); | ||||
| 		if (resource == null) { | ||||
| 			resource = getResource(new File(home, config).getAbsolutePath()); | ||||
| 		} | ||||
| 		if (resource == null) { | ||||
| 			config = SystemPropertyUtils.resolvePlaceholders(System.getProperty( | ||||
| 					CONFIG_LOCATION, config)); | ||||
| 			resource = getResource(config); | ||||
| 		} | ||||
| 		String config = "classpath:" | ||||
| 				+ SystemPropertyUtils.resolvePlaceholders(SystemPropertyUtils | ||||
| 						.getProperty(CONFIG_NAME, "application")) + ".properties"; | ||||
| 		config = SystemPropertyUtils.resolvePlaceholders(SystemPropertyUtils.getProperty( | ||||
| 				CONFIG_LOCATION, config)); | ||||
| 		InputStream resource = getResource(config); | ||||
| 
 | ||||
| 		if (resource != null) { | ||||
| 			this.logger.info("Found: " + config); | ||||
|  |  | |||
|  | @ -147,21 +147,55 @@ public abstract class SystemPropertyUtils { | |||
| 
 | ||||
| 	private static String resolvePlaceholder(Properties properties, String text, | ||||
| 			String placeholderName) { | ||||
| 		String propVal = getProperty(placeholderName, null, text); | ||||
| 		if (propVal != null) { | ||||
| 			return propVal; | ||||
| 		} | ||||
| 		return properties == null ? null : properties.getProperty(placeholderName); | ||||
| 	} | ||||
| 
 | ||||
| 	public static String getProperty(String key) { | ||||
| 		return getProperty(key, null, ""); | ||||
| 	} | ||||
| 
 | ||||
| 	public static String getProperty(String key, String defaultValue) { | ||||
| 		return getProperty(key, defaultValue, ""); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Search the System properties and environment variables for a value with the | ||||
| 	 * provided key. Environment variables in <code>UPPER_CASE</code> style are allowed | ||||
| 	 * where System properties would normally be <code>lower.case</code>. | ||||
| 	 *  | ||||
| 	 * @param key the key to resolve | ||||
| 	 * @param text optional extra context for an error message if the key resolution fails | ||||
| 	 * (e.g. if System properties are not accessible) | ||||
| 	 * @return a static property value or null of not found | ||||
| 	 */ | ||||
| 	public static String getProperty(String key, String defaultValue, String text) { | ||||
| 		try { | ||||
| 			String propVal = System.getProperty(placeholderName); | ||||
| 			String propVal = System.getProperty(key); | ||||
| 			if (propVal == null) { | ||||
| 				// Fall back to searching the system environment. | ||||
| 				propVal = System.getenv(placeholderName); | ||||
| 				propVal = System.getenv(key); | ||||
| 			} | ||||
| 			if (propVal == null) { | ||||
| 				// Try with underscores. | ||||
| 				propVal = System.getenv(key.replace(".", "_")); | ||||
| 			} | ||||
| 			if (propVal == null) { | ||||
| 				// Try uppercase with underscores as well. | ||||
| 				propVal = System.getenv(key.toUpperCase().replace(".", "_")); | ||||
| 			} | ||||
| 			if (propVal != null) { | ||||
| 				return propVal; | ||||
| 			} | ||||
| 		} | ||||
| 		catch (Throwable ex) { | ||||
| 			System.err.println("Could not resolve placeholder '" + placeholderName | ||||
| 					+ "' in [" + text + "] as system property: " + ex); | ||||
| 			System.err.println("Could not resolve key '" + key + "' in '" + text | ||||
| 					+ "' as system property or in environment: " + ex); | ||||
| 		} | ||||
| 		return properties == null ? null : properties.getProperty(placeholderName); | ||||
| 		return defaultValue; | ||||
| 	} | ||||
| 
 | ||||
| 	private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) { | ||||
|  |  | |||
|  | @ -72,6 +72,15 @@ public class PropertiesLauncherTests { | |||
| 		assertEquals("[etc/]", ReflectionTestUtils.getField(launcher, "paths").toString()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void testUserSpecifiedConfigPathWins() throws Exception { | ||||
| 
 | ||||
| 		System.setProperty("loader.config.name", "foo"); | ||||
| 		System.setProperty("loader.config.location", "classpath:bar.properties"); | ||||
| 		PropertiesLauncher launcher = new PropertiesLauncher(); | ||||
| 		assertEquals("my.BarApplication", launcher.getMainClass()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void testSystemPropertySpecifiedMain() throws Exception { | ||||
| 		System.setProperty("loader.main", "foo.Bar"); | ||||
|  |  | |||
|  | @ -19,7 +19,6 @@ package org.springframework.boot.loader.util; | |||
| import org.junit.AfterClass; | ||||
| import org.junit.BeforeClass; | ||||
| import org.junit.Test; | ||||
| import org.springframework.boot.loader.util.SystemPropertyUtils; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
|  | @ -53,4 +52,9 @@ public class SystemPropertyUtilsTests { | |||
| 		assertEquals("foo", SystemPropertyUtils.resolvePlaceholders("${bar:${spam:foo}}")); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void testEnvVar() { | ||||
| 		assertEquals(System.getenv("LANG"), SystemPropertyUtils.getProperty("lang")); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1 @@ | |||
| loader.main: my.BarApplication | ||||
		Loading…
	
		Reference in New Issue