/*
 * UG Name Util for Syndicate
 */
package SyndicateHadoop.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import org.apache.hadoop.conf.Configuration;

/**
 *
 * @author iychoi
 */
public class UGNameUtil {
    
    private static String getIPAddress() throws SocketException {
        Enumeration e = NetworkInterface.getNetworkInterfaces();
        while(e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration ee = n.getInetAddresses();
            while(ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                
                if(i instanceof Inet4Address) {
                    if(i.isLoopbackAddress())
                        continue;
                    if(i.isMulticastAddress())
                        continue;
                    if(i.isSiteLocalAddress())
                        continue;
                
                    return i.getHostAddress();
                }
            }
        }
        
        return null;
    }
    
    public static String getUGName(String prefix) {
        String address = null;
        
        try {
            address = getIPAddress();
        } catch (SocketException ex) {}
        
        if(address == null || address.isEmpty()) {
            return null;
        } else {
            if(prefix == null)
                return address.replaceAll("\\.", "_");
            else
                return prefix + address.replaceAll("\\.", "_");
        }
    }
}

@@ -220,53 +220,8 @@
 ion;
-%0Aimport org.apache.hadoop.conf.Configuration;
 %0A%0A/*
@@ -308,48 +308,185 @@
 ing 
-getIPAddress() throws SocketException %7B%0A
+ipAddress = null;%0A    private static boolean ipAddressChecked = false;%0A    %0A    private static String getIPAddress() %7B%0A        if(!ipAddressChecked) %7B%0A            try %7B%0A        
     
@@ -542,24 +542,32 @@
 terfaces();%0A
+        
         whil
@@ -567,16 +567,17 @@
    while
+ 
 (e.hasMo
@@ -604,16 +604,24 @@
         
+  
+      
 NetworkI
@@ -673,32 +673,40 @@
 ();%0A            
+      
+  
 Enumeration ee =
@@ -740,21 +740,30 @@
         
+        
 while
+ 
 (ee.hasM
@@ -795,16 +795,24 @@
         
+        
 InetAddr
@@ -843,39 +843,33 @@
 .nextElement();%0A
-       
+%0A
          %0A      
@@ -853,33 +853,32 @@
 nt();%0A%0A         
-%0A
                 
@@ -876,19 +876,19 @@
         
- 
 if
+ 
 (i insta
@@ -925,26 +925,35 @@
             
+   
+     
 if
+ 
 (i.isLoopbac
@@ -955,33 +955,39 @@
 opbackAddress())
-%0A
+ %7B%0A    
                 
@@ -986,32 +986,36 @@
                 
+    
 continue;%0A      
@@ -1000,32 +1000,70 @@
       continue;%0A
+                            %7D%0A        
                 
@@ -1064,24 +1064,25 @@
           if
+ 
 (i.isMultica
@@ -1085,32 +1085,34 @@
 ticastAddress())
+ %7B
 %0A               
@@ -1112,32 +1112,40 @@
                 
+        
 continue;%0A      
@@ -1130,32 +1130,70 @@
       continue;%0A
+                            %7D%0A        
                 
@@ -1198,16 +1198,17 @@
       if
+ 
 (i.isSit
@@ -1223,17 +1223,25 @@
 dress())
-%0A
+ %7B%0A      
         
@@ -1248,32 +1248,34 @@
                 
+  
 continue;%0A      
@@ -1288,73 +1288,445 @@
     
-%0A                    return i.getHostAddress();%0A                %7D
+            %7D%0A%0A                            // get first ipaddress%0A                            ipAddress = i.getHostAddress();%0A                            ipAddressChecked = true;%0A                            return ipAddress;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D catch (SocketException ex) %7B%0A                ipAddress = null;%0A                ipAddressChecked = true;%0A                return null;
 %0A   
@@ -1766,28 +1766,33 @@
      return 
-null
+ipAddress
 ;%0A    %7D%0A    
@@ -1887,100 +1887,33 @@
     
-%0A        try %7B%0A            address = getIPAddress();%0A        %7D catch (SocketException ex) %7B%7D
+address = getIPAddress();
 %0A   

Store local IP address for next call