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 { | 	private void initializeProperties(File home) throws Exception, IOException { | ||||||
| 		String config = SystemPropertyUtils.resolvePlaceholders(System.getProperty( | 		String config = "classpath:" | ||||||
| 				CONFIG_NAME, "application")) + ".properties"; | 				+ SystemPropertyUtils.resolvePlaceholders(SystemPropertyUtils | ||||||
| 		InputStream resource = getClasspathResource(config); | 						.getProperty(CONFIG_NAME, "application")) + ".properties"; | ||||||
| 		if (resource == null) { | 		config = SystemPropertyUtils.resolvePlaceholders(SystemPropertyUtils.getProperty( | ||||||
| 			resource = getResource(new File(home, config).getAbsolutePath()); | 				CONFIG_LOCATION, config)); | ||||||
| 		} | 		InputStream resource = getResource(config); | ||||||
| 		if (resource == null) { |  | ||||||
| 			config = SystemPropertyUtils.resolvePlaceholders(System.getProperty( |  | ||||||
| 					CONFIG_LOCATION, config)); |  | ||||||
| 			resource = getResource(config); |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		if (resource != null) { | 		if (resource != null) { | ||||||
| 			this.logger.info("Found: " + config); | 			this.logger.info("Found: " + config); | ||||||
|  |  | ||||||
|  | @ -147,21 +147,55 @@ public abstract class SystemPropertyUtils { | ||||||
| 
 | 
 | ||||||
| 	private static String resolvePlaceholder(Properties properties, String text, | 	private static String resolvePlaceholder(Properties properties, String text, | ||||||
| 			String placeholderName) { | 			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 { | 		try { | ||||||
| 			String propVal = System.getProperty(placeholderName); | 			String propVal = System.getProperty(key); | ||||||
| 			if (propVal == null) { | 			if (propVal == null) { | ||||||
| 				// Fall back to searching the system environment. | 				// 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) { | 			if (propVal != null) { | ||||||
| 				return propVal; | 				return propVal; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		catch (Throwable ex) { | 		catch (Throwable ex) { | ||||||
| 			System.err.println("Could not resolve placeholder '" + placeholderName | 			System.err.println("Could not resolve key '" + key + "' in '" + text | ||||||
| 					+ "' in [" + text + "] as system property: " + ex); | 					+ "' as system property or in environment: " + ex); | ||||||
| 		} | 		} | ||||||
| 		return properties == null ? null : properties.getProperty(placeholderName); | 		return defaultValue; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) { | 	private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) { | ||||||
|  |  | ||||||
|  | @ -72,6 +72,15 @@ public class PropertiesLauncherTests { | ||||||
| 		assertEquals("[etc/]", ReflectionTestUtils.getField(launcher, "paths").toString()); | 		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 | 	@Test | ||||||
| 	public void testSystemPropertySpecifiedMain() throws Exception { | 	public void testSystemPropertySpecifiedMain() throws Exception { | ||||||
| 		System.setProperty("loader.main", "foo.Bar"); | 		System.setProperty("loader.main", "foo.Bar"); | ||||||
|  |  | ||||||
|  | @ -19,7 +19,6 @@ package org.springframework.boot.loader.util; | ||||||
| import org.junit.AfterClass; | import org.junit.AfterClass; | ||||||
| import org.junit.BeforeClass; | import org.junit.BeforeClass; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| import org.springframework.boot.loader.util.SystemPropertyUtils; |  | ||||||
| 
 | 
 | ||||||
| import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||||
| 
 | 
 | ||||||
|  | @ -53,4 +52,9 @@ public class SystemPropertyUtilsTests { | ||||||
| 		assertEquals("foo", SystemPropertyUtils.resolvePlaceholders("${bar:${spam:foo}}")); | 		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