/**
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * 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.
 * </p>
 */

package com.dangdang.ddframe.job.internal.env;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import com.dangdang.ddframe.job.exception.JobException;

/**
 * 获取真实本机网络的实现类类.
 * 
 * @author zhangliang
 */
public final class RealLocalHostService implements LocalHostService {
    
    private static volatile String cachedIpAddress;
    
    @Override
    public String getIp() {
        if (null != cachedIpAddress) {
            return cachedIpAddress;
        }
        Enumeration<NetworkInterface> netInterfaces;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (final SocketException ex) {
            throw new JobException(ex);
        }
        String localIpAddress = null;
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = netInterfaces.nextElement();
            Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses();
            while (ipAddresses.hasMoreElements()) {
                InetAddress ipAddress = ipAddresses.nextElement();
                if (isPublicIpAddress(ipAddress)) {
                    String publicIpAddress = ipAddress.getHostAddress();
                    cachedIpAddress = publicIpAddress;
                    return publicIpAddress;
                }
                if (isLocalIpAddress(ipAddress)) {
                    localIpAddress = ipAddress.getHostAddress();
                }
            }
        }
        cachedIpAddress = localIpAddress;
        return localIpAddress;
    }
    
    private boolean isPublicIpAddress(final InetAddress ipAddress) {
        return !ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && -1 == ipAddress.getHostAddress().indexOf(":");
    }
    
    private boolean isLocalIpAddress(final InetAddress ipAddress) {
        return ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && -1 == ipAddress.getHostAddress().indexOf(":");
    }
    
    @Override
    public String getHostName() {
        return getLocalHost().getHostName();
    }
    
    private static InetAddress getLocalHost() {
        InetAddress result;
        try {
            result = InetAddress.getLocalHost();
        } catch (final UnknownHostException ex) {
            throw new JobException(ex);
        }
        return result;
    }
}

@@ -2421,39 +2421,38 @@
 ddress() && 
--1 == i
+!isV6I
 pAddress
 .getHostAddr
@@ -2443,37 +2443,18 @@
 ress
-.getHostAddress().indexOf(%22:%22
+(ipAddress
 );%0A 
@@ -2617,13 +2617,131 @@
 ) &&
+ !isV6IpAddress(ipAddress);%0A    %7D%0A    %0A    private boolean isV6IpAddress(final InetAddress ipAddress) %7B%0A        return
  -1 
-=
+!
 = ip

refactor ip v6 method