KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > util > Network


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

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 JavaDoc;
32 import java.net.NetworkInterface JavaDoc;
33 import java.net.SocketException JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.Enumeration JavaDoc;
36
37
38 /**
39  * This utility class provides a method to get all Inet addresses of current host,
40  * in a round-robin way.
41  * @author Bruno Dillenseger
42  */

43 abstract public class Network
44 {
45     static protected Logger log = Monolog.getMonologFactory().getLogger(
46         Network.class.getName());
47     static protected InetAddress JavaDoc[] inetAddresses = null;
48     static private int lastInetAddressIndex = 0;
49
50     static {
51         try
52         {
53             Vector JavaDoc allAddresses = new Vector JavaDoc();
54             Enumeration JavaDoc interfaces = NetworkInterface.getNetworkInterfaces();
55             while (interfaces.hasMoreElements())
56             {
57                 Enumeration JavaDoc addresses =
58                     ((NetworkInterface JavaDoc)interfaces.nextElement()).getInetAddresses();
59                 while (addresses.hasMoreElements())
60                 {
61                     InetAddress JavaDoc address = (InetAddress JavaDoc)addresses.nextElement();
62                     if (!address.isLoopbackAddress() && address.isSiteLocalAddress())
63                     {
64                         allAddresses.add(address);
65                     }
66                 }
67             }
68             inetAddresses =
69                 (InetAddress JavaDoc[])allAddresses.toArray(new InetAddress JavaDoc[allAddresses.size()]);
70         }
71         catch (SocketException JavaDoc ex)
72         {
73             throw new Error JavaDoc("Could not get a network interface on this host", ex);
74         }
75         String JavaDoc 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 JavaDoc 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 JavaDoc[] getInetAddresses()
102     {
103         return inetAddresses;
104     }
105 }
106
Popular Tags