TargetHelpers { public static Map<String,String> expandProperties( Instance scopedInstance, Map<String,String> targetProperties ) { Logger logger = Logger.getLogger( TargetHelpers.class.getName()); String ipAddress = scopedInstance.data.get( Instance.IP_ADDRESS ); if( ipAddress == null ) ipAddress = InstanceHelpers.findRootInstance( scopedInstance ).data.get( Instance.IP_ADDRESS ); if( ipAddress == null ) ipAddress = ""; Properties params = new Properties(); params.setProperty( Constants.SPECIFIC_VARIABLE_IP, ipAddress ); Map<String,String> newTargetProperties = new HashMap<>( targetProperties.size()); for( Map.Entry<String,String> entry : targetProperties.entrySet()) { String p = Utils.expandTemplate( entry.getValue(), params ); newTargetProperties.put( entry.getKey(), p ); if( ! p.equals( entry.getValue())) logger.fine( "Target property '" + entry.getKey() + "' was expanded to " + p ); } return newTargetProperties; } private  TargetHelpers(); static void verifyTargets(
			ITargetHandlerResolver targetHandlerResolver,
			ManagedApplication ma,
			ITargetsMngr targetsMngr ); static Map<String,String> expandProperties( Instance scopedInstance, Map<String,String> targetProperties ); static String findTargetHandlerName( Properties props ); static String findTargetHandlerName( Map<String,String> props );  }
@Test public void testExpandProperties_scopedInstanceIsNotRoot() throws Exception { Map<String,String> map = new HashMap<> (); map.put( "test.ip", "{{ip}}:4243" ); final Component component = new Component( "my-vm" ).installerName( Constants.TARGET_INSTALLER ); Instance instance = new Instance( "my-scope-instance" ).component( component ); Instance rootInstance = new Instance( "root" ); InstanceHelpers.insertChild( rootInstance, instance ); rootInstance.data.put( Instance.IP_ADDRESS, "192.168.1.87" ); Map<String,String> loadedProperties = TargetHelpers.expandProperties( instance, map ); Assert.assertNotNull( loadedProperties ); Assert.assertEquals( "192.168.1.87:4243", loadedProperties.get("test.ip")); }
@Test public void testExpandProperties_scopedInstanceIsRoot() throws Exception { Map<String,String> map = new HashMap<> (); map.put( "test.ip", "{{ip}}:4243" ); final Component component = new Component( "my-vm" ).installerName( Constants.TARGET_INSTALLER ); Instance instance = new Instance( "my-scope-instance" ).component( component ); instance.data.put( Instance.IP_ADDRESS, "192.168.1.84" ); Map<String,String> loadedProperties = TargetHelpers.expandProperties( instance, map ); Assert.assertNotNull( loadedProperties ); Assert.assertEquals( "192.168.1.84:4243", loadedProperties.get("test.ip")); }
@Test public void testExpandProperties_noIp() throws Exception { Map<String,String> map = new HashMap<> (); map.put( "test.ip", "{{ip}}:4243" ); final Component component = new Component( "my-vm" ).installerName( Constants.TARGET_INSTALLER ); Instance instance = new Instance( "my-scope-instance" ).component( component ); Map<String,String> loadedProperties = TargetHelpers.expandProperties( instance, map ); Assert.assertNotNull( loadedProperties ); Assert.assertEquals( ":4243", loadedProperties.get("test.ip")); }