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.BufferedInputStream;
 
  19 import java.io.BufferedReader;
 
  21 import java.io.FileInputStream;
 
  22 import java.io.IOException;
 
  23 import java.io.InputStreamReader;
 
  24 import java.util.ArrayList;
 
  25 import java.util.List;
 
  27 import java.util.zip.ZipEntry;
 
  28 import java.util.zip.ZipFile;
 
  29 import java.util.zip.ZipInputStream;
 
  31 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
 
  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);
 
  87    * @throws CatalogResourceException
 
  89   public static String readStringFromZipFile(String zipFileName, String zipEntryName) throws CatalogResourceException {
 
  90     String[] lines = readFromZipFile(zipFileName, zipEntryName);
 
  91     StringBuffer sb = new StringBuffer();
 
  92     for (String line : lines) {
 
  93       sb.append(line).append(System.lineSeparator());
 
 100    * @param zipEntryName
 
 102    * @throws CatalogResourceException
 
 104   @SuppressWarnings("resource")
 
 105   public static String[] readFromZipFile(String zipFileName, String zipEntryName)
 
 106       throws CatalogResourceException {
 
 107     ZipInputStream zipIns = null;
 
 108     BufferedReader zipEntryBr = null;
 
 110       ZipFile zipFile = new ZipFile(zipFileName);
 
 112       zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
 
 114       while ((zipEntry = zipIns.getNextEntry()) != null) {
 
 115         if (zipEntryName.equals(zipEntry.getName())
 
 116             || (zipEntryName.replaceAll("/", "\\\\")).equals(zipEntry.getName())) {
 
 117           zipEntryBr = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
 
 118           List<String> lineList = new ArrayList<>();
 
 120           while ((line = zipEntryBr.readLine()) != null) {
 
 124           return lineList.toArray(new String[0]);
 
 127     } catch (IOException e) {
 
 128       throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
 
 130       closeStreamAndReader(zipIns, zipEntryBr);
 
 132     return new String[0];
 
 135   private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
 
 139       } catch (IOException e1) {
 
 140         logger.error("Buffered reader close failed !");
 
 146       } catch (IOException e2) {
 
 147         logger.error("Zip inputStream close failed !");