/** * Creates an InetAddress representing the local host. The checked exception <code> * java.lang.UnknownHostException</code> is captured and results in an Assertion failure instead. * * @return InetAddress instance representing the local host */ public static InetAddress createLocalHost() { try { return SocketCreator.getLocalHost(); } catch (java.net.UnknownHostException e) { logStackTrace(e); Assert.assertTrue(false, "Failed to get local host"); return null; // will never happen } }
/** * Validates the host by making sure it can successfully be used to get an instance of * InetAddress. If the host string is null, empty or would result in <code> * java.lang.UnknownHostException</code> then null is returned. * * <p>Any leading slashes on host will be ignored. * * @param host string version the InetAddress * @return the host converted to InetAddress instance */ public static String validateHost(String host) { if (host == null || host.length() == 0) { return null; } try { InetAddress.getByName(trimLeadingSlash(host)); return host; } catch (java.net.UnknownHostException e) { logStackTrace(e); return null; } }
/** * Converts the string host to an instance of InetAddress. Returns null if the string is empty. * Fails Assertion if the conversion would result in <code>java.lang.UnknownHostException</code>. * * <p>Any leading slashes on host will be ignored. * * @param host string version the InetAddress * @return the host converted to InetAddress instance */ public static InetAddress toInetAddress(String host) { if (host == null || host.length() == 0) { return null; } try { if (host.indexOf("/") > -1) { return InetAddress.getByName(host.substring(host.indexOf("/") + 1)); } else { return InetAddress.getByName(host); } } catch (java.net.UnknownHostException e) { logStackTrace(e); Assert.assertTrue(false, "Failed to get InetAddress: " + host); return null; // will never happen since the Assert will fail } }