Example usage for java.net NetworkInterface getName

List of usage examples for java.net NetworkInterface getName

Introduction

In this page you can find the example usage for java.net NetworkInterface getName.

Prototype

public String getName() 

Source Link

Document

Get the name of this network interface.

Usage

From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

public List<InetAddress> getLocalAddresses(InetAddressAcceptor aAcceptor) throws SocketException {
    List<InetAddress> result = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        if (aAcceptor.acceptNetworkInterface(networkInterface)) {
            if (fLog.isInfoEnabled()) {
                String name = networkInterface.getName();
                String displayName = networkInterface.getDisplayName();
                fLog.info(MessageFormat.format(
                        "Discovered NetworkInterface - ACCEPTED. Name:[{0}]. Display Name:[{1}]", name,
                        displayName));/*from  w  w w  .  ja v  a2 s .c o  m*/
            }
            collectInterfaceAddresses(networkInterface, result, aAcceptor);
        } else {
            if (fLog.isInfoEnabled()) {
                String name = networkInterface.getName();
                String displayName = networkInterface.getDisplayName();
                fLog.info(MessageFormat.format(
                        "Discovered NetworkInterface - SKIPPED. Name:[{0}]. Display Name:[{1}]", name,
                        displayName));
            }
        }
    }
    return result;
}

From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

public List<NetworkInterface> getNetworkInterfaces(InetAddressAcceptor aAcceptor) throws SocketException {
    List<NetworkInterface> result = new ArrayList<NetworkInterface>();
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        if (aAcceptor.acceptNetworkInterface(networkInterface)) {
            if (fLog.isInfoEnabled()) {
                String name = networkInterface.getName();
                String displayName = networkInterface.getDisplayName();
                fLog.info(MessageFormat.format(
                        "Discovered NetworkInterface - ACCEPTED. Name:[{0}]. Display Name:[{1}]", name,
                        displayName));//w  ww  .  j ava2  s .  co  m
            }
            result.add(networkInterface);
        } else {
            if (fLog.isInfoEnabled()) {
                String name = networkInterface.getName();
                String displayName = networkInterface.getDisplayName();
                fLog.info(MessageFormat.format(
                        "Discovered NetworkInterface - SKIPPED. Name:[{0}]. Display Name:[{1}]", name,
                        displayName));
            }
        }
    }
    return result;
}

From source file:org.openbaton.nfvo.system.SystemStartup.java

@Override
public void run(String... args) throws Exception {
    log.info("Initializing OpenBaton");

    log.debug(Arrays.asList(args).toString());

    propFileLocation = propFileLocation.replace("file:", "");
    log.debug("Property file: " + propFileLocation);

    InputStream is = new FileInputStream(propFileLocation);
    Properties properties = new Properties();
    properties.load(is);//from w w w. j  av a  2  s. c  o  m

    log.debug("Config Values are: " + properties.values());

    Configuration c = new Configuration();

    c.setName("system");
    c.setConfigurationParameters(new HashSet<ConfigurationParameter>());

    /**
     * Adding properties from file
     */
    for (Entry<Object, Object> entry : properties.entrySet()) {
        ConfigurationParameter cp = new ConfigurationParameter();
        cp.setConfKey((String) entry.getKey());
        cp.setValue((String) entry.getValue());
        c.getConfigurationParameters().add(cp);
    }

    /**
     * Adding system properties
     */
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        ConfigurationParameter cp = new ConfigurationParameter();
        log.trace("Display name: " + netint.getDisplayName());
        log.trace("Name: " + netint.getName());
        cp.setConfKey("ip-" + netint.getName().replaceAll("\\s", ""));
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            if (inetAddress.getHostAddress().contains(".")) {
                log.trace("InetAddress: " + inetAddress.getHostAddress());
                cp.setValue(inetAddress.getHostAddress());
            }
        }
        log.trace("");
        c.getConfigurationParameters().add(cp);
    }

    configurationRepository.save(c);

    if (installPlugin) {
        startPlugins(pluginDir);
    }
}

