@Test public void testFileResolveEnvVars() throws EtiqetException { String testFilePath = "${etiqet.directory}/etiqet-core/src/test/resources/properties/test.properties"; InputStream inputStream = fileResolveEnvVars(testFilePath); assertNotNull(inputStream); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); final Map<String, String> properties = reader.lines() .collect(Collectors.toMap(s -> s.split("=")[0], s -> s.split("=")[1])); assertFalse(properties.isEmpty()); assertEquals(12, properties.size()); assertEquals("config.properties", properties.get("fix.config.file.path")); assertEquals("server.cfg", properties.get("fix.server.config.file.path")); assertEquals("database.properties", properties.get("shell.database.properties.path")); assertEquals("remoteshell.properties", properties.get("shell.remoteshell.properties.path")); assertEquals("protocolConfig.properties", properties.get("config.protocol.file.path")); assertEquals("true", properties.get("test.boolean.true")); assertEquals("1", properties.get("test.boolean.convert.true")); assertEquals("false", properties.get("test.boolean.false")); assertEquals("0", properties.get("test.boolean.convert.false")); assertEquals("20", properties.get("test.int")); assertEquals("25.3", properties.get("test.double")); String expected = "etiqet-core/src/test/resources/properties/testConfig.properties" .replaceAll("/", Matcher.quoteReplacement(File.separator)); assertEquals(expected, properties.get("config.client.file.path")); }
public static InputStream fileResolveEnvVars(String path) throws EtiqetException { InputStream res = null; if (!StringUtils.isNullOrEmpty(path)) { StringBuilder sbr = new StringBuilder(); path = resolveEnvVars(path); try (Stream<String> stream = Files.lines(Paths.get(path))) { Iterator<String> str = stream.iterator(); while (str.hasNext()) { String str2 = str.next(); Pattern p = Pattern.compile(("\\$\\{([\\w\\.]+)\\}|\\$([\\w\\.]+)")); Matcher m = p.matcher(str2); StringBuffer sb = new StringBuffer(); resolveEnvVars(m, sb); sbr.append(sb) .append(System.lineSeparator()); } res = new ByteArrayInputStream( sbr.toString().replace("/", File.separator).getBytes(StandardCharsets.UTF_8.name())); } catch (IOException e) { LOG.error("Exception occurred while reading file {}", path, e); } } return res; }
Environment { public static InputStream fileResolveEnvVars(String path) throws EtiqetException { InputStream res = null; if (!StringUtils.isNullOrEmpty(path)) { StringBuilder sbr = new StringBuilder(); path = resolveEnvVars(path); try (Stream<String> stream = Files.lines(Paths.get(path))) { Iterator<String> str = stream.iterator(); while (str.hasNext()) { String str2 = str.next(); Pattern p = Pattern.compile(("\\$\\{([\\w\\.]+)\\}|\\$([\\w\\.]+)")); Matcher m = p.matcher(str2); StringBuffer sb = new StringBuffer(); resolveEnvVars(m, sb); sbr.append(sb) .append(System.lineSeparator()); } res = new ByteArrayInputStream( sbr.toString().replace("/", File.separator).getBytes(StandardCharsets.UTF_8.name())); } catch (IOException e) { LOG.error("Exception occurred while reading file {}", path, e); } } return res; } }
Environment { public static InputStream fileResolveEnvVars(String path) throws EtiqetException { InputStream res = null; if (!StringUtils.isNullOrEmpty(path)) { StringBuilder sbr = new StringBuilder(); path = resolveEnvVars(path); try (Stream<String> stream = Files.lines(Paths.get(path))) { Iterator<String> str = stream.iterator(); while (str.hasNext()) { String str2 = str.next(); Pattern p = Pattern.compile(("\\$\\{([\\w\\.]+)\\}|\\$([\\w\\.]+)")); Matcher m = p.matcher(str2); StringBuffer sb = new StringBuffer(); resolveEnvVars(m, sb); sbr.append(sb) .append(System.lineSeparator()); } res = new ByteArrayInputStream( sbr.toString().replace("/", File.separator).getBytes(StandardCharsets.UTF_8.name())); } catch (IOException e) { LOG.error("Exception occurred while reading file {}", path, e); } } return res; } private  Environment(); }
Environment { public static InputStream fileResolveEnvVars(String path) throws EtiqetException { InputStream res = null; if (!StringUtils.isNullOrEmpty(path)) { StringBuilder sbr = new StringBuilder(); path = resolveEnvVars(path); try (Stream<String> stream = Files.lines(Paths.get(path))) { Iterator<String> str = stream.iterator(); while (str.hasNext()) { String str2 = str.next(); Pattern p = Pattern.compile(("\\$\\{([\\w\\.]+)\\}|\\$([\\w\\.]+)")); Matcher m = p.matcher(str2); StringBuffer sb = new StringBuffer(); resolveEnvVars(m, sb); sbr.append(sb) .append(System.lineSeparator()); } res = new ByteArrayInputStream( sbr.toString().replace("/", File.separator).getBytes(StandardCharsets.UTF_8.name())); } catch (IOException e) { LOG.error("Exception occurred while reading file {}", path, e); } } return res; } private  Environment(); static Boolean isEnvVarSet(String key); static String resolveEnvVars(String input); static InputStream fileResolveEnvVars(String path); }
Environment { public static InputStream fileResolveEnvVars(String path) throws EtiqetException { InputStream res = null; if (!StringUtils.isNullOrEmpty(path)) { StringBuilder sbr = new StringBuilder(); path = resolveEnvVars(path); try (Stream<String> stream = Files.lines(Paths.get(path))) { Iterator<String> str = stream.iterator(); while (str.hasNext()) { String str2 = str.next(); Pattern p = Pattern.compile(("\\$\\{([\\w\\.]+)\\}|\\$([\\w\\.]+)")); Matcher m = p.matcher(str2); StringBuffer sb = new StringBuffer(); resolveEnvVars(m, sb); sbr.append(sb) .append(System.lineSeparator()); } res = new ByteArrayInputStream( sbr.toString().replace("/", File.separator).getBytes(StandardCharsets.UTF_8.name())); } catch (IOException e) { LOG.error("Exception occurred while reading file {}", path, e); } } return res; } private  Environment(); static Boolean isEnvVarSet(String key); static String resolveEnvVars(String input); static InputStream fileResolveEnvVars(String path);  }