@Test public void testWriteEnvironmentVariables() throws Exception { Path src = FileSystems.getDefault().getPath("src/test/resources/env.sh"); Path target = FileSystems.getDefault().getPath("target/env.sh"); Files.copy(src, target, StandardCopyOption.REPLACE_EXISTING); File env = new File("target/env.sh"); Map<String, String> envs = new LinkedHashMap<>(); envs.put("HTTP_PORT", "4444"); envs.put("SSH_PORT", "5555"); InstallHelper.writeEnvironmentVariables(env, envs); List<String> lines = Files.readAllLines(env.toPath(), StandardCharsets.UTF_8); int pos1 = 0; int pos2 = 0; int pos3 = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (line.contains("export HTTP_PORT=\"4444\"")) { pos1 = i; } else if (line.contains("export SSH_PORT=\"5555\"")) { pos2 = i; } else if (line.contains("export KARAF_OPTS")) { pos3 = i; } } Assert.assertTrue(pos1 < pos2); Assert.assertTrue(pos1 < pos3); Assert.assertTrue(pos2 < pos3); }
public static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables) throws IOException { if (!envScriptFile.exists()) { envScriptFile.createNewFile(); } List<String> lines = FilesHelper.readLines(envScriptFile); int pos = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (Strings.isNullOrBlank(line)) { continue; } line = line.trim(); if (line.startsWith("set ") || line.startsWith("export ")) { pos = i; break; } else { pos = i; } } lines.add(pos, ""); Set<Map.Entry<String, String>> entries = environmentVariables.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); String line; if (envScriptFile.getName().endsWith(".bat")) { line = "set " + name + "=" + value; } else { line = "export " + name + "=\"" + value + "\""; } lines.add(++pos, line); } lines.add(++pos, ""); Files.writeLines(envScriptFile, lines); }
InstallHelper { public static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables) throws IOException { if (!envScriptFile.exists()) { envScriptFile.createNewFile(); } List<String> lines = FilesHelper.readLines(envScriptFile); int pos = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (Strings.isNullOrBlank(line)) { continue; } line = line.trim(); if (line.startsWith("set ") || line.startsWith("export ")) { pos = i; break; } else { pos = i; } } lines.add(pos, ""); Set<Map.Entry<String, String>> entries = environmentVariables.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); String line; if (envScriptFile.getName().endsWith(".bat")) { line = "set " + name + "=" + value; } else { line = "export " + name + "=\"" + value + "\""; } lines.add(++pos, line); } lines.add(++pos, ""); Files.writeLines(envScriptFile, lines); } }
InstallHelper { public static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables) throws IOException { if (!envScriptFile.exists()) { envScriptFile.createNewFile(); } List<String> lines = FilesHelper.readLines(envScriptFile); int pos = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (Strings.isNullOrBlank(line)) { continue; } line = line.trim(); if (line.startsWith("set ") || line.startsWith("export ")) { pos = i; break; } else { pos = i; } } lines.add(pos, ""); Set<Map.Entry<String, String>> entries = environmentVariables.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); String line; if (envScriptFile.getName().endsWith(".bat")) { line = "set " + name + "=" + value; } else { line = "export " + name + "=\"" + value + "\""; } lines.add(++pos, line); } lines.add(++pos, ""); Files.writeLines(envScriptFile, lines); } private  InstallHelper(); }
InstallHelper { public static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables) throws IOException { if (!envScriptFile.exists()) { envScriptFile.createNewFile(); } List<String> lines = FilesHelper.readLines(envScriptFile); int pos = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (Strings.isNullOrBlank(line)) { continue; } line = line.trim(); if (line.startsWith("set ") || line.startsWith("export ")) { pos = i; break; } else { pos = i; } } lines.add(pos, ""); Set<Map.Entry<String, String>> entries = environmentVariables.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); String line; if (envScriptFile.getName().endsWith(".bat")) { line = "set " + name + "=" + value; } else { line = "export " + name + "=\"" + value + "\""; } lines.add(++pos, line); } lines.add(++pos, ""); Files.writeLines(envScriptFile, lines); } private  InstallHelper(); static void chmodAllScripts(File installDir); static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables); static Map<String, String> readPortsFromDirectory(File directory); static Map<String, String> readPorts(File propertiesFile); static void writePorts(File portFile, Map<String, String> portMap); static String portNameToHostEnvVarName(String portName); }
InstallHelper { public static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables) throws IOException { if (!envScriptFile.exists()) { envScriptFile.createNewFile(); } List<String> lines = FilesHelper.readLines(envScriptFile); int pos = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (Strings.isNullOrBlank(line)) { continue; } line = line.trim(); if (line.startsWith("set ") || line.startsWith("export ")) { pos = i; break; } else { pos = i; } } lines.add(pos, ""); Set<Map.Entry<String, String>> entries = environmentVariables.entrySet(); for (Map.Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); String line; if (envScriptFile.getName().endsWith(".bat")) { line = "set " + name + "=" + value; } else { line = "export " + name + "=\"" + value + "\""; } lines.add(++pos, line); } lines.add(++pos, ""); Files.writeLines(envScriptFile, lines); } private  InstallHelper(); static void chmodAllScripts(File installDir); static void writeEnvironmentVariables(File envScriptFile, Map<String, String> environmentVariables); static Map<String, String> readPortsFromDirectory(File directory); static Map<String, String> readPorts(File propertiesFile); static void writePorts(File portFile, Map<String, String> portMap); static String portNameToHostEnvVarName(String portName); static final String PORTS_PROPERTIES_FILE; static final String ENVIRONMENT_VARIABLE_SCRIPT; static final String ENVIRONMENT_VARIABLE_SCRIPT_WINDOWS; }