e45d31bf5b72a74629c8997383165c483055a6fc
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vnfsdk.marketplace.filemanage.http;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 public class ToolUtil {
31   private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtil.class);
32
33   private ToolUtil() {
34   }
35
36   /**
37    * copy in directory.
38    * @param srcDirName source directory name
39    * @param destDirName destination directory name
40    * @param files file list in directory
41    * @param overlay overwrite or not
42    * @return boolean
43    * @throws IOException e
44    */
45   public static boolean copyInDirectory(String srcDirName, String destDirName, File[] files, boolean overlay)
46       throws IOException
47   {
48     for (int i = 0; i < files.length; i++) {
49         boolean flag = false;
50         if (files[i].isFile()) {
51           flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
52         }
53         if (files[i].isDirectory()) {
54           flag = copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay);
55         }
56         if (!flag) {
57           String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
58           LOGGER.error(message);
59           return false;
60         }
61       }
62       return true;
63   }
64
65   /**
66    * copy from directory.
67    * @param srcDirName source directory name
68    * @param destDirName destination directory name
69    * @param overlay overwrite or not
70    * @return boolean
71    * @throws IOException e
72    */
73   public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
74       throws IOException {
75     File srcDir = new File(srcDirName);
76     if (!srcDir.exists() || !srcDir.isDirectory()) {
77       return false;
78     }
79
80     String useDestDirName = destDirName;
81     if (!useDestDirName.endsWith(File.separator)) {
82       useDestDirName += File.separator;
83     }
84     File destDir = new File(useDestDirName);
85     if (destDir.exists() && overlay) {
86         new File(useDestDirName).delete();
87     } else if (destDir.exists() && !overlay) {
88         return false;
89     }
90
91     if (!destDir.mkdirs()) {
92         return false;
93     }
94
95     File[] files = srcDir.listFiles();
96
97     return copyInDirectory(srcDirName, destDirName, files, overlay);
98   }
99
100   /**
101    * copy byte.
102    * @param srcFileName source file name
103    * @param destFileName target file name
104    * @return boolean
105    */
106   public static boolean copyByte(File srcFile,  File destFile)
107   {
108     try (
109           InputStream in = new FileInputStream(srcFile);
110           OutputStream out = new FileOutputStream(destFile);
111       ) {
112
113       byte[] buffer = new byte[1024];
114
115       for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
116         out.write(buffer, 0, byteread);
117       }
118
119       return true;
120     } catch (IOException e) {
121       LOGGER.error("IOException in copyFile", e);
122       return false;
123     }
124   }
125
126   /**
127    * copy file.
128    * @param srcFileName source file name
129    * @param destFileName target file name
130    * @param overlay overwrite or not
131    * @return boolean
132    */
133   public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
134     File srcFile = new File(srcFileName);
135
136     if (!srcFile.exists() || !srcFile.isFile()) {
137       String message = "Source file : " + srcFileName + " not exist !";
138       LOGGER.error(message);
139       return false;
140     }
141
142     File destFile = new File(destFileName);
143     if (destFile.exists() && overlay) {
144         new File(destFileName).delete();
145     } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
146         return false;
147     }
148
149     return copyByte(srcFile,  destFile);
150   }
151
152   /**
153    * create directory.
154    * @param destDirName target directory name
155    * @return boolean
156    */
157   public static boolean createDir(String destDirName) {
158     String useDestDirName = destDirName;
159     if (!useDestDirName.endsWith(File.separator)) {
160       useDestDirName += File.separator;
161     }
162     File dir = new File(useDestDirName);
163     if (dir.exists()) {
164       dir.delete();
165     }
166
167     return dir.mkdirs();
168   }
169
170   public static String getHttpServerAbsolutePath() {
171     return Thread.currentThread().getContextClassLoader().getResource("/").getPath() +  HttpServerPathConfig.getHttpServerPath();
172   }
173
174   /**
175    * delete directory.
176    * @param dir file to delete
177    * @return boolean
178    */
179   public static boolean deleteDir(File dir) {
180     if (dir.isDirectory()) {
181       String[] children = dir.list();
182       for (int i = 0; i < children.length; i++) {
183         boolean success = deleteDir(new File(dir, children[i]));
184         if (!success) {
185           return false;
186         }
187       }
188     }
189     return dir.delete();
190   }
191
192   public static String getAppDeployPath()
193   {
194       return Thread.currentThread().getContextClassLoader().getResource("/").getPath();
195   }
196 }
197