48a7d0cabfb979611fece74492d3950b3f7829f1
[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    * simple copy 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 simpleCopyDirectory(String srcDirName, String destDirName)
46       throws IOException
47   {
48     File srcDir = new File(srcDirName);
49     File[] files = srcDir.listFiles();
50     for (int i = 0; i < files.length; i++) {
51         boolean flag = false;
52         if (files[i].isFile()) {
53           flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
54         }
55         if (files[i].isDirectory()) {
56           flag = simpleCopyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName());
57         }
58         if (!flag) {
59           String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
60           LOGGER.error(message);
61           return false;
62         }
63      }
64      return true;
65   }
66
67   /**
68    * create destDir
69    * @param destDirName destination directory name
70    * @param overlay overwrite or not
71    * @return boolean
72    */
73   public static boolean createDestDir(String destDirName, boolean overlay)
74   {
75     File destDir = new File(destDirName);
76     if (destDir.exists() && overlay) {
77         new File(destDirName).delete();
78     } else if (destDir.exists() && !overlay) {
79         return false;
80     }
81
82     return destDir.mkdirs();
83   }
84
85   /**
86    * copy from directory.
87    * @param srcDirName source directory name
88    * @param destDirName destination directory name
89    * @param overlay overwrite or not
90    * @return boolean
91    * @throws IOException e
92    */
93   public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
94       throws IOException {
95     File srcDir = new File(srcDirName);
96     if (!srcDir.exists() || !srcDir.isDirectory()) {
97       return false;
98     }
99
100     String fullDestDirName = destDirName;
101     if (!fullDestDirName.endsWith(File.separator)) {
102       fullDestDirName += File.separator;
103     }
104
105     if (!createDestDir(fullDestDirName, overlay))
106     {
107       return false;
108     }
109
110     return simpleCopyDirectory(srcDirName, fullDestDirName);
111   }
112
113   /**
114    * copy byte.
115    * @param srcFileName source file name
116    * @param destFileName target file name
117    * @return boolean
118    */
119   public static boolean copyByte(File srcFile,  File destFile)
120   {
121     try (
122           InputStream in = new FileInputStream(srcFile);
123           OutputStream out = new FileOutputStream(destFile);
124       ) {
125
126       byte[] buffer = new byte[1024];
127
128       for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
129         out.write(buffer, 0, byteread);
130       }
131
132       return true;
133     } catch (IOException e) {
134       LOGGER.error("IOException in copyFile", e);
135       return false;
136     }
137   }
138
139   /**
140    * copy file.
141    * @param srcFileName source file name
142    * @param destFileName target file name
143    * @param overlay overwrite or not
144    * @return boolean
145    */
146   public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
147     File srcFile = new File(srcFileName);
148
149     if (!srcFile.exists() || !srcFile.isFile()) {
150       String message = "Source file : " + srcFileName + " not exist !";
151       LOGGER.error(message);
152       return false;
153     }
154
155     File destFile = new File(destFileName);
156     if (destFile.exists() && overlay) {
157         new File(destFileName).delete();
158     } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
159         return false;
160     }
161
162     return copyByte(srcFile,  destFile);
163   }
164
165   /**
166    * create directory.
167    * @param destDirName target directory name
168    * @return boolean
169    */
170   public static boolean createDir(String destDirName) {
171     String useDestDirName = destDirName;
172     if (!useDestDirName.endsWith(File.separator)) {
173       useDestDirName += File.separator;
174     }
175     File dir = new File(useDestDirName);
176     if (dir.exists()) {
177       dir.delete();
178     }
179
180     return dir.mkdirs();
181   }
182
183   public static String getHttpServerAbsolutePath() {
184     return Thread.currentThread().getContextClassLoader().getResource("/").getPath() +  HttpServerPathConfig.getHttpServerPath();
185   }
186
187   /**
188    * delete directory.
189    * @param dir file to delete
190    * @return boolean
191    */
192   public static boolean deleteDir(File dir) {
193     if (dir.isDirectory()) {
194       String[] children = dir.list();
195       for (int i = 0; i < children.length; i++) {
196         boolean success = deleteDir(new File(dir, children[i]));
197         if (!success) {
198           return false;
199         }
200       }
201     }
202     return dir.delete();
203   }
204
205   public static String getAppDeployPath()
206   {
207       return Thread.currentThread().getContextClassLoader().getResource("/").getPath();
208   }
209 }
210