Browse Source

Deprecate static methods in InetUtils

pull/120/head
Dave Syer 9 years ago
parent
commit
ff59dc8a42
  1. 16
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/HostInfoEnvironmentPostProcessor.java
  2. 9
      spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java
  3. 49
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/HostInfoEnvironmentPostProcessorTests.java

16
spring-cloud-commons/src/main/java/org/springframework/cloud/client/HostInfoEnvironmentPostProcessor.java

@ -3,9 +3,13 @@ package org.springframework.cloud.client; @@ -3,9 +3,13 @@ package org.springframework.cloud.client;
import java.util.LinkedHashMap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
@ -27,7 +31,7 @@ public class HostInfoEnvironmentPostProcessor @@ -27,7 +31,7 @@ public class HostInfoEnvironmentPostProcessor
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
InetUtils.HostInfo hostInfo = InetUtils.getFirstNonLoopbackHostInfo();
InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("spring.cloud.client.hostname", hostInfo.getHostname());
map.put("spring.cloud.client.ipAddress", hostInfo.getIpAddress());
@ -35,4 +39,14 @@ public class HostInfoEnvironmentPostProcessor @@ -35,4 +39,14 @@ public class HostInfoEnvironmentPostProcessor
"springCloudClientHostInfo", map);
environment.getPropertySources().addLast(propertySource);
}
private HostInfo getFirstNonLoopbackHostInfo(ConfigurableEnvironment environment) {
InetUtilsProperties target = new InetUtilsProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(target,
InetUtilsProperties.PREFIX);
binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));
try (InetUtils utils = new InetUtils(target)) {
return utils.findFirstNonLoopbackHostInfo();
}
}
}

9
spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java

@ -162,11 +162,20 @@ public class InetUtils implements Closeable { @@ -162,11 +162,20 @@ public class InetUtils implements Closeable {
/**
* Find the first non-loopback host info. If there were errors return a host info with
* 'localhost' and '127.0.0.1' for hostname and ipAddress respectively.
*
* @deprecated use the non-static findFirstNonLoopbackHostInfo() instead
*/
@Deprecated
public static HostInfo getFirstNonLoopbackHostInfo() {
return instance.findFirstNonLoopbackHostInfo();
}
/**
* Convert an internet address to a HostInfo.
*
* @deprecated use the non-static convertAddress() instead
*/
@Deprecated
public static HostInfo convert(final InetAddress address) {
return instance.convertAddress(address);
}

49
spring-cloud-commons/src/test/java/org/springframework/cloud/client/HostInfoEnvironmentPostProcessorTests.java

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.client;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class HostInfoEnvironmentPostProcessorTests {
private HostInfoEnvironmentPostProcessor processor = new HostInfoEnvironmentPostProcessor();
private ConfigurableEnvironment environment = new StandardEnvironment();
@Test
public void hostname() {
this.processor.postProcessEnvironment(this.environment,
new SpringApplication(""));
assertNotNull(this.environment.getProperty("spring.cloud.client.hostname"));
}
@Test
public void ipAddress() {
this.processor.postProcessEnvironment(this.environment,
new SpringApplication(""));
String address = this.environment.getProperty("spring.cloud.client.ipAddress");
assertNotNull(address);
}
}
Loading…
Cancel
Save