DMRLocationResolver implements LocationResolver<DMRNodeLocation> { @Override public String applyTemplate(String nameTemplate, DMRNodeLocation location, String endpointName) { List<String> args = new ArrayList<>(); List<PathElement> reverseSegments = new ArrayList<>(); for (PathElement segment : location.getPathAddress()) { reverseSegments.add(segment); args.add(segment.getKey()); args.add(segment.getValue()); nameTemplate = nameTemplate.replace("%" + segment.getKey() + "%", segment.getValue()); } Collections.reverse(reverseSegments); for (PathElement segment : reverseSegments) { nameTemplate = nameTemplate.replace("%-" + segment.getKey() + "%", segment.getValue()); } nameTemplate = nameTemplate.replaceAll("%(\\d+)", "%$1\\$s"); nameTemplate = nameTemplate.replaceAll("%(-)", "%" + args.size() + "\\$s"); nameTemplate = nameTemplate.replaceAll("%ManagedServerName", endpointName); String nameStr = String.format(nameTemplate, args.toArray()); return nameStr; }  @Override DMRNodeLocation buildLocation(String path); @Override String findWildcardMatch(DMRNodeLocation multiTargetLocation, DMRNodeLocation singleLocation); @Override boolean isMultiTarget(DMRNodeLocation location); @Override DMRNodeLocation absolutize(DMRNodeLocation base, DMRNodeLocation location); @Override boolean isParent(DMRNodeLocation parent, DMRNodeLocation child); @Override boolean matches(DMRNodeLocation query, DMRNodeLocation location); @Override String applyTemplate(String nameTemplate, DMRNodeLocation location, String endpointName);  }
@Test public void testApplyTemplateWithDuplicateKeys() { DMRLocationResolver resolver = new DMRLocationResolver(); DMRNodeLocation location = DMRNodeLocation .of("/host=master/server=server-one/subsystem=undertow/server=default-server/http-listener=default"); String endpointName = "eName"; String str; str = resolver.applyTemplate("%subsystem%", location, endpointName); Assert.assertEquals("undertow", str); str = resolver.applyTemplate("hello %subsystem%", location, endpointName); Assert.assertEquals("hello undertow", str); str = resolver.applyTemplate("%server%", location, endpointName); Assert.assertEquals("server-one", str); str = resolver.applyTemplate("hello %server%", location, endpointName); Assert.assertEquals("hello server-one", str); str = resolver.applyTemplate("%-server%", location, endpointName); Assert.assertEquals("default-server", str); str = resolver.applyTemplate("hello %-server%", location, endpointName); Assert.assertEquals("hello default-server", str); }
@Test public void testApplyTemplate() { DMRLocationResolver resolver = new DMRLocationResolver(); DMRNodeLocation location = DMRNodeLocation.of("/one=1/two=2"); String endpointName = "eName"; String str = resolver.applyTemplate("hello", location, endpointName); Assert.assertEquals("hello", str); str = resolver.applyTemplate("hello %-", location, endpointName); Assert.assertEquals("hello 2", str); str = resolver.applyTemplate("hello %1 %2", location, endpointName); Assert.assertEquals("hello one 1", str); str = resolver.applyTemplate("hello %1.%2.%3.%4.[%ManagedServerName]", location, endpointName); Assert.assertEquals("hello one.1.two.2.[eName]", str); }