Util { public static Properties extractPropertiesFromExpression(String expression) { Properties props = new Properties(); if (expression == null || expression.isEmpty()) { return props; } String[] expressionArr = expression.split("\\|"); if (expressionArr.length != 3) { throw new IllegalArgumentException("Bad expression: " + expression); } String propertyName = expressionArr[0]; Pattern regex = Pattern.compile(expressionArr[1]); String[] labelNames = expressionArr[2].split(","); if (labelNames.length > 0) { String propertyValue = System.getProperty(propertyName, ""); Matcher matcher = regex.matcher(propertyValue); if (matcher.matches()) { if (matcher.groupCount() == 0) { props.put(labelNames[0], matcher.group(0)); } else { for (int g = 1, l = 0; g <= matcher.groupCount() && l < labelNames.length; g++, l++) { String groupValue = matcher.group(g); if (!groupValue.isEmpty()) { props.put(labelNames[l], matcher.group(g)); } } } } } return props; }  static String toJson(Object obj); static T fromJson(String json, Class<T> clazz); static T fromJson(Reader in, TypeReference<T> typeReference); static String urlEncode(String str); static String urlEncodeQuery(String str); static StringBuilder getContextUrlString(String baseUrl, String context); static void ensureEndsWithSlash(StringBuilder str); static long copyStream(InputStream input, OutputStream output, boolean closeStreams); static String slurpStream(InputStream input, String encoding); static String read(File file); static void write(String string, File file); static String base64Encode(String plainTextString); static String getMachineId(); static String getContainerId(); static String extractDockerContainerIdFromString(String s); static Properties extractPropertiesFromExpression(String expression);  }
@Test public void testExtractPropertiesFromExpression() { Properties props; System.setProperty("jboss.node.name", "master:server-one"); props = Util.extractPropertiesFromExpression("jboss.node.name|([^:]+)[:]?(.*)?|domain_host,domain_server"); Assert.assertEquals(2, props.size()); Assert.assertEquals("master", props.get("domain_host")); Assert.assertEquals("server-one", props.get("domain_server")); System.setProperty("jboss.node.name", "server-one"); props = Util.extractPropertiesFromExpression("jboss.node.name|([^:]+)[:]?(.*)?|domain_host,domain_server"); Assert.assertEquals(1, props.size()); Assert.assertEquals("server-one", props.get("domain_host")); props = Util.extractPropertiesFromExpression(null); Assert.assertEquals(0, props.size()); props = Util.extractPropertiesFromExpression(""); Assert.assertEquals(0, props.size()); System.clearProperty("my.prop"); props = Util.extractPropertiesFromExpression("my.prop|([^:]+)[:]?(.*)?|abc,xyz"); Assert.assertEquals(0, props.size()); System.setProperty("my.prop", "foo"); props = Util.extractPropertiesFromExpression("my.prop|([^:]+)[:]?(.*)?|abc,xyz"); Assert.assertEquals(1, props.size()); Assert.assertEquals("foo", props.get("abc")); System.setProperty("my.prop", "foo"); props = Util.extractPropertiesFromExpression("my.prop|(.+)|abc,xyz"); Assert.assertEquals(1, props.size()); Assert.assertEquals("foo", props.get("abc")); System.setProperty("my.prop", "foo"); props = Util.extractPropertiesFromExpression("my.prop|.+|abc,xyz"); System.out.println(props); Assert.assertEquals(1, props.size()); Assert.assertEquals("foo", props.get("abc")); System.clearProperty("my.prop"); try { Util.extractPropertiesFromExpression("my.prop|(.+)"); Assert.fail("bad expression: missing labels"); } catch (Exception expected) { } try { Util.extractPropertiesFromExpression("my.prop|(.+)|a,b|this is bad"); Assert.fail("bad expression"); } catch (Exception expected) { } }