1d5d46f97e0e243430d52210c4ee3f36cfc1c9ab
[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    * copy from directory.
37    * @param srcDirName source directory name
38    * @param destDirName destination directory name
39    * @param overlay overwrite or not
40    * @return boolean
41    * @throws IOException e
42    */
43   public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
44       throws IOException {
45     File srcDir = new File(srcDirName);
46     if (!srcDir.exists() || !srcDir.isDirectory()) {
47       return false;
48     }
49
50     String useDestDirName = destDirName;
51     if (!useDestDirName.endsWith(File.separator)) {
52       useDestDirName += File.separator;
53     }
54     File destDir = new File(useDestDirName);
55     if (destDir.exists() && overlay) {
56         new File(destDirName).delete();
57     } else if ((destDir.exists() && !overlay) || (!destDir.exists() && !destDir.mkdirs())) {
58         return false;
59     }
60     boolean flag = true;
61     File[] files = srcDir.listFiles();
62     for (int i = 0; i < files.length; i++) {
63       if (files[i].isFile()) {
64         flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
65       } else if (files[i].isDirectory()) {
66         flag = copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay);
67       }
68       if (!flag) {
69         String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
70         LOGGER.error(message);
71         return false;
72       }
73     }
74
75     return true;
76   }
77
78   /**
79    * copy file.
80    * @param srcFileName source file name
81    * @param destFileName target file name
82    * @param overlay overwrite or not
83    * @return boolean
84    */
85   public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
86     File srcFile = new File(srcFileName);
87
88     if (!srcFile.exists() || !srcFile.isFile()) {
89       String message = "Source file : " + srcFileName + " not exist !";
90       LOGGER.error(message);
91       return false;
92     }
93
94     File destFile = new File(destFileName);
95     if (destFile.exists() && overlay) {
96         new File(destFileName).delete();
97     } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
98         return false;
99     }
100
101     int byteread = 0;
102
103     try (
104           InputStream in = new FileInputStream(srcFile);
105           OutputStream out = new FileOutputStream(destFile);
106       ) {
107       byte[] buffer = new byte[1024];
108
109       while ((byteread = in.read(buffer)) != -1) {
110         out.write(buffer, 0, byteread);
111       }
112       return true;
113     } catch (IOException e) {
114       LOGGER.error("IOException in copyFile", e);
115       return false;
116     }
117
118   }
119
120   /**
121    * create directory.
122    * @param destDirName target directory name
123    * @return boolean
124    */
125   public static boolean createDir(String destDirName) {
126     String useDestDirName = destDirName;
127     if (!useDestDirName.endsWith(File.separator)) {
128       useDestDirName += File.separator;
129     }
130     File dir = new File(useDestDirName);
131     if (dir.exists()) {
132       dir.delete();
133     }
134
135     return dir.mkdirs();
136   }
137
138   public static String getHttpServerAbsolutePath() {
139     return Thread.currentThread().getContextClassLoader().getResource("/").getPath() +  HttpServerPathConfig.getHttpServerPath();
140   }
141
142   /**
143    * delete directory.
144    * @param dir file to delete
145    * @return boolean
146    */
147   public static boolean deleteDir(File dir) {
148     if (dir.isDirectory()) {
149       String[] children = dir.list();
150       for (int i = 0; i < children.length; i++) {
151         boolean success = deleteDir(new File(dir, children[i]));
152         if (!success) {
153           return false;
154         }
155       }
156     }
157     return dir.delete();
158   }
159
160   public static String getAppDeployPath()
161   {
162       return Thread.currentThread().getContextClassLoader().getResource("/").getPath();
163   }
164 }
165