From ba4fb37b6ab8bd3a6c3d0693f295d99a94879838 Mon Sep 17 00:00:00 2001 From: liubao Date: Sat, 5 Dec 2020 11:10:58 +0800 Subject: [PATCH] [SCB-2145]fix review comments --- .../localregistry/LocalRegistryStore.java | 12 +------ .../LocalServiceRegistryClientImpl.java | 34 ++++++++----------- .../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 98c43f4d5b..4e6cba5554 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 @@ private List loadYamlBeans() { 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 202e301aef..f2827b1e33 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.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 LocalServiceRegistryClientImpl(String localFile) { 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 data) { - initFromData(data); - } - private void initFromData(InputStream is) { - try { - Map data = YAMLUtil.yaml2Properties(is); - initFromData(data); - } finally { - try { - is.close(); - } catch (IOException e) { - LOGGER.error("", e); - } - } + Map data = YAMLUtil.yaml2Properties(is); + initFromData(data); } private void initFromData(Map 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 bcebe82988..b50344bc81 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