com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

Source

/******************************************************************************
 * Copyright(c) 2005-2007 SoftAMIS (http://www.soft-amis.com)                 *
 * All Rights Reserved.                                                       *
 *                                                                            *
 * 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.                                             *
 ******************************************************************************/

package com.zz.cluster4spring.localinfo;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.MessageFormat;
import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Default implementation of <code>LocalNetworkInfoProvider</code>
 * 
 * @author Andrew Sazonov
 * @version 1.0
 */
public class DefaultLocalNetworkInfoProvider implements LocalNetworkInfoProvider {
    private static final Log fLog = LogFactory.getLog(DefaultLocalNetworkInfoProvider.class);

    public DefaultLocalNetworkInfoProvider() {
    }

    public InetAddress getLocalAddress() throws UnknownHostException {
        InetAddress result = InetAddress.getLocalHost();
        return result;
    }

    public byte[] getLocalAddressRaw() {
        byte[] result = null;
        try {
            InetAddress localHost = getLocalAddress();
            result = localHost.getAddress();
        } catch (UnknownHostException ex) {
            if (fLog.isErrorEnabled()) {
                fLog.error("Unable to determine local host address. " + ex);
            }
        }
        return result;

    }

    public String getLocalHostAddress() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        String result = localHost.getHostAddress();
        return result;
    }

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

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

    public void collectInterfaceAddresses(NetworkInterface aNetworkInterface, List<InetAddress> aResult,
            InetAddressAcceptor aAcceptor) {
        for (Enumeration<InetAddress> addresses = aNetworkInterface.getInetAddresses(); addresses
                .hasMoreElements();) {
            InetAddress address = addresses.nextElement();
            if (aAcceptor.acceptInetAddress(aNetworkInterface, address)) {
                if (fLog.isInfoEnabled()) {
                    String hostAddress = address.getHostAddress();
                    fLog.info(MessageFormat.format("Found ACCEPTED IP:{0}", hostAddress));
                }
                aResult.add(address);
            } else {
                if (fLog.isInfoEnabled()) {
                    String hostAddress = address.getHostAddress();
                    fLog.info(MessageFormat.format("Found SKIPPED IP:{0}", hostAddress));
                }
            }
        }
    }

    public List<String> getLocalHostAddresses(InetAddressAcceptor aAcceptor) throws SocketException {
        List<String> result = new ArrayList<String>();
        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));
                }
                collectInterfaceIPs(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;
    }

    public static void collectInterfaceIPs(NetworkInterface aNetworkInterface, List<String> aResult,
            InetAddressAcceptor aAcceptor) {
        for (Enumeration<InetAddress> addresses = aNetworkInterface.getInetAddresses(); addresses
                .hasMoreElements();) {
            InetAddress address = addresses.nextElement();
            if (aAcceptor.acceptInetAddress(aNetworkInterface, address)) {
                String hostAddress = address.getHostAddress();
                if (fLog.isInfoEnabled()) {
                    fLog.info(MessageFormat.format("Found ACCEPTED IP:{0}", hostAddress));
                }
                aResult.add(hostAddress);
            } else if (fLog.isInfoEnabled()) {
                String hostAddress = address.getHostAddress();
                fLog.info(MessageFormat.format("Found SKIPPED IP:{0}", hostAddress));
            }
        }
    }
}