Search in sources :

Example 1 with InvalidAddressException

use of com.hazelcast.util.AddressUtil.InvalidAddressException in project hazelcast by hazelcast.

the class TcpIpJoiner method getPossibleAddresses.

@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity" })
protected Collection<Address> getPossibleAddresses() {
    final Collection<String> possibleMembers = getMembers();
    final Set<Address> possibleAddresses = new HashSet<Address>();
    final NetworkConfig networkConfig = config.getNetworkConfig();
    for (String possibleMember : possibleMembers) {
        AddressHolder addressHolder = AddressUtil.getAddressHolder(possibleMember);
        try {
            boolean portIsDefined = addressHolder.getPort() != -1 || !networkConfig.isPortAutoIncrement();
            int count = portIsDefined ? 1 : maxPortTryCount;
            int port = addressHolder.getPort() != -1 ? addressHolder.getPort() : networkConfig.getPort();
            AddressMatcher addressMatcher = null;
            try {
                addressMatcher = AddressUtil.getAddressMatcher(addressHolder.getAddress());
            } catch (InvalidAddressException ignore) {
                EmptyStatement.ignore(ignore);
            }
            if (addressMatcher != null) {
                final Collection<String> matchedAddresses;
                if (addressMatcher.isIPv4()) {
                    matchedAddresses = AddressUtil.getMatchingIpv4Addresses(addressMatcher);
                } else {
                    // for IPv6 we are not doing wildcard matching
                    matchedAddresses = Collections.singleton(addressHolder.getAddress());
                }
                for (String matchedAddress : matchedAddresses) {
                    addPossibleAddresses(possibleAddresses, null, InetAddress.getByName(matchedAddress), port, count);
                }
            } else {
                final String host = addressHolder.getAddress();
                final InterfacesConfig interfaces = networkConfig.getInterfaces();
                if (interfaces.isEnabled()) {
                    final InetAddress[] inetAddresses = InetAddress.getAllByName(host);
                    for (InetAddress inetAddress : inetAddresses) {
                        if (AddressUtil.matchAnyInterface(inetAddress.getHostAddress(), interfaces.getInterfaces())) {
                            addPossibleAddresses(possibleAddresses, host, inetAddress, port, count);
                        }
                    }
                } else {
                    addPossibleAddresses(possibleAddresses, host, null, port, count);
                }
            }
        } catch (UnknownHostException e) {
            logger.warning("Cannot resolve hostname '" + addressHolder.getAddress() + "'. Please make sure host is valid and reachable.");
            if (logger.isFineEnabled()) {
                logger.fine("Error during resolving possible target!", e);
            }
        }
    }
    possibleAddresses.remove(node.getThisAddress());
    return possibleAddresses;
}
Also used : InterfacesConfig(com.hazelcast.config.InterfacesConfig) Address(com.hazelcast.nio.Address) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) NetworkConfig(com.hazelcast.config.NetworkConfig) AddressMatcher(com.hazelcast.util.AddressUtil.AddressMatcher) AddressHolder(com.hazelcast.util.AddressUtil.AddressHolder) InetAddress(java.net.InetAddress) InvalidAddressException(com.hazelcast.util.AddressUtil.InvalidAddressException) HashSet(java.util.HashSet)

Aggregations

InterfacesConfig (com.hazelcast.config.InterfacesConfig)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 Address (com.hazelcast.nio.Address)1 AddressHolder (com.hazelcast.util.AddressUtil.AddressHolder)1 AddressMatcher (com.hazelcast.util.AddressUtil.AddressMatcher)1 InvalidAddressException (com.hazelcast.util.AddressUtil.InvalidAddressException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1