Deprecate static methods in InetUtils

This commit is contained in:
Dave Syer
2016-06-06 15:58:34 +01:00
parent 7399045706
commit ff59dc8a42
3 changed files with 73 additions and 1 deletions

View File

@@ -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
@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
"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();
}
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}