summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
author10090474 <yuan.hu1@zte.com.cn>2016-12-12 19:38:15 +0800
committer10090474 <yuan.hu1@zte.com.cn>2016-12-12 19:38:15 +0800
commit3e702870b6e2ae56e6db8304d80bef785e8ef9c6 (patch)
treece1ff6a9a4262423a63f2d7ac44d9ab7355fd25e
parent6788304c235e3e43f6aaec76f271b5e2b4065e1b (diff)
Close all InputStreams, Readers, ZipFiles while not use.
Change-Id: I2e94d3fff246e250e3c192966a42ffdeba6f2a36 Issue-id: TOSCA-191 Signed-off-by: 10090474 <yuan.hu1@zte.com.cn>
-rw-r--r--catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/common/TemplateUtils.java111
-rw-r--r--catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java4
-rw-r--r--catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java67
3 files changed, 99 insertions, 83 deletions
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/common/TemplateUtils.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/common/TemplateUtils.java
index ffb4a4dc..697ca1ab 100644
--- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/common/TemplateUtils.java
+++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/common/TemplateUtils.java
@@ -15,17 +15,17 @@
*/
package org.openo.commontosca.catalog.model.common;
-import java.io.BufferedInputStream;
import java.io.BufferedReader;
-import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.Reader;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
-import java.util.zip.ZipInputStream;
import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Input;
@@ -69,7 +69,7 @@ public class TemplateUtils {
}
}
}
-}
+ }
/**
@@ -102,50 +102,95 @@ public class TemplateUtils {
* @return
* @throws CatalogResourceException
*/
- @SuppressWarnings("resource")
public static String[] readFromZipFile(String zipFileName, String zipEntryName)
throws CatalogResourceException {
- ZipInputStream zipIns = null;
- BufferedReader zipEntryBr = null;
+ ZipFile zf = null;
+ InputStream ins = null;
try {
- ZipFile zipFile = new ZipFile(zipFileName);
+ zf = new ZipFile(zipFileName);
+ ZipEntry ze = getZipEntryZipFile(zf, zipEntryName);
- zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
- ZipEntry zipEntry;
- while ((zipEntry = zipIns.getNextEntry()) != null) {
- if (zipEntryName.equals(zipEntry.getName())
- || (zipEntryName.replaceAll("/", "\\\\")).equals(zipEntry.getName())) {
- zipEntryBr = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
- List<String> lineList = new ArrayList<>();
- String line;
- while ((line = zipEntryBr.readLine()) != null) {
- lineList.add(line);
- }
-
- return lineList.toArray(new String[0]);
- }
+ if (ze != null) {
+ ins = zf.getInputStream(ze);
+ return readFromInputStream(ins);
}
} catch (IOException e) {
- throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
+ throw new CatalogResourceException("readFromZipFile failed.", e);
} finally {
- closeStreamAndReader(zipIns, zipEntryBr);
+ closeInputStream(ins);
+ closeZipFile(zf);
}
return new String[0];
}
+
+ public static ZipEntry getZipEntryZipFile(ZipFile zf, String zipEntryName) {
+ Enumeration<?> zes = zf.entries();
+ while (zes.hasMoreElements()) {
+ ZipEntry ze = (ZipEntry) zes.nextElement();
+ if (zipEntryName.equals(ze.getName())
+ || (zipEntryName.replaceAll("\\\\", "/")).equals(ze.getName())) {
+ return ze;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * @param ins
+ */
+ public static void closeInputStream(InputStream ins) {
+ if (ins != null) {
+ try {
+ ins.close();
+ } catch (IOException e) {
+ logger.warn("closeInputStream failed.", e);
+ }
+ }
+ }
- private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
- if (br != null) {
+ /**
+ * @param zf
+ */
+ public static void closeZipFile(ZipFile zf) {
+ if (zf != null) {
try {
- br.close();
- } catch (IOException e1) {
- logger.error("Buffered reader close failed !");
+ zf.close();
+ } catch (IOException e) {
+ logger.warn("closeZipFile failed.", e);
}
}
- if (zin != null) {
+ }
+
+ private static String[] readFromInputStream(InputStream ins) throws CatalogResourceException {
+ InputStreamReader insReader = new InputStreamReader(ins);
+ BufferedReader reader = new BufferedReader(insReader);
+
+ List<String> lineList = new ArrayList<>();
+ String line;
+ try {
+ while ((line = reader.readLine()) != null) {
+ lineList.add(line);
+ }
+ } catch (IOException e) {
+ throw new CatalogResourceException("readFromInputStream failed.", e);
+ } finally {
+ closeReader(reader);
+ closeReader(insReader);
+ }
+
+ return lineList.toArray(new String[0]);
+ }
+
+ /**
+ * @param reader
+ */
+ private static void closeReader(Reader reader) {
+ if (reader != null) {
try {
- zin.closeEntry();
- } catch (IOException e2) {
- logger.error("Zip inputStream close failed !");
+ reader.close();
+ } catch (IOException e) {
+ logger.warn("closeReader failed.", e);
}
}
}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java
index 71372c2c..8d37319c 100644
--- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java
+++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java
@@ -60,7 +60,7 @@ public class AriaModelParser extends AbstractModelParser {
logger.info("Parse begin.");
String stFileLocation = parseServiceTemplateFileName(packageId, fileLocation);
- AriaParserResult result = getAriaParserResult(packageId, fileLocation, stFileLocation);
+ AriaParserResult result = getAriaParserResult(packageId, stFileLocation);
// service template
ServiceTemplate st = parseServiceTemplate(result, packageId, stFileLocation);
@@ -302,7 +302,7 @@ public class AriaModelParser extends AbstractModelParser {
return retList.toArray(new OutputParameter[0]);
}
- private AriaParserResult getAriaParserResult(String packageId, String fileLocation, String stFileLocation) throws CatalogResourceException {
+ private AriaParserResult getAriaParserResult(String packageId, String stFileLocation) throws CatalogResourceException {
CsarFileUriResponse stDownloadUri =
PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileLocation);
return AriaParserServiceConsumer.parseCsarPackage(stDownloadUri.getDownloadUri());
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java
index 1d753744..67f9a9e3 100644
--- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java
+++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java
@@ -15,9 +15,7 @@
*/
package org.openo.commontosca.catalog.model.plan.wso2;
-import java.io.BufferedInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -26,7 +24,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
-import java.util.zip.ZipInputStream;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -38,6 +35,7 @@ import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.glassfish.jersey.client.ClientConfig;
import org.openo.commontosca.catalog.common.Config;
import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
+import org.openo.commontosca.catalog.model.common.TemplateUtils;
import org.openo.commontosca.catalog.model.plan.wso2.entity.DeletePackageResponse;
import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessRequest;
@@ -49,10 +47,9 @@ import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
import com.google.gson.Gson;
-
public class Wso2ServiceConsumer {
public static final String WSO2_APP_URL = "/openoapi/wso2bpel/v1/package";
- private static final Logger LOGGER = LoggerFactory.getLogger(Wso2ServiceConsumer.class);
+ private static final Logger logger = LoggerFactory.getLogger(Wso2ServiceConsumer.class);
/**
* deploy package.
@@ -67,9 +64,11 @@ public class Wso2ServiceConsumer {
if (ipPort == null) {
throw new CatalogResourceException("getMsbServerAddr failed.");
}
+ ZipFile zf = null;
InputStream ins = null;
try {
- ins = getInputStream(zipFileLocation, planFilePath);
+ zf = new ZipFile(zipFileLocation);
+ ins = getInputStream(zf, planFilePath);
RestResponse res = RestfulClient.post(
ipPort.getIp(), ipPort.getPort(), WSO2_APP_URL,
buildRequest(ins, planFilePath));
@@ -88,17 +87,14 @@ public class Wso2ServiceConsumer {
}
throw new CatalogResourceException(
- "Deploy Package return fail. Response = " + res.getResult());
- } catch (FileNotFoundException e1) {
- throw new CatalogResourceException("Deploy Package failed.", e1);
+ "Deploy Package return failed. Response = " + res.getResult());
+ } catch (FileNotFoundException e) {
+ throw new CatalogResourceException("buildRequest failed.", e);
+ } catch (IOException e) {
+ throw new CatalogResourceException("ZipFile failed.", e);
} finally {
- if (ins != null) {
- try {
- ins.close();
- } catch (IOException e1) {
- LOGGER.error("inputStream close failed !");
- }
- }
+ TemplateUtils.closeInputStream(ins);
+ TemplateUtils.closeZipFile(zf);
}
}
@@ -128,39 +124,14 @@ public class Wso2ServiceConsumer {
return builder.build();
}
- @SuppressWarnings("resource")
- private static InputStream getInputStream(String zipFileLocation, String planFilePath)
- throws CatalogResourceException {
- ZipInputStream zin = null;
- try {
- InputStream in = new BufferedInputStream(new FileInputStream(zipFileLocation));
- zin = new ZipInputStream(in);
- ZipEntry ze;
- while ((ze = zin.getNextEntry()) != null) {
- if (planFilePath.equals(ze.getName())) {
- ZipFile zf = new ZipFile(zipFileLocation);
- return zf.getInputStream(ze);
- }
- }
- } catch (IOException e1) {
- throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath,
- e1);
- } finally {
- closeStream(zin);
- }
-
- throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath);
- }
-
- private static void closeStream(ZipInputStream zin) {
-
- if (zin != null) {
- try {
- zin.closeEntry();
- } catch (IOException e1) {
- LOGGER.error("zip inputStream close failed !");
- }
+ private static InputStream getInputStream(ZipFile zf, String planFilePath)
+ throws CatalogResourceException, IOException {
+ ZipEntry ze = TemplateUtils.getZipEntryZipFile(zf, planFilePath);
+
+ if (ze == null) {
+ throw new CatalogResourceException("Can't file plan file in the csar. planFilePath = " + planFilePath);
}
+ return zf.getInputStream(ze);
}
/**