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

patterntesting.runtime.net.Localhost Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/*
 * $Id: Localhost.java,v 1.4 2011/07/09 21:43:22 oboehm Exp $
 *
 * Copyright (c) 2010 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 30.01.2010 by oliver ([email protected])
 */

package patterntesting.runtime.net;

import java.net.*;
import java.util.*;

import org.slf4j.*;

/**
 * This (static) class represents your local host. You can ask it for the
 * local address and other things.
 *
 * @author oliver
 * @since 1.0 (30.01.2010)
 */
public final class Localhost {

    private static final Logger log = LoggerFactory.getLogger(Localhost.class);

    private static final Collection inetAddresses = new ArrayList();

    /**
     * We don't expect that the network addresses changes during the lifetime
     * of the application. So we identify it only once at the start time of
     * this class.
     */
    static {
        try {
            Enumeration networkInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    inetAddresses.add(address);
                }
            }
        } catch (SocketException e) {
            log.debug("can't get local NetworkInterfaces (" + e + ")");
        }
    }

    /** This is utility class which needs not to be instantiated. */
    private Localhost() {}

    /**
     * Returns all local IP addresses. If you want to get the local host name
     * use {@link InetAddress#getLocalHost()}.
     *
     * @return a list of the host addresses
     */
    public static Collection getInetAddresses() {
        return inetAddresses;
    }

    /**
     * Here we try to get all network addresses to compare it against the
     * given hosts. The host can be given as hostname (e.g. "localhost") or
     * as IP address (e.g. "127.0.0.1").
     *
     * @param hosts an array of hosts
     * @return true if matches one of the given hosts.
     */
    public static boolean matches(final String[] hosts) {
        for (int i = 0; i < hosts.length; i++) {
            if (matches(hosts[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * Here we try to get all network addresses to compare it against the given
     * hosts.
     *
     * @param host
     *            either IP address (e.g. "127.0.0.1") or hostname (e.g.
     *            "localhost")
     * @return true if matches the given host
     */
    public static boolean matches(final String host) {
        for (Iterator iterator = inetAddresses.iterator(); iterator.hasNext();) {
            InetAddress address = iterator.next();
            if (host.equals(address.getHostAddress())
                    || host.equals(address.getHostName())) {
                return true;
            }
        }
        return false;
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy