AgentUtils { public static String findIpAddress( String networkInterface ) { String ipAddress = null; Logger logger = Logger.getLogger( AgentUtils.class.getName()); try { NetworkInterface nif; if( networkInterface != null && ! AgentConstants.DEFAULT_NETWORK_INTERFACE.equalsIgnoreCase( networkInterface ) && (nif = NetworkInterface.getByName( networkInterface )) != null ) { Enumeration<InetAddress> addrs = nif.getInetAddresses(); while( addrs.hasMoreElements() && (ipAddress == null || ipAddress.startsWith( "127.0" ))) { Object obj = addrs.nextElement(); if( !( obj instanceof Inet4Address )) continue; ipAddress = obj.toString(); if( ipAddress.startsWith( "/" )) ipAddress = ipAddress.substring( 1 ); } } else if( ! AgentConstants.DEFAULT_NETWORK_INTERFACE.equalsIgnoreCase( networkInterface )) { logger.severe( "Network interface " + networkInterface + " does not exists. The host's default IP will be picked up." ); } if( ipAddress == null ) { logger.fine( "Picking up the host's default IP address." ); ipAddress = InetAddress.getLocalHost().getHostAddress(); } } catch( Exception e ) { ipAddress = "127.0.0.1"; logger.warning( "The IP address could not be found. " + e.getMessage()); Utils.logException( logger, e ); } logger.info( "The agent's address was resolved to " + ipAddress ); return ipAddress; } private  AgentUtils(); static boolean isValidIP( String ip ); static void copyInstanceResources( Instance instance, Map<String,byte[]> fileNameToFileContent ); static void executeScriptResources( File scriptsDir ); static void deleteInstanceResources( Instance instance ); static void changeRoboconfLogLevel( String logLevel, String etcDir ); static Map<String,byte[]> collectLogs( String karafData ); static String findIpAddress( String networkInterface ); static void injectConfigurations(
			String karafEtc,
			String applicationName,
			String scopedInstancePath,
			String domain,
			String ipAddress ); static final String INJECTED_CONFIGS_DIR; }
@Test public void testFindIpAddress_invalidNetworkInterface() throws Exception { String expectedIp = InetAddress.getLocalHost().getHostAddress(); Assert.assertEquals( expectedIp, AgentUtils.findIpAddress( "invalid-network" )); }
@Test public void testFindIpAddress_validNetworkInterface() throws Exception { NetworkInterface nif = NetworkInterface.getByName( "eth0" ); if( nif != null ) Assert.assertNotNull( AgentUtils.findIpAddress( "eth0" )); }
@Test public void testFindIpAddress_nullNetworkInterface() throws Exception { String expectedIp = InetAddress.getLocalHost().getHostAddress(); Assert.assertEquals( expectedIp, AgentUtils.findIpAddress( null )); }
@Test public void testFindIpAddress_nforceDefaultNetworkInterface() throws Exception { String expectedIp = InetAddress.getLocalHost().getHostAddress(); Assert.assertEquals( expectedIp, AgentUtils.findIpAddress( AgentConstants.DEFAULT_NETWORK_INTERFACE )); }