1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to you under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.hadoop.hbase.util; 18 19 import java.lang.reflect.Method; 20 import java.net.UnknownHostException; 21 22 import org.apache.hadoop.hbase.classification.InterfaceAudience; 23 24 /** 25 * Wrapper around Hadoop's DNS class to hide reflection. 26 */ 27 @InterfaceAudience.Private 28 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION", 29 justification="If exception, presume HAS_NEW_DNS_GET_DEFAULT_HOST_API false") 30 public final class DNS { 31 private static boolean HAS_NEW_DNS_GET_DEFAULT_HOST_API; 32 private static Method GET_DEFAULT_HOST_METHOD; 33 34 static { 35 try { 36 GET_DEFAULT_HOST_METHOD = org.apache.hadoop.net.DNS.class 37 .getMethod("getDefaultHost", String.class, String.class, boolean.class); 38 HAS_NEW_DNS_GET_DEFAULT_HOST_API = true; 39 } catch (Exception e) { 40 HAS_NEW_DNS_GET_DEFAULT_HOST_API = false; // FindBugs: Causes REC_CATCH_EXCEPTION. Suppressed 41 } 42 } 43 44 private DNS() {} 45 46 /** 47 * Wrapper around DNS.getDefaultHost(String, String), calling 48 * DNS.getDefaultHost(String, String, boolean) when available. 49 * 50 * @param strInterface The network interface to query. 51 * @param nameserver The DNS host name. 52 * @return The default host names associated with IPs bound to the network interface. 53 */ 54 public static String getDefaultHost(String strInterface, String nameserver) 55 throws UnknownHostException { 56 if (HAS_NEW_DNS_GET_DEFAULT_HOST_API) { 57 try { 58 // Hadoop-2.8 includes a String, String, boolean variant of getDefaultHost 59 // which properly handles multi-homed systems with Kerberos. 60 return (String) GET_DEFAULT_HOST_METHOD.invoke(null, strInterface, nameserver, true); 61 } catch (Exception e) { 62 // If we can't invoke the method as it should exist, throw an exception 63 throw new RuntimeException("Failed to invoke DNS.getDefaultHost via reflection", e); 64 } 65 } else { 66 return org.apache.hadoop.net.DNS.getDefaultHost(strInterface, nameserver); 67 } 68 } 69 }