All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nordstrom.automation.selenium.utility.NetIdentity Maven / Gradle / Ivy

Go to download

Selenium Foundation is an automation framework designed to extend and enhance the capabilities provided by Selenium (WebDriver).

There is a newer version: 28.3.1-s4
Show newest version
package com.nordstrom.automation.selenium.utility;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.regex.Pattern;

public class NetIdentity {
    
    private static final Pattern INTERNAL = Pattern.compile("(10|172\\.16|192\\.168)\\.");

    private String hostAddress;

    public NetIdentity() {
        try {
            this.hostAddress = findInternalAddress(NetworkInterface.getNetworkInterfaces());
        } catch (SocketException | UnknownHostException e) {
            this.hostAddress = "127.0.0.1";
        }
    }

    private String findInternalAddress(Enumeration interfaces) throws UnknownHostException {
        while (interfaces.hasMoreElements()) {
            NetworkInterface intrface = interfaces.nextElement();
            
            try {
                if ((intrface != null) && intrface.isUp()) {
                    Enumeration addresses = intrface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        String hostAddr = address.getHostAddress();
   
                        if (INTERNAL.matcher(hostAddr).find(0)) {
                            return hostAddr;
                        }
                    }
                }
            } catch (SocketException eaten) {
                // nothing to do here
            }
        }
        
        return InetAddress.getLocalHost().getHostAddress();
    }

    public String getHostAddress() {
        return hostAddress;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy