Skip to content

Commit

Permalink
reverted adding IOException to NetworkService, return null if cannot …
Browse files Browse the repository at this point in the history
…resolve name and log
  • Loading branch information
Paul_Loy authored and kimchy committed Jun 15, 2011
1 parent 64ffd66 commit e1514aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public static interface CustomNameResolver {
/**
* Resolves the default value if possible. If not, return <tt>null</tt>.
*/
InetAddress resolveDefault() throws IOException;
InetAddress resolveDefault();

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

private final List<CustomNameResolver> customNameResolvers = new CopyOnWriteArrayList<CustomNameResolver>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.net.InetAddress;
import java.net.URL;

import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.network.NetworkService.CustomNameResolver;

/**
Expand All @@ -33,7 +35,7 @@
* <p />
* Valid config values for {@link Ec2HostnameType}s are -
* <ul>
* <li>_ec2 - maps to privateIpv4</li>
* <li>_ec2_ - maps to privateIpv4</li>
* <li>_ec2:privateIp_ - maps to privateIpv4</li>
* <li>_ec2:privateIpv4_</li>
* <li>_ec2:privateDns_</li>
Expand Down Expand Up @@ -73,40 +75,47 @@ private Ec2HostnameType(String configName, String ec2Name) {
}

private static final String EC2_METADATA_URL = "http://169.254.169.254/latest/meta-data/";
private final ESLogger logger;

/**
* Construct a {@link CustomNameResolver} with the given {@link Ec2HostnameType}
* address type.
* Construct a {@link CustomNameResolver}.
*
* @param addressType the type of ec2 host to bind to.
*/
public Ec2NameResolver() {
logger = Loggers.getLogger(getClass());
}

/**
* @param type the ec2 hostname type to discover.
* @return the appropriate host resolved from ec2 meta-data.
* @throws IOException if ec2 meta-data cannot be obtained.
*
* @see CustomNameResolver#resolveIfPossible(String)
*/
public InetAddress resolve(Ec2HostnameType type) throws IOException {

URL url = new URL(EC2_METADATA_URL + type.ec2Name);
BufferedReader urlReader = new BufferedReader(new InputStreamReader(url.openStream()));

String metadataResult = urlReader.readLine();
if (metadataResult == null || metadataResult.length() == 0) {
throw new IOException("no ec2 metadata returned from :" + url);
public InetAddress resolve(Ec2HostnameType type) {
try {
URL url = new URL(EC2_METADATA_URL + type.ec2Name);
logger.info("obtaining ec2 hostname from ec2 meta-data url {}", url);
BufferedReader urlReader = new BufferedReader(new InputStreamReader(url.openStream()));

String metadataResult = urlReader.readLine();
if (metadataResult == null || metadataResult.length() == 0) {
logger.error("no ec2 metadata returned from {}", url);
return null;
}
return InetAddress.getByName(metadataResult);
} catch (IOException e) {
logger.error("exception obtaining metadata", e);
return null;
}
return InetAddress.getByName(metadataResult);
}

/*
* (non-Javadoc)
* @see org.elasticsearch.common.network.NetworkService.CustomNameResolver#resolveDefault()
*/
@Override
public InetAddress resolveDefault() throws IOException {
public InetAddress resolveDefault() {
return resolve(Ec2HostnameType.DEFAULT);
}

Expand All @@ -115,7 +124,7 @@ public InetAddress resolveDefault() throws IOException {
* @see org.elasticsearch.common.network.NetworkService.CustomNameResolver#resolveIfPossible(java.lang.String)
*/
@Override
public InetAddress resolveIfPossible(String value) throws IOException {
public InetAddress resolveIfPossible(String value) {
for (Ec2HostnameType type : Ec2HostnameType.values()) {
if (type.configName.equals(value)) {
return resolve(type);
Expand Down

0 comments on commit e1514aa

Please sign in to comment.