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