Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
fix: release streams resource while loading config (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0ta1o authored Jun 28, 2020
1 parent 33a9a05 commit 2b244d9
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/main/java/com/xiaomi/infra/pegasus/client/PConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.client;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import java.util.Properties;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer;
Expand Down Expand Up @@ -52,22 +49,19 @@ public static String getBusinessConfigZkUri(String zkServers, String businessNam
// load client configuration from configPath, which could be local file path or zk path or
// resource path.
public static Properties loadConfiguration(String configPath) throws PException {
InputStream stream = null;
try {
Properties config = new Properties();
if (PConfigUtil.isZkPath(configPath)) {
config.load(new ByteArrayInputStream(PConfigUtil.loadConfigFromZK(configPath)));
stream = new ByteArrayInputStream(PConfigUtil.loadConfigFromZK(configPath));
} else if (PConfigUtil.isLocalFile(configPath)) {
config.load(
stream =
new BufferedInputStream(
new FileInputStream(configPath.substring(PConfigUtil.LOCAL_FILE_PREFIX.length()))));
new FileInputStream(configPath.substring(PConfigUtil.LOCAL_FILE_PREFIX.length())));
} else if (PConfigUtil.isResource(configPath)) {
InputStream stream =
stream =
PegasusClient.class.getResourceAsStream(
configPath.substring(PConfigUtil.RESOURCE_PREFIX.length()));
if (stream == null) {
throw new PException("config resource not found: " + configPath);
}
config.load(stream);
} else {
throw new PException(
"configPath format error, "
Expand All @@ -77,13 +71,25 @@ public static Properties loadConfiguration(String configPath) throws PException
+ "but actual configPath is "
+ configPath);
}
if (stream == null) {
throw new PException("config resource not found: " + configPath);
}
config.load(stream);
return config;
} catch (Throwable e) {
if (e instanceof PException) {
throw (PException) e;
} else {
throw new PException(e);
}
} finally {
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
throw new PException(e);
}
}
}
}

Expand Down

0 comments on commit 2b244d9

Please sign in to comment.