432ee219842b87b663074f921836b2e6732ddb54
[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(destDirName).delete();
87     } else if ((destDir.exists() && !overlay) || (!destDir.exists() && !destDir.mkdirs())) {
88         return false;
89     }
90
91     File[] files = srcDir.listFiles();
92
93     return copyInDirectory(srcDirName, destDirName, files, overlay);
94   }
95
96   /**
97    * copy byte.
98    * @param srcFileName source file name
99    * @param destFileName target file name
100    * @return boolean
101    */
102   public static boolean copyByte(File srcFile,  File destFile)
103   {
104     try (
105           InputStream in = new FileInputStream(srcFile);
106           OutputStream out = new FileOutputStream(destFile);
107       ) {
108
109       byte[] buffer = new byte[1024];
110       int byteread = 0;
111
112       while ((byteread = in.read(buffer)) != -1) {
113         out.write(buffer, 0, byteread);
114       }
115       return true;
116     } catch (IOException e) {
117       LOGGER.error("IOException in copyFile", e);
118       return false;
119     }
120   }
121
122   /**
123    * copy file.
124    * @param srcFileName source file name
125    * @param destFileName target file name
126    * @param overlay overwrite or not
127    * @return boolean
128    */
129   public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
130     File srcFile = new File(srcFileName);
131
132     if (!srcFile.exists() || !srcFile.isFile()) {
133       String message = "Source file : " + srcFileName + " not exist !";
134       LOGGER.error(message);
135       return false;
136     }
137
138     File destFile = new File(destFileName);
139     if (destFile.exists() && overlay) {
140         new File(destFileName).delete();
141     } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
142         return false;
143     }
144
145     return copyByte(srcFile,  destFile);
146   }
147
148   /**
149    * create directory.
150    * @param destDirName target directory name
151    * @return boolean
152    */
153   public static boolean createDir(String destDirName) {
154     String useDestDirName = destDirName;
155     if (!useDestDirName.endsWith(File.separator)) {
156       useDestDirName += File.separator;
157     }
158     File dir = new File(useDestDirName);
159     if (dir.exists()) {
160       dir.delete();
161     }
162
163     return dir.mkdirs();
164   }
165
166   public static String getHttpServerAbsolutePath() {
167     return Thread.currentThread().getContextClassLoader().getResource("/").getPath() +  HttpServerPathConfig.getHttpServerPath();
168   }
169
170   /**
171    * delete directory.
172    * @param dir file to delete
173    * @return boolean
174    */
175   public static boolean deleteDir(File dir) {
176     if (dir.isDirectory()) {
177       String[] children = dir.list();
178       for (int i = 0; i < children.length; i++) {
179         boolean success = deleteDir(new File(dir, children[i]));
180         if (!success) {
181           return false;
182         }
183       }
184     }
185     return dir.delete();
186   }
187
188   public static String getAppDeployPath()
189   {
190       return Thread.currentThread().getContextClassLoader().getResource("/").getPath();
191   }
192 }
193