Skip to content

Commit

Permalink
[SCB-2145]fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored and wujimin committed Dec 6, 2020
1 parent 839a52e commit ba4fb37
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
Expand Up @@ -118,20 +118,10 @@ private List<RegistryBean> 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) {
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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) {
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit ba4fb37

Please sign in to comment.