Example usage for java.net NetworkInterface isVirtual

List of usage examples for java.net NetworkInterface isVirtual

Introduction

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

Prototype

public boolean isVirtual() 

Source Link

Document

Returns whether this interface is a virtual interface (also called subinterface).

Usage

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

private String getBroadcastIP() {
    String myIP = null;/*from ww  w .  j  a v  a2s . com*/

    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;
}

From source file:com.cloud.utils.net.NetUtils.java

public static InetAddress[] getAllLocalInetAddresses() {
    final List<InetAddress> addrList = new ArrayList<InetAddress>();
    try {/*from  w ww . j  a  v  a 2 s  .co  m*/
        for (final NetworkInterface ifc : IteratorUtil
                .enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual()) {
                for (final InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) {
                    addrList.add(addr);
                }
            }
        }
    } catch (final SocketException e) {
        s_logger.warn("SocketException in getAllLocalInetAddresses().", e);
    }

    final InetAddress[] addrs = new InetAddress[addrList.size()];
    if (addrList.size() > 0) {
        System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size());
    }
    return addrs;
}

From source file:com.cloud.utils.net.NetUtils.java

public static InetAddress[] getInterfaceInetAddresses(final String ifName) {
    final List<InetAddress> addrList = new ArrayList<InetAddress>();
    try {//w w w .  j  a v a 2  s. c  om
        for (final NetworkInterface ifc : IteratorUtil
                .enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual() && ifc.getName().equals(ifName)) {
                for (final InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) {
                    addrList.add(addr);
                }
            }
        }
    } catch (final SocketException e) {
        s_logger.warn("SocketException in getAllLocalInetAddresses().", e);
    }

    final InetAddress[] addrs = new InetAddress[addrList.size()];
    if (addrList.size() > 0) {
        System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size());
    }
    return addrs;
}

From source file:com.cloud.utils.net.NetUtils.java

public static String[] getLocalCidrs() {
    final String defaultHostIp = getDefaultHostIp();

    final List<String> cidrList = new ArrayList<String>();
    try {// w  w w.  j  ava 2s .  c  o m
        for (final NetworkInterface ifc : IteratorUtil
                .enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
                for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
                    final InetAddress addr = address.getAddress();
                    final int prefixLength = address.getNetworkPrefixLength();
                    if (prefixLength < MAX_CIDR && prefixLength > 0) {
                        final String ip = ipFromInetAddress(addr);
                        if (ip.equalsIgnoreCase(defaultHostIp)) {
                            cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
                        }
                    }
                }
            }
        }
    } catch (final SocketException e) {
        s_logger.warn("UnknownHostException in getLocalCidrs().", e);
    }

    return cidrList.toArray(new String[0]);
}

From source file:fr.inria.ucn.collectors.NetworkStateCollector.java

private JSONArray getIfconfig(Map<String, JSONObject> stats) throws JSONException {
    JSONArray ifaces = new JSONArray();

    // make sure the stats is read
    networkStats();//w  ww.j a v a 2 s  .  c om

    Enumeration<NetworkInterface> en = null;
    try {
        en = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        Log.d(Constants.LOGTAG, "failed to list interfaces", e);
    }

    if (en != null) {
        while (en.hasMoreElements()) {
            NetworkInterface intf = en.nextElement();

            JSONObject iface = new JSONObject();
            iface.put("display_name", intf.getDisplayName());
            iface.put("name", intf.getName());
            iface.put("is_virtual", intf.isVirtual());
            iface.put("stats", stats.get(intf.getName()));

            try {
                iface.put("mtu", intf.getMTU());
                iface.put("is_loopback", intf.isLoopback());
                iface.put("is_ptop", intf.isPointToPoint());
                iface.put("is_up", intf.isUp());
            } catch (SocketException e) {
                Log.d(Constants.LOGTAG, "failed to read interface data", e);
            }

            JSONArray ips = new JSONArray();
            List<InterfaceAddress> ilist = intf.getInterfaceAddresses();
            for (InterfaceAddress ia : ilist) {
                ips.put(ia.getAddress().getHostAddress());
            }
            iface.put("addresses", ips);

            ifaces.put(iface);
        }
    } else {
        for (String name : stats.keySet()) {
            JSONObject iface = new JSONObject();
            iface.put("name", name);
            iface.put("stats", stats.get(name));
            ifaces.put(iface);
        }
    }

    return ifaces;
}

From source file:com.portfolio.data.attachment.FileServlet.java

@Override
public void init(ServletConfig config) {
    /// List possible local address
    try {//from w  w w. j  av a2 s.c  o m
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();
            if (!current.isUp() || current.isLoopback() || current.isVirtual())
                continue;
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress current_addr = addresses.nextElement();
                if (current_addr instanceof Inet4Address)
                    ourIPs.add(current_addr.getHostAddress());
            }
        }
    } catch (Exception e) {
    }

    servContext = config.getServletContext();
    backend = config.getServletContext().getInitParameter("backendserver");
    server = config.getServletContext().getInitParameter("fileserver");
    try {
        String dataProviderName = config.getInitParameter("dataProviderClass");
        dataProvider = (DataProvider) Class.forName(dataProviderName).newInstance();

        InitialContext cxt = new InitialContext();
        if (cxt == null) {
            throw new Exception("no context found!");
        }

        /// Init this here, might fail depending on server hosting
        ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/portfolio-backend");
        if (ds == null) {
            throw new Exception("Data  jdbc/portfolio-backend source not found!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}