InetAddressFactory.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.address;
00018 
00019 import com.google.common.collect.Lists;
00020 import com.google.common.net.InetAddresses;
00021 
00022 import org.ros.exception.RosRuntimeException;
00023 
00024 import java.net.InetAddress;
00025 import java.net.NetworkInterface;
00026 import java.net.SocketException;
00027 import java.net.UnknownHostException;
00028 import java.util.Arrays;
00029 import java.util.Collection;
00030 import java.util.Collections;
00031 import java.util.List;
00032 
00036 public class InetAddressFactory {
00037 
00038   private InetAddressFactory() {
00039     // Utility class
00040   }
00041 
00042   private static boolean isIpv4(InetAddress address) {
00043     return address.getAddress().length == 4;
00044   }
00045 
00046   private static Collection<InetAddress> getAllInetAddresses() {
00047     List<NetworkInterface> networkInterfaces;
00048     try {
00049       networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
00050     } catch (SocketException e) {
00051       throw new RosRuntimeException(e);
00052     }
00053     List<InetAddress> inetAddresses = Lists.newArrayList();
00054     for (NetworkInterface networkInterface : networkInterfaces) {
00055       try {
00056         if (networkInterface.isUp()) {
00057           inetAddresses.addAll(Collections.list(networkInterface.getInetAddresses()));
00058         }
00059       } catch (SocketException e) {
00060         throw new RosRuntimeException(e);
00061       }
00062     }
00063     return inetAddresses;
00064   }
00065 
00066   public static InetAddress newNonLoopback() {
00067     for (InetAddress address : getAllInetAddresses()) {
00068       // IPv4 only for now.
00069       if (!address.isLoopbackAddress() && isIpv4(address)) {
00070         return address;
00071       }
00072     }
00073     throw new RosRuntimeException("No non-loopback interface found.");
00074   }
00075 
00076   private static Collection<InetAddress> getAllInetAddressByName(String host) {
00077     InetAddress[] allAddressesByName;
00078     try {
00079       allAddressesByName = org.xbill.DNS.Address.getAllByName(host);
00080     } catch (UnknownHostException unused) {
00081       try {
00082         allAddressesByName = InetAddress.getAllByName(host);
00083       } catch (UnknownHostException e) {
00084         throw new RosRuntimeException(e);
00085       }
00086     }
00087     return Arrays.asList(allAddressesByName);
00088   }
00089 
00108   public static InetAddress newFromHostString(String host) {
00109     try {
00110       if (InetAddresses.isInetAddress(host)) {
00111         return InetAddress.getByAddress(host, InetAddresses.forString(host).getAddress());
00112       }
00113       if (host.equals(Address.LOCALHOST)) {
00114         return InetAddress.getByAddress(Address.LOCALHOST, InetAddresses
00115             .forString(Address.LOOPBACK).getAddress());
00116       }
00117     } catch (UnknownHostException e) {
00118       throw new RosRuntimeException(e);
00119     }
00120     Collection<InetAddress> allAddressesByName = getAllInetAddressByName(host);
00121     // First, try to find a non-loopback IPv4 address.
00122     for (InetAddress address : allAddressesByName) {
00123       if (!address.isLoopbackAddress() && isIpv4(address)) {
00124         return address;
00125       }
00126     }
00127     // Return a loopback IPv4 address as a last resort.
00128     for (InetAddress address : allAddressesByName) {
00129       if (isIpv4(address)) {
00130         return address;
00131       }
00132     }
00133     throw new RosRuntimeException("Unable to construct InetAddress for host: " + host);
00134   }
00135 
00136   public static InetAddress newLoopback() {
00137     return newFromHostString(Address.LOOPBACK);
00138   }
00139 
00140 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49