1 23 24 package org.objectweb.clif.util; 25 26 27 import org.objectweb.util.monolog.Monolog; 28 import org.objectweb.util.monolog.api.Logger; 29 import org.objectweb.util.monolog.api.BasicLevel; 30 31 import java.net.InetAddress ; 32 import java.net.NetworkInterface ; 33 import java.net.SocketException ; 34 import java.util.Vector ; 35 import java.util.Enumeration ; 36 37 38 43 abstract public class Network 44 { 45 static protected Logger log = Monolog.getMonologFactory().getLogger( 46 Network.class.getName()); 47 static protected InetAddress [] inetAddresses = null; 48 static private int lastInetAddressIndex = 0; 49 50 static { 51 try 52 { 53 Vector allAddresses = new Vector (); 54 Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); 55 while (interfaces.hasMoreElements()) 56 { 57 Enumeration addresses = 58 ((NetworkInterface )interfaces.nextElement()).getInetAddresses(); 59 while (addresses.hasMoreElements()) 60 { 61 InetAddress address = (InetAddress )addresses.nextElement(); 62 if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) 63 { 64 allAddresses.add(address); 65 } 66 } 67 } 68 inetAddresses = 69 (InetAddress [])allAddresses.toArray(new InetAddress [allAddresses.size()]); 70 } 71 catch (SocketException ex) 72 { 73 throw new Error ("Could not get a network interface on this host", ex); 74 } 75 String addresses = "available InetAddresses:"; 76 for (int i=0 ; i<inetAddresses.length ; ++i) 77 { 78 addresses += " " + inetAddresses[i]; 79 } 80 log.log(BasicLevel.INFO, addresses); 81 } 82 83 84 static public synchronized InetAddress getInetAddress() 85 { 86 if (inetAddresses == null || inetAddresses.length == 0) 87 { 88 return null; 89 } 90 else 91 { 92 if (lastInetAddressIndex == inetAddresses.length) 93 { 94 lastInetAddressIndex = 0; 95 } 96 return inetAddresses[lastInetAddressIndex++]; 97 } 98 } 99 100 101 static public InetAddress [] getInetAddresses() 102 { 103 return inetAddresses; 104 } 105 } 106 | Popular Tags |