ApplicationProperties extends PropertiesConfiguration { public static InputStream getFileAsInputStream(Configuration configuration, String propertyName, String defaultFileName) throws AtlasException { File fileToLoad = null; String fileName = configuration.getString(propertyName); if (fileName == null) { if (defaultFileName == null) { throw new AtlasException(propertyName + " property not set and no default value specified"); } fileName = defaultFileName; String atlasConfDir = System.getProperty(ATLAS_CONFIGURATION_DIRECTORY_PROPERTY); if (atlasConfDir != null) { fileToLoad = new File(atlasConfDir, fileName); } else { fileToLoad = new File(fileName); } if (LOG.isDebugEnabled()) { LOG.debug("{} property not set - defaulting to {}", propertyName, fileToLoad.getPath()); } } else { fileToLoad = new File(fileName); if (LOG.isDebugEnabled()) { LOG.debug("Using {} property setting: {}", propertyName, fileToLoad.getPath()); } } InputStream inStr = null; if (fileToLoad.exists()) { try { inStr = new FileInputStream(fileToLoad); } catch (FileNotFoundException e) { throw new AtlasException("Error loading file " + fileName, e); } if (LOG.isDebugEnabled()) { LOG.debug("Loaded file from : {}", fileToLoad.getPath()); } } else { inStr = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); if (inStr == null) { String msg = fileName + " not found in file system or as class loader resource"; LOG.error(msg); throw new AtlasException(msg); } if (LOG.isDebugEnabled()) { LOG.debug("Loaded {} as resource from : {}", fileName, Thread.currentThread().getContextClassLoader().getResource(fileName).toString()); } } return inStr; } private  ApplicationProperties(URL url); static void forceReload(); static Configuration get(); static Configuration get(String fileName); static Configuration getSubsetConfiguration(Configuration inConf, String prefix); static Class getClass(Configuration configuration, String propertyName, String defaultValue,
                                 Class assignableClass); static InputStream getFileAsInputStream(Configuration configuration, String propertyName, String defaultFileName); static final String ATLAS_CONFIGURATION_DIRECTORY_PROPERTY; static final String APPLICATION_PROPERTIES; }
@Test public void testGetFileAsInputStream() throws Exception { Configuration props = ApplicationProperties.get("test.properties"); InputStream inStr = null; try { inStr = ApplicationProperties.getFileAsInputStream(props, "jaas.properties.file", null); assertNotNull(inStr); } finally { if (inStr != null) { inStr.close(); } } props.setProperty("jaas.properties.file", "src/test/resources/atlas-jaas.properties"); try { inStr = ApplicationProperties.getFileAsInputStream(props, "jaas.properties.file", null); assertNotNull(inStr); } finally { if (inStr != null) { inStr.close(); } } try { inStr = ApplicationProperties.getFileAsInputStream(props, "property.not.specified.in.config", "atlas-jaas.properties"); assertNotNull(inStr); } finally { if (inStr != null) { inStr.close(); } } try { inStr = ApplicationProperties.getFileAsInputStream(props, "property.not.specified.in.config", "src/test/resources/atlas-jaas.properties"); assertNotNull(inStr); } finally { if (inStr != null) { inStr.close(); } } String originalConfDirSetting = System.setProperty(ApplicationProperties.ATLAS_CONFIGURATION_DIRECTORY_PROPERTY, "src/test/resources"); try { inStr = ApplicationProperties.getFileAsInputStream(props, "property.not.specified.in.config", "atlas-jaas.properties"); assertNotNull(inStr); } finally { if (inStr != null) { inStr.close(); } if (originalConfDirSetting != null) { System.setProperty(ApplicationProperties.ATLAS_CONFIGURATION_DIRECTORY_PROPERTY, originalConfDirSetting); } else { System.clearProperty(ApplicationProperties.ATLAS_CONFIGURATION_DIRECTORY_PROPERTY); } } try { inStr = ApplicationProperties.getFileAsInputStream(props, "property.not.specified.in.config", null); fail("Expected " + AtlasException.class.getSimpleName() + " but none thrown"); } catch (AtlasException e) { } finally { if (inStr != null) { inStr.close(); } } props.setProperty("jaas.properties.file", "does_not_exist.txt"); try { inStr = ApplicationProperties.getFileAsInputStream(props, "jaas.properties.file", null); fail("Expected " + AtlasException.class.getSimpleName() + " but none thrown"); } catch (AtlasException e) { } finally { if (inStr != null) { inStr.close(); } } }