|
19 | 19 |
|
20 | 20 | package org.apache.ranger.plugin.util; |
21 | 21 |
|
22 | | -import com.kstruct.gethostname4j.Hostname; |
23 | 22 | import org.apache.commons.lang3.StringUtils; |
24 | 23 | import org.slf4j.Logger; |
25 | 24 | import org.slf4j.LoggerFactory; |
@@ -73,7 +72,7 @@ public class RangerRESTUtils { |
73 | 72 | private static final Logger LOG = LoggerFactory.getLogger(RangerRESTUtils.class); |
74 | 73 | private static final int MAX_PLUGIN_ID_LEN = 255; |
75 | 74 |
|
76 | | - public static String hostname; |
| 75 | + public static final String hostname = getHostname(); |
77 | 76 |
|
78 | 77 | public String getPluginId(String serviceName, String appId) { |
79 | 78 | String hostName; |
@@ -143,12 +142,31 @@ public String getAppIdFromPluginId(String pluginId) { |
143 | 142 | return ret; |
144 | 143 | } |
145 | 144 |
|
146 | | - static { |
| 145 | + private static String getHostname() { |
| 146 | + String hostname = null; |
| 147 | + |
147 | 148 | try { |
148 | | - hostname = Hostname.getHostname(); |
149 | | - } catch (Exception e) { |
150 | | - LOG.error("ERROR: Unable to find hostname for the agent ", e); |
151 | | - hostname = "unknownHost"; |
| 149 | + hostname = System.getenv("HOSTNAME"); |
| 150 | + } catch (SecurityException excp) { |
| 151 | + LOG.error("Error in getting environment HOSTNAME", excp); |
152 | 152 | } |
| 153 | + |
| 154 | + if (StringUtils.isBlank(hostname)) { |
| 155 | + try { |
| 156 | + hostname = System.getenv("COMPUTERNAME"); |
| 157 | + } catch (SecurityException excp) { |
| 158 | + LOG.error("Error in getting environment COMPUTERNAME", excp); |
| 159 | + } |
| 160 | + |
| 161 | + if (StringUtils.isBlank(hostname)) { |
| 162 | + try { |
| 163 | + hostname = InetAddress.getLocalHost().getHostName(); |
| 164 | + } catch (Exception e) { |
| 165 | + LOG.error("ERROR: unable to find hostname", e); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return StringUtils.isBlank(hostname) ? "unknownHost" : hostname.trim(); |
153 | 171 | } |
154 | 172 | } |
0 commit comments