2 * Copyright 2016 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.commontosca.catalog.model.common;
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
30 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
31 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Input;
32 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Plan;
33 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.ServiceTemplate;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.esotericsoftware.yamlbeans.YamlConfig;
38 import com.esotericsoftware.yamlbeans.YamlException;
39 import com.esotericsoftware.yamlbeans.YamlReader;
41 public class TemplateUtils {
42 private static final Logger logger = LoggerFactory.getLogger(TemplateUtils.class);
44 public static Map<String, Plan> loadPlan(String yamlString) throws CatalogResourceException {
45 ServiceTemplate st = loadServiceTemplate(yamlString);
52 * @throws CatalogResourceException
54 public static ServiceTemplate loadServiceTemplate(String yamlString) throws CatalogResourceException {
55 if (yamlString == null || yamlString.isEmpty()) {
56 return new ServiceTemplate();
58 final YamlReader reader = new YamlReader(yamlString);
59 adjustConfig(reader.getConfig());
61 return reader.read(ServiceTemplate.class);
62 } catch (final YamlException e) {
63 throw new CatalogResourceException("Load plan information failed.", e);
68 } catch (IOException e) {
78 private static void adjustConfig(YamlConfig config) {
79 config.setPropertyElementType(ServiceTemplate.class, "plans", Plan.class);
80 config.setPropertyElementType(Plan.class, "inputs", Input.class);
88 * @throws CatalogResourceException
90 public static String readStringFromZipFile(String zipFileName, String zipEntryName) throws CatalogResourceException {
91 String[] lines = readFromZipFile(zipFileName, zipEntryName);
92 StringBuffer sb = new StringBuffer();
93 for (String line : lines) {
94 sb.append(line).append(System.lineSeparator());
101 * @param zipEntryName
103 * @throws CatalogResourceException
105 public static String[] readFromZipFile(String zipFileName, String zipEntryName)
106 throws CatalogResourceException {
108 InputStream ins = null;
110 zf = new ZipFile(zipFileName);
111 ZipEntry ze = getZipEntryZipFile(zf, zipEntryName);
114 ins = zf.getInputStream(ze);
115 return readFromInputStream(ins);
117 } catch (IOException e) {
118 throw new CatalogResourceException("readFromZipFile failed.", e);
120 closeInputStream(ins);
123 return new String[0];
126 public static ZipEntry getZipEntryZipFile(ZipFile zf, String zipEntryName) {
127 Enumeration<?> zes = zf.entries();
128 while (zes.hasMoreElements()) {
129 ZipEntry ze = (ZipEntry) zes.nextElement();
130 if (zipEntryName.equals(ze.getName())
131 || (zipEntryName.replaceAll("\\\\", "/")).equals(ze.getName())) {
142 public static void closeInputStream(InputStream ins) {
146 } catch (IOException e) {
147 logger.warn("closeInputStream failed.", e);
155 public static void closeZipFile(ZipFile zf) {
159 } catch (IOException e) {
160 logger.warn("closeZipFile failed.", e);
165 private static String[] readFromInputStream(InputStream ins) throws CatalogResourceException {
166 InputStreamReader insReader = new InputStreamReader(ins);
167 BufferedReader reader = new BufferedReader(insReader);
169 List<String> lineList = new ArrayList<>();
172 while ((line = reader.readLine()) != null) {
175 } catch (IOException e) {
176 throw new CatalogResourceException("readFromInputStream failed.", e);
179 closeReader(insReader);
182 return lineList.toArray(new String[0]);
188 private static void closeReader(Reader reader) {
189 if (reader != null) {
192 } catch (IOException e) {
193 logger.warn("closeReader failed.", e);