/** * Checks for the presence of a supplied or system property named {@code spring.cloud.propertiesFile}. If the property * is present, load its contents into {@link #fileProperties}. If there's a problem, log but continue. */ private void readFileProperties() { fileProperties = new Properties(); logger.fine("looking for a properties file"); // will search system properties and the classpath File propertiesFile = new PropertiesFileResolver(env).findCloudPropertiesFile(); if (propertiesFile == null) { logger.info("not loading service definitions from a properties file"); return; } if (!fileExists(propertiesFile)) { logger.info("properties file " + propertiesFile + " does not exist; probably running in a real cloud"); return; } logger.info("loading service definitions from properties file " + propertiesFile); try { InputStream fis = openFile(propertiesFile); fileProperties.load(fis); fis.close(); } catch (IOException e) { logger.log(Level.SEVERE, "exception while loading properties from file " + propertiesFile, e); return; } logger.info("properties loaded successfully"); }
/** * Checks for the presence of a supplied or system property named {@code spring.cloud.propertiesFile}. If the property * is present, load its contents into {@link #fileProperties}. If there's a problem, log but continue. */ private void readFileProperties() { fileProperties = new Properties(); logger.fine("looking for a properties file"); // will search system properties and the classpath File propertiesFile = new PropertiesFileResolver(env).findCloudPropertiesFile(); if (propertiesFile == null) { logger.info("not loading service definitions from a properties file"); return; } if (!fileExists(propertiesFile)) { logger.info("properties file " + propertiesFile + " does not exist; probably running in a real cloud"); return; } logger.info("loading service definitions from properties file " + propertiesFile); try { InputStream fis = openFile(propertiesFile); fileProperties.load(fis); fis.close(); } catch (IOException e) { logger.log(Level.SEVERE, "exception while loading properties from file " + propertiesFile, e); return; } logger.info("properties loaded successfully"); }
@Test public void testNowhere() { assertNull(resolver.findCloudPropertiesFile()); }
@Test public void testFromSystem() { env.setSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY, PROPERTIES_FILE_NAME); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFile().getPath()); }
@Test public void testPrecedence() { env.setSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY, PROPERTIES_FILE_NAME); resolver = new PropertiesFileResolver(env, "spring-cloud-literal.properties"); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFile().getPath()); } }
@Test public void testFromClasspath() { resolver = new PropertiesFileResolver(env, "spring-cloud-template.properties"); env.setSystemProperty("user.home", "/foo"); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFile().getPath()); }