View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.util;
20  
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  
24  /**
25   *  Utility class for obtaining the local host.
26   *
27   *  @author Alberto Montresor
28   *  @author Hein Meling
29   *  @since Jgroup 1.2
30   */
31  public class Network
32  {
33  
34    /////////////////////////////////////////////////////////////////////////////
35    // Static fields/block
36    /////////////////////////////////////////////////////////////////////////////
37  
38    /** <CODE>InetAddress</CODE> of the local host. */
39    private static InetAddress local = null;
40  
41    /** Integer version of the local host. */
42    private static int packedLocal = 0;
43  
44    static {
45      try {
46        local = InetAddress.getLocalHost();
47      } catch (UnknownHostException e) { }
48      if (local == null) {
49        String name = System.getProperty("jgroup.hostname");
50        if (name != null && name.length() > 0) {
51          try {
52            local = InetAddress.getByName(name);
53          } catch (UnknownHostException e) { }
54        }
55      }
56      if (local == null)
57        Abort.exit("Unable to detect local host");
58      System.out.println("localhost: "+ getLocalHostName());
59      packedLocal = translate(local);
60    }
61  
62  
63    /////////////////////////////////////////////////////////////////////////////
64    // Static methods
65    /////////////////////////////////////////////////////////////////////////////
66  
67    /**
68     *  Returns the internet address of the local host.
69     */
70    public static InetAddress getLocalHost()
71    {
72      return local;
73    }
74  
75  
76    /**
77     *  Returns the hostname of the local host; <i>may</i> include the
78     *  fully qualified domain name.
79     */
80    public static String getLocalHostName()
81    {
82      return local.getHostName();
83    }
84  
85  
86    /**
87     *  Returns the machine name of the local host; <i>not</i> including
88     *  the fully qualified domain name.
89     */
90    public static String getLocalMachineName()
91    {
92      return getMachineName(local.getHostName());
93    }
94  
95  
96    /**
97     *  Returns the machine name of the given host; <i>not</i> including
98     *  the fully qualified domain name.
99     */
100   public static String getMachineName(String hostName)
101   {
102     int machEndIndex = hostName.indexOf('.');
103     if (machEndIndex == -1)
104       return hostName;
105     else
106       return hostName.substring(0, machEndIndex);
107   }
108 
109 
110   /**
111    *  Returns the domain name of the local host; <i>not</i> including
112    *  the machine name part.
113    */
114   public static String getLocalDomainName()
115   {
116     String hostName = local.getHostName();
117     int machEndIndex = hostName.indexOf('.');
118     if (machEndIndex == -1)
119       return "";
120     else
121       return hostName.substring(machEndIndex+1);
122   }
123 
124 
125   /**
126    *  Returns true if the specified <code>address</code> is the local
127    *  host; false is returned otherwise.
128    */
129   public static boolean isLocal(InetAddress address)
130   {
131     return local.equals(address);
132   }
133 
134   /**
135    *  Returns true if the specified <code>packedAddress</code> is the local
136    *  host; false is returned otherwise.
137    */
138   public static boolean isLocal(int packedAddress)
139   {
140     return packedAddress == packedLocal;
141   }
142 
143 
144   /**
145    *  Returns an integer representation of the specified
146    *  <CODE>InetAddress</CODE>.  This is used for efficiency.
147    */
148   public static int translate(InetAddress address)
149   {
150     byte[] ad = address.getAddress();
151     if (ad.length != 4) 
152       throw new IllegalArgumentException("Jgroup accepts only 32-bit IP addresses");
153     int packedAddress =
154       ((ad[0] << 24) & 0xFF000000) |
155       ((ad[1] << 16) & 0xFF0000) |
156       ((ad[2] <<  8) & 0xFF00) |
157       (ad[3] & 0xFF);
158     return packedAddress;
159   }
160 
161 
162   /**
163    *  Returns an <code>InetAddress</code> representation of the
164    *  specified integer.  This is used for performance improvements in
165    *  serialization.
166    *
167    *  @param packed
168    *     An integer representation of an internet address.
169    *  @return 
170    *     The corresponding <code>InetAddress</code> representation.
171    *  @exception UnknownHostException 
172    *     If the specified integer does not correspond to a known host.
173    */
174   public static InetAddress translate(int packed)
175     throws UnknownHostException
176   {
177     int[] ad = new int[4];
178     ad[0] = (packed  >>> 24);
179     ad[1] = ((packed >>> 16) & 0x000000FF);
180     ad[2] = ((packed >>>  8) & 0x000000FF);
181     ad[3] = (packed          & 0x000000FF);
182     StringBuilder buf = new StringBuilder();
183     buf.append(ad[0]);
184     buf.append(".");
185     buf.append(ad[1]);
186     buf.append(".");
187     buf.append(ad[2]);
188     buf.append(".");
189     buf.append(ad[3]);
190     return InetAddress.getByName(buf.toString());
191   }
192 
193 
194   /**
195    *  Returns an integer representation of the specified
196    *  <CODE>String</CODE> IP-address.  This is used for efficiency.
197    */
198   public static int translateToInt(String address)
199     throws UnknownHostException
200   {
201     return translate(InetAddress.getByName(address));
202   }
203 
204 
205   /**
206    *  Returns a <code>String</code> representation of the IP-address for
207    *  the specified integer.
208    *
209    *  @param packed
210    *     An integer representation of an internet address.
211    *  @return 
212    *     The corresponding <code>InetAddress</code> representation.
213    */
214   public static String translateToString(int packed)
215   {
216     int[] ad = new int[4];
217     ad[0] = (packed  >>> 24);
218     ad[1] = ((packed >>> 16) & 0x000000FF);
219     ad[2] = ((packed >>>  8) & 0x000000FF);
220     ad[3] = (packed          & 0x000000FF);
221     StringBuilder buf = new StringBuilder();
222     buf.append(ad[0]);
223     buf.append(".");
224     buf.append(ad[1]);
225     buf.append(".");
226     buf.append(ad[2]);
227     buf.append(".");
228     buf.append(ad[3]);
229     return buf.toString();
230   }
231 
232 } // END Network