@Test public void testFilterEnvInCmdStr() { Map<String, String> env = new HashMap<>(); env.put("fool", "duff"); env.put("ass", "david"); env.put("LS_COLORS", "r=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01"); String cmd = "In front of me is ${fool}, beside me is $ass. $fool likes ${ass}. this is ${nobody} i don't know."; String expected = "In front of me is duff, beside me is david. duff likes david. this is ${nobody} i don't know."; String result = ScriptPidUtils.filterEnvInCmdStr(env, cmd); assertThat(result).isEqualTo(expected); }
public static String filterEnvInCmdStr(Map<String, String> env, String cmd) { String patternString = "\\$\\{?(" + StringUtils.join(env.keySet(), "|") + ")\\}?"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(cmd); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, env.get(matcher.group(1))); } matcher.appendTail(sb); return sb.toString(); }
ScriptPidUtils { public static String filterEnvInCmdStr(Map<String, String> env, String cmd) { String patternString = "\\$\\{?(" + StringUtils.join(env.keySet(), "|") + ")\\}?"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(cmd); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, env.get(matcher.group(1))); } matcher.appendTail(sb); return sb.toString(); } }
ScriptPidUtils { public static String filterEnvInCmdStr(Map<String, String> env, String cmd) { String patternString = "\\$\\{?(" + StringUtils.join(env.keySet(), "|") + ")\\}?"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(cmd); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, env.get(matcher.group(1))); } matcher.appendTail(sb); return sb.toString(); }  }
ScriptPidUtils { public static String filterEnvInCmdStr(Map<String, String> env, String cmd) { String patternString = "\\$\\{?(" + StringUtils.join(env.keySet(), "|") + ")\\}?"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(cmd); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, env.get(matcher.group(1))); } matcher.appendTail(sb); return sb.toString(); }  static File getSaturnExecutingHome(); static void writePidToFile(String executorName, String jobName, int jobItem, long pid); @Deprecated static long _getPidFromFile(String executorName, String jobName, String jobItem); static long getFirstPidFromFile(String executorName, String jobName, String jobItem); static List<Long> getPidsFromFile(String executorName, String jobName, String jobItem); static String[] getItemsPaths(String executorName, String jobName); static boolean removeAllPidFile(String executorName, String jobName, String jobItem); static boolean removePidFile(String executorName, String jobName, String jobItem, long pid); static boolean removeAllPidFile(String executorName, String jobName, int jobItem); static void killAllChildrenByPid(long pid, boolean force); static String exeWholeCmd(String cmd); static String exeCmdWithoutPipe(CommandLine cmdLine, ByteArrayInputStream input, Map<String, String> env); static void checkAllExistJobs(final CoordinatorRegistryCenter regCenter); static void forceStopRunningShellJob(final String executorName, final String jobName); static void checkOneExistJob(final CoordinatorRegistryCenter regCenter, final String jobName); static boolean isPidRunning(long pid); static boolean isPidRunning(String pid); static Map<String, String> parseString2Map(String source); static Map<String, String> loadEnv(); static String filterEnvInCmdStr(Map<String, String> env, String cmd); }
ScriptPidUtils { public static String filterEnvInCmdStr(Map<String, String> env, String cmd) { String patternString = "\\$\\{?(" + StringUtils.join(env.keySet(), "|") + ")\\}?"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(cmd); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, env.get(matcher.group(1))); } matcher.appendTail(sb); return sb.toString(); }  static File getSaturnExecutingHome(); static void writePidToFile(String executorName, String jobName, int jobItem, long pid); @Deprecated static long _getPidFromFile(String executorName, String jobName, String jobItem); static long getFirstPidFromFile(String executorName, String jobName, String jobItem); static List<Long> getPidsFromFile(String executorName, String jobName, String jobItem); static String[] getItemsPaths(String executorName, String jobName); static boolean removeAllPidFile(String executorName, String jobName, String jobItem); static boolean removePidFile(String executorName, String jobName, String jobItem, long pid); static boolean removeAllPidFile(String executorName, String jobName, int jobItem); static void killAllChildrenByPid(long pid, boolean force); static String exeWholeCmd(String cmd); static String exeCmdWithoutPipe(CommandLine cmdLine, ByteArrayInputStream input, Map<String, String> env); static void checkAllExistJobs(final CoordinatorRegistryCenter regCenter); static void forceStopRunningShellJob(final String executorName, final String jobName); static void checkOneExistJob(final CoordinatorRegistryCenter regCenter, final String jobName); static boolean isPidRunning(long pid); static boolean isPidRunning(String pid); static Map<String, String> parseString2Map(String source); static Map<String, String> loadEnv(); static String filterEnvInCmdStr(Map<String, String> env, String cmd); static final long UNKNOWN_PID; static final String EXECUTINGPATH; static final String OUTPUT_PATH; static final String EXECUTINGJOBPATH; static final String JOBITEMPIDSPATH; static final String JOBITEMPATH; static final String JOBITEMPIDPATH2; static final String JOBITEMOUTPUTPATH; }