From source file:org.trafodion.rest.util.NetworkConfiguration.java

public NetworkConfiguration(Configuration conf) throws Exception {

    this.conf = conf;
    ia = InetAddress.getLocalHost();

    String dnsInterface = conf.get(Constants.REST_DNS_INTERFACE, Constants.DEFAULT_REST_DNS_INTERFACE);
    if (dnsInterface.equalsIgnoreCase("default")) {
        intHostAddress = extHostAddress = ia.getHostAddress();
        canonicalHostName = ia.getCanonicalHostName();
        LOG.info("Using local host [" + canonicalHostName + "," + extHostAddress + "]");
    } else {/*from   w  w w. j a v a2 s  .  c  om*/
        // For all nics get all hostnames and addresses   
        // and try to match against rest.dns.interface property 
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        while (nics.hasMoreElements() && !matchedInterface) {
            InetAddress inet = null;
            NetworkInterface ni = nics.nextElement();
            LOG.info("Found interface [" + ni.getDisplayName() + "]");
            if (dnsInterface.equalsIgnoreCase(ni.getDisplayName())) {
                LOG.info("Matched specified interface [" + ni.getName() + "]");
                inet = getInetAddress(ni);
                getCanonicalHostName(ni, inet);
            } else {
                Enumeration<NetworkInterface> subIfs = ni.getSubInterfaces();
                for (NetworkInterface subIf : Collections.list(subIfs)) {
                    LOG.debug("Sub Interface Display name [" + subIf.getDisplayName() + "]");
                    if (dnsInterface.equalsIgnoreCase(subIf.getDisplayName())) {
                        LOG.info("Matched subIf [" + subIf.getName() + "]");
                        inet = getInetAddress(subIf);
                        getCanonicalHostName(subIf, inet);
                        break;
                    }
                }
            }
        }
    }

    if (!matchedInterface)
        checkCloud();
}

From source file:com.sixt.service.framework.registry.consul.RegistrationManager.java

private void updateIpAddress() {
    try {//from ww w  .jav a2  s.  c o m
        Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
        ipAddress = null;
        while (b.hasMoreElements()) {
            NetworkInterface iface = b.nextElement();
            if (iface.getName().startsWith("dock")) {
                continue;
            }
            for (InterfaceAddress f : iface.getInterfaceAddresses()) {
                if (f.getAddress().isSiteLocalAddress()) {
                    ipAddress = f.getAddress().getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
}

From source file:org.openhab.binding.network.internal.utils.NetworkUtils.java

/**
 * Get a set of all interface names./* www  .ja va2  s . com*/
 *
 * @return Set of interface names
 */
public Set<String> getInterfaceNames() {
    Set<String> result = new HashSet<>();

    try {
        // For each interface ...
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface networkInterface = en.nextElement();
            if (!networkInterface.isLoopback()) {
                result.add(networkInterface.getName());
            }
        }
    } catch (SocketException ignored) {
        // If we are not allowed to enumerate, we return an empty result set.
    }

    return result;
}

From source file:com.entertailion.android.slideshow.utils.Utils.java

public static final InetAddress getLocalInetAddress() {
    InetAddress selectedInetAddress = null;
    try {//from  w w w.j a  v a  2  s .c  om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            if (intf.isUp()) {
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        if (inetAddress instanceof Inet4Address) { // only
                            // want
                            // ipv4
                            // address
                            if (inetAddress.getHostAddress().toString().charAt(0) != '0') {
                                if (selectedInetAddress == null) {
                                    selectedInetAddress = inetAddress;
                                } else if (intf.getName().startsWith("eth")) { // prefer
                                    // wired
                                    // interface
                                    selectedInetAddress = inetAddress;
                                }
                            }
                        }
                    }
                }
            }
        }
        return selectedInetAddress;
    } catch (Throwable e) {
        Log.e(LOG_TAG, "Failed to get the IP address", e);
    }
    return null;
}

From source file:org.sakuli.actions.environment.CipherUtil.java

/**
 * fetch the local network interfaceLog and reads out the MAC of the chosen encryption interface.
 * Must be called before the methods {@link #encrypt(String)} or {@link #decrypt(String)}.
 *
 * @throws SakuliCipherException for wrong interface names and MACs.
 *///from  ww  w. j a  v  a2  s  . c om
@PostConstruct
public void scanNetworkInterfaces() throws SakuliCipherException {
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        interfaceName = checkEthInterfaceName();
        networkInterfaces = getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface anInterface = networkInterfaces.nextElement();
            if (anInterface.getHardwareAddress() != null) {
                interfaceLog = interfaceLog + "\nNET-Interface " + anInterface.getIndex() + " - Name: "
                        + anInterface.getName() + "\t MAC: " + formatMAC(anInterface.getHardwareAddress())
                        + "\t VirtualAdapter: " + anInterface.isVirtual() + "\t Loopback: "
                        + anInterface.isLoopback() + "\t Desc.: " + anInterface.getDisplayName();
            }
            if (anInterface.getName().equals(interfaceName)) {
                macOfEncryptionInterface = anInterface.getHardwareAddress();
            }

        }
        if (macOfEncryptionInterface == null) {
            throw new SakuliCipherException(
                    "Cannot resolve MAC address ... please check your config of the property: "
                            + ActionProperties.ENCRYPTION_INTERFACE + "=" + interfaceName,
                    interfaceLog);
        }
    } catch (Exception e) {
        throw new SakuliCipherException(e, interfaceLog);
    }
}

From source file:com.adito.setup.forms.SystemInfoForm.java

/**
 * Get a list (as strings) of the nwrok interfaces that are available
 * on this host./*from  ww w  . j  a  va  2 s. c  o  m*/
 *   
 * TODO This should be the interfaces that Adito is using.
 *   
 * @return network interfaces
 */
public List getNetworkInterfaces() {
    Enumeration e = null;
    try {
        List niList = new ArrayList();
        e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface netface = (NetworkInterface) e.nextElement();
            Enumeration e2 = netface.getInetAddresses();
            while (e2.hasMoreElements()) {
                InetAddress ip = (InetAddress) e2.nextElement();
                niList.add(netface.getName() + "=" + ip.toString());
            }
        }
        return niList;
    } catch (SocketException e1) {
        return new ArrayList();
    }
}

From source file:org.psit.transwatcher.TransWatcher.java

private String getBroadcastIP() {
    String myIP = null;//  w ww .  j a v  a  2s  .c  o  m

    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements() && myIP == null) {
            NetworkInterface current = interfaces.nextElement();
            notifyMessage(current.toString());
            notifyMessage("Name: " + current.getName());
            if (!current.isUp() || current.isLoopback() || current.isVirtual()
                    || !current.getName().startsWith("wl"))
                continue;
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress current_addr = addresses.nextElement();
                if (current_addr.isLoopbackAddress())
                    continue;
                if (current_addr instanceof Inet4Address) {
                    myIP = current_addr.getHostAddress();
                    break;
                }
            }
        }
    } catch (Exception exc) {
        notifyMessage("Error determining network interfaces:\n");
    }

    if (myIP != null) {
        // broadcast for IPv4
        StringTokenizer st = new StringTokenizer(myIP, ".");
        StringBuffer broadcastIP = new StringBuffer();
        // hate that archaic string puzzle
        broadcastIP.append(st.nextElement());
        broadcastIP.append(".");
        broadcastIP.append(st.nextElement());
        broadcastIP.append(".");
        broadcastIP.append(st.nextElement());
        broadcastIP.append(".");
        broadcastIP.append("255");
        return broadcastIP.toString();
    }

    return null;
}