Skip to content

Commit

Permalink
Cleanup/fix logic around custom resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
rmuir committed Aug 17, 2015
1 parent bd7065f commit c7b9f3a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
Expand Up @@ -38,7 +38,8 @@
*/
public class NetworkService extends AbstractComponent {

public static final String LOCAL = "#local#";
/** By default, we bind to loopback interfaces */
public static final String DEFAULT_NETWORK_HOST = "_local_";

private static final String GLOBAL_NETWORK_HOST_SETTING = "network.host";
private static final String GLOBAL_NETWORK_BINDHOST_SETTING = "network.bind_host";
Expand Down Expand Up @@ -68,12 +69,12 @@ public static interface CustomNameResolver {
/**
* Resolves the default value if possible. If not, return <tt>null</tt>.
*/
InetAddress resolveDefault();
InetAddress[] resolveDefault();

/**
* Resolves a custom value handling, return <tt>null</tt> if can't handle it.
*/
InetAddress resolveIfPossible(String value);
InetAddress[] resolveIfPossible(String value);
}

private final List<CustomNameResolver> customNameResolvers = new CopyOnWriteArrayList<>();
Expand All @@ -91,59 +92,58 @@ public void addCustomNameResolver(CustomNameResolver customNameResolver) {
customNameResolvers.add(customNameResolver);
}


public InetAddress[] resolveBindHostAddress(String bindHost) throws IOException {
return resolveBindHostAddress(bindHost, "_local_");
}

private InetAddress[] resolveBindHostAddress(String bindHost, String defaultValue2) throws IOException {
/** TODO: move this leniency out */
// first check settings
if (bindHost == null) {
bindHost = settings.get(GLOBAL_NETWORK_BINDHOST_SETTING, settings.get(GLOBAL_NETWORK_HOST_SETTING));
}
// next check any registered custom resolvers
if (bindHost == null) {
for (CustomNameResolver customNameResolver : customNameResolvers) {
InetAddress addresses[] = customNameResolver.resolveDefault();
if (addresses != null) {
return addresses;
}
}
}
// finally, fill with our default
if (bindHost == null) {
bindHost = defaultValue2;
bindHost = DEFAULT_NETWORK_HOST;
}
return resolveInetAddress(bindHost);
}

// TODO: needs to be InetAddress[]
public InetAddress resolvePublishHostAddress(String publishHost) throws IOException {
InetAddress address = resolvePublishHostAddress(publishHost, "_local_");
return address;
}

private InetAddress resolvePublishHostAddress(String publishHost, String defaultValue2) throws IOException {
/** TODO: move this leniency out */
// first check settings
if (publishHost == null) {
publishHost = settings.get(GLOBAL_NETWORK_PUBLISHHOST_SETTING, settings.get(GLOBAL_NETWORK_HOST_SETTING));
}
// next check any registered custom resolvers
if (publishHost == null) {
publishHost = defaultValue2;
for (CustomNameResolver customNameResolver : customNameResolvers) {
InetAddress addresses[] = customNameResolver.resolveDefault();
if (addresses != null) {
return addresses[0];
}
}
}
// finally, fill with our default
if (publishHost == null) {
publishHost = DEFAULT_NETWORK_HOST;
}
// TODO: allow publishing multiple addresses
return resolveInetAddress(publishHost)[0];
}

private InetAddress[] resolveInetAddress(String host) throws UnknownHostException, IOException {
if (host == null) {
// TODO: what is this null host business????
for (CustomNameResolver customNameResolver : customNameResolvers) {
InetAddress inetAddress = customNameResolver.resolveDefault();
if (inetAddress != null) {
// TODO: change CustomNameResolver to return array.
return new InetAddress[] { inetAddress };
}
}
return null;
}
if ((host.startsWith("#") && host.endsWith("#")) || (host.startsWith("_") && host.endsWith("_"))) {
host = host.substring(1, host.length() - 1);
// allow custom resolvers to have special names
for (CustomNameResolver customNameResolver : customNameResolvers) {
InetAddress inetAddress = customNameResolver.resolveIfPossible(host);
if (inetAddress != null) {
// TODO: change CustomNameResolver to return array.
return new InetAddress[] { inetAddress };
InetAddress addresses[] = customNameResolver.resolveIfPossible(host);
if (addresses != null) {
return addresses;
}
}
switch (host) {
Expand Down
Expand Up @@ -93,7 +93,7 @@ public Ec2NameResolver(Settings settings) {
* @throws IOException if ec2 meta-data cannot be obtained.
* @see CustomNameResolver#resolveIfPossible(String)
*/
public InetAddress resolve(Ec2HostnameType type, boolean warnOnFailure) {
public InetAddress[] resolve(Ec2HostnameType type, boolean warnOnFailure) {
URLConnection urlConnection = null;
InputStream in = null;
try {
Expand All @@ -109,7 +109,8 @@ public InetAddress resolve(Ec2HostnameType type, boolean warnOnFailure) {
logger.error("no ec2 metadata returned from {}", url);
return null;
}
return InetAddress.getByName(metadataResult);
// only one address: because we explicitly ask for only one via the Ec2HostnameType
return new InetAddress[] { InetAddress.getByName(metadataResult) };
} catch (IOException e) {
if (warnOnFailure) {
logger.warn("failed to get metadata for [" + type.configName + "]: " + ExceptionsHelper.detailedMessage(e));
Expand All @@ -123,13 +124,13 @@ public InetAddress resolve(Ec2HostnameType type, boolean warnOnFailure) {
}

@Override
public InetAddress resolveDefault() {
public InetAddress[] resolveDefault() {
return null; // using this, one has to explicitly specify _ec2_ in network setting
// return resolve(Ec2HostnameType.DEFAULT, false);
}

@Override
public InetAddress resolveIfPossible(String value) {
public InetAddress[] resolveIfPossible(String value) {
for (Ec2HostnameType type : Ec2HostnameType.values()) {
if (type.configName.equals(value)) {
return resolve(type, true);
Expand Down

0 comments on commit c7b9f3a

Please sign in to comment.