Package systeminformationmonitor.system

Source Code of systeminformationmonitor.system.NetInterface

/*
*  Copyright 2009 Leo Xu.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*  under the License.
*/
package systeminformationmonitor.system;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import systeminformationmonitor.swing.model.NetInfoComboBoxModel;
import systeminformationmonitor.system.object.NetInterfaceObject;

/**
* NetInterface class generates all information regarding network adapters
* @author Leo Xu
*/
public class NetInterface extends AbstractSystemUpdater {

    private NetInfoComboBoxModel comboModel;

    public NetInterface() {
        comboModel = NetInfoComboBoxModel.getInstance();
    }

    @Override
    public void update() {
        Vector<String> rowData = new Vector<String>();
        for (NetInterfaceObject obj : getNetInfoList()) {
            rowData.add(obj.getName());
        }

        threadLock.lock();
        try {
            comboModel.setRowData(rowData);
        } finally {
            threadLock.unlock();
        }
    }

    private List<NetInterfaceObject> getNetInfoList() {
        List<NetInterfaceObject> netList = new Vector<NetInterfaceObject>();

        try {
            Enumeration networks = NetworkInterface.getNetworkInterfaces();
            for (Object netint : Collections.list(networks)) {
                NetworkInterface netInterface = (NetworkInterface) netint;
                NetInterfaceObject netObj = new NetInterfaceObject();

                // get the name of the interface
                netObj.setName(netInterface.getDisplayName());

                if (netInterface.getInterfaceAddresses().size() >= 1) {
                    netList.add(netObj);
                }
            }
        } catch (SocketException ex) {
            Logger.getLogger(
                    NetInterface.class.getName()).log(Level.SEVERE, null, ex);
        }

        return netList;
    }

    public static NetInterfaceObject getNetInfoObject(String interfaceName) {
        NetInterfaceObject netObj = new NetInterfaceObject();

        try {
            Enumeration networks = NetworkInterface.getNetworkInterfaces();
            for (Object netint : Collections.list(networks)) {
                NetworkInterface netInterface = (NetworkInterface) netint;

                if (interfaceName.equals(netInterface.getDisplayName())) {
                    // get the name of the interface
                    netObj.setName(netInterface.getDisplayName());
                    netObj.setIsVirtual(netInterface.isVirtual());

                    try {
                        // get the MAC address of the interface
                        netObj.setHardwareAddress(
                                netInterface.getHardwareAddress());
                        // test if the interface is up and running
                        netObj.setIsUp(netInterface.isUp());
                        netObj.setIsLoopback(netInterface.isLoopback());
                    } catch (SocketException ex) {
                        Logger.getLogger(
                                NetInterface.class.getName()).log(
                                Level.SEVERE, null, ex);
                    }

                    // get the IP addresses of this interface
                    Vector<String> ipVector = new Vector<String>();
                    Vector<String> hostNameVector = new Vector<String>();
                    Enumeration ipAddress = netInterface.getInetAddresses();
                    for (Object ipAdd : Collections.list(ipAddress)) {
                        InetAddress ip = (InetAddress) ipAdd;
                        ipVector.add(ip.getHostAddress());
                        hostNameVector.add(ip.getCanonicalHostName());
                    }

                    if (!ipVector.isEmpty()) {
                        // add the IP address
                        netObj.setIpAddress(ipVector.lastElement());
                    }
                    if (!hostNameVector.isEmpty()) {
                        // add the host name
                        netObj.setHostName(hostNameVector.lastElement());
                    }
                }
            }
        } catch (SocketException ex) {
            Logger.getLogger(NetInterface.class.getName()).log(
                    Level.SEVERE, null, ex);
        }

        return netObj;
    }
}
TOP

Related Classes of systeminformationmonitor.system.NetInterface

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.