001    /*
002     * $Id: Localhost.java,v 1.4 2011/07/09 21:43:22 oboehm Exp $
003     *
004     * Copyright (c) 2010 by Oliver Boehm
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     *
018     * (c)reated 30.01.2010 by oliver (ob@oasd.de)
019     */
020    
021    package patterntesting.runtime.net;
022    
023    import java.net.*;
024    import java.util.*;
025    
026    import org.slf4j.*;
027    
028    /**
029     * This (static) class represents your local host. You can ask it for the
030     * local address and other things.
031     *
032     * @author oliver
033     * @since 1.0 (30.01.2010)
034     */
035    public final class Localhost {
036    
037        private static final Logger log = LoggerFactory.getLogger(Localhost.class);
038    
039        private static final Collection<InetAddress> inetAddresses = new ArrayList<InetAddress>();
040    
041        /**
042         * We don't expect that the network addresses changes during the lifetime
043         * of the application. So we identify it only once at the start time of
044         * this class.
045         */
046        static {
047            try {
048                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
049                        .getNetworkInterfaces();
050                while (networkInterfaces.hasMoreElements()) {
051                    NetworkInterface networkInterface = networkInterfaces.nextElement();
052                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
053                    while (addresses.hasMoreElements()) {
054                        InetAddress address = addresses.nextElement();
055                        inetAddresses.add(address);
056                    }
057                }
058            } catch (SocketException e) {
059                log.debug("can't get local NetworkInterfaces (" + e + ")");
060            }
061        }
062    
063        /** This is utility class which needs not to be instantiated. */
064        private Localhost() {}
065    
066        /**
067         * Returns all local IP addresses. If you want to get the local host name
068         * use {@link InetAddress#getLocalHost()}.
069         *
070         * @return a list of the host addresses
071         */
072        public static Collection<InetAddress> getInetAddresses() {
073            return inetAddresses;
074        }
075    
076        /**
077         * Here we try to get all network addresses to compare it against the
078         * given hosts. The host can be given as hostname (e.g. "localhost") or
079         * as IP address (e.g. "127.0.0.1").
080         *
081         * @param hosts an array of hosts
082         * @return true if matches one of the given hosts.
083         */
084        public static boolean matches(final String[] hosts) {
085            for (int i = 0; i < hosts.length; i++) {
086                if (matches(hosts[i])) {
087                    return true;
088                }
089            }
090            return false;
091        }
092    
093        /**
094         * Here we try to get all network addresses to compare it against the given
095         * hosts.
096         *
097         * @param host
098         *            either IP address (e.g. "127.0.0.1") or hostname (e.g.
099         *            "localhost")
100         * @return true if matches the given host
101         */
102        public static boolean matches(final String host) {
103            for (Iterator<InetAddress> iterator = inetAddresses.iterator(); iterator.hasNext();) {
104                InetAddress address = iterator.next();
105                if (host.equals(address.getHostAddress())
106                        || host.equals(address.getHostName())) {
107                    return true;
108                }
109            }
110            return false;
111        }
112    
113    }
114