Skip to content
This repository

Handling of hostnames in static mode #2

Open
wants to merge 1 commit into from

1 participant

mtritschler
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

Showing 1 unique commit by 1 author.

May 14, 2011
brlnguy Fixed hostname handling 2478b8d
This page is out of date. Refresh to see the latest.
23 s4-comm/src/main/java/io/s4/comm/file/StaticTaskManager.java
@@ -25,6 +25,7 @@
25 25 import java.io.File;
26 26 import java.io.FileOutputStream;
27 27 import java.net.InetAddress;
  28 +import java.net.NetworkInterface;
28 29 import java.nio.channels.FileLock;
29 30 import java.util.HashMap;
30 31 import java.util.HashSet;
@@ -161,6 +162,21 @@ private String createLockFileName(Map<String, String> processConfig) {
161 162 return lockFileName;
162 163 }
163 164 }
  165 +
  166 + private boolean isAddressValid(InetAddress inetAddress) {
  167 + boolean result = false;
  168 + try {
  169 + if (InetAddress.getLocalHost().equals(inetAddress)) {
  170 + result = true;
  171 + } else {
  172 + result = NetworkInterface.getByInetAddress(inetAddress) != null;
  173 + }
  174 + } catch (Exception e) {
  175 + logger.warn(e.getMessage(), e);
  176 + result = false;
  177 + }
  178 + return result;
  179 + }
164 180
165 181 private boolean canTakeupProcess(Map<String, String> processConfig) {
166 182 String host = processConfig.get("process.host");
@@ -168,10 +184,9 @@ private boolean canTakeupProcess(Map<String, String> processConfig) {
168 184 InetAddress inetAddress = InetAddress.getByName(host);
169 185 logger.info("Host Name: "
170 186 + InetAddress.getLocalHost().getCanonicalHostName());
171   - if (!host.equals("localhost")) {
172   - if (!InetAddress.getLocalHost().equals(inetAddress)) {
173   - return false;
174   - }
  187 + if (!isAddressValid(inetAddress)) {
  188 + logger.error(host + " is not a valid address for this machine. Check the configuration.");
  189 + return false;
175 190 }
176 191 } catch (Exception e) {
177 192 logger.error("Invalid host:" + host);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.