Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handling of hostnames in static mode #2

Open
wants to merge 1 commit into from

1 participant

@mtritschler

Fixes a problem when InetAddress.getLocalHost() does not return the public IP but rather the localhost (i.e. 127.0.0.1)

See also http://groups.google.com/group/s4-project/browse_thread/thread/649b81cbf40e9c2d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2011
  1. Fixed hostname handling

    brlnguy authored
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 4 deletions.
  1. +19 −4 s4-comm/src/main/java/io/s4/comm/file/StaticTaskManager.java
View
23 s4-comm/src/main/java/io/s4/comm/file/StaticTaskManager.java
@@ -25,6 +25,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.net.InetAddress;
+import java.net.NetworkInterface;
import java.nio.channels.FileLock;
import java.util.HashMap;
import java.util.HashSet;
@@ -161,6 +162,21 @@ private String createLockFileName(Map<String, String> processConfig) {
return lockFileName;
}
}
+
+ private boolean isAddressValid(InetAddress inetAddress) {
+ boolean result = false;
+ try {
+ if (InetAddress.getLocalHost().equals(inetAddress)) {
+ result = true;
+ } else {
+ result = NetworkInterface.getByInetAddress(inetAddress) != null;
+ }
+ } catch (Exception e) {
+ logger.warn(e.getMessage(), e);
+ result = false;
+ }
+ return result;
+ }
private boolean canTakeupProcess(Map<String, String> processConfig) {
String host = processConfig.get("process.host");
@@ -168,10 +184,9 @@ private boolean canTakeupProcess(Map<String, String> processConfig) {
InetAddress inetAddress = InetAddress.getByName(host);
logger.info("Host Name: "
+ InetAddress.getLocalHost().getCanonicalHostName());
- if (!host.equals("localhost")) {
- if (!InetAddress.getLocalHost().equals(inetAddress)) {
- return false;
- }
+ if (!isAddressValid(inetAddress)) {
+ logger.error(host + " is not a valid address for this machine. Check the configuration.");
+ return false;
}
} catch (Exception e) {
logger.error("Invalid host:" + host);
Something went wrong with that request. Please try again.