This is an automated email from the ASF dual-hosted git repository. wujimin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit ba4fb37b6ab8bd3a6c3d0693f295d99a94879838 Author: liubao <bi...@qq.com> AuthorDate: Sat Dec 5 11:10:58 2020 +0800 [SCB-2145]fix review comments --- .../localregistry/LocalRegistryStore.java | 12 +------- .../client/LocalServiceRegistryClientImpl.java | 34 +++++++++------------- .../client/LocalServiceRegistryClientImplTest.java | 7 +++-- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java index 98c43f4..4e6cba5 100644 --- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java +++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java @@ -118,20 +118,10 @@ public class LocalRegistryStore { while (urls.hasMoreElements()) { URL url = urls.nextElement(); - InputStream is = null; - try { - is = url.openStream(); + try (InputStream is = url.openStream()) { if (is != null) { beans.addAll(initFromData(is)); } - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - // nothing to do - } - } } } } catch (IOException e) { diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java index 202e301..f2827b1 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java @@ -51,6 +51,7 @@ import org.apache.servicecomb.serviceregistry.client.http.Holder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Charsets; import com.google.common.hash.Hashing; @@ -78,33 +79,26 @@ public class LocalServiceRegistryClientImpl implements ServiceRegistryClient { return; } - InputStream is = this.getClass().getClassLoader().getResourceAsStream(localFile); - if (is == null) { - return; + try { + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(localFile)) { + if (is == null) { + return; + } + initFromData(is); + } + } catch (IOException e) { + LOGGER.error("", e); } - - initFromData(is); } - public LocalServiceRegistryClientImpl(InputStream is) { + @VisibleForTesting + LocalServiceRegistryClientImpl(InputStream is) { initFromData(is); } - public LocalServiceRegistryClientImpl(Map<String, Object> data) { - initFromData(data); - } - private void initFromData(InputStream is) { - try { - Map<String, Object> data = YAMLUtil.yaml2Properties(is); - initFromData(data); - } finally { - try { - is.close(); - } catch (IOException e) { - LOGGER.error("", e); - } - } + Map<String, Object> data = YAMLUtil.yaml2Properties(is); + initFromData(data); } private void initFromData(Map<String, Object> data) { diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java index bcebe82..b50344b 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java @@ -49,10 +49,11 @@ public class LocalServiceRegistryClientImplTest { public ExpectedException expectedException = ExpectedException.none(); @Before - public void loadRegistryFile() { + public void loadRegistryFile() throws Exception { ClassLoader loader = Thread.currentThread().getContextClassLoader(); - InputStream is = loader.getResourceAsStream("registry.yaml"); - registryClient = new LocalServiceRegistryClientImpl(is); + try (InputStream is = loader.getResourceAsStream("registry.yaml")) { + registryClient = new LocalServiceRegistryClientImpl(is); + } } @Test