2 * Copyright 2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vnfsdk.marketplace.filemanage.http;
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 import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
30 public class ToolUtil {
31 private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtil.class);
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
43 * @throws IOException e
45 public static boolean simpleCopyDirectory(String srcDirName, String destDirName)
48 File srcDir = new File(srcDirName);
49 File[] files = srcDir.listFiles();
50 for (int i = 0; i < files.length; i++) {
52 if (files[i].isFile()) {
53 flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
55 if (files[i].isDirectory()) {
56 flag = simpleCopyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName());
59 String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
60 LOGGER.error(message);
69 * @param destDirName destination directory name
70 * @param overlay overwrite or not
73 public static boolean createDestDir(String destDirName, boolean overlay)
75 File destDir = new File(destDirName);
76 if (destDir.exists() && overlay) {
77 String fileAbsPath = destDir.getAbsolutePath();
79 Files.delete(Paths.get(destDirName));
80 } catch (IOException e) {
81 LOGGER.error("fail to delete {} {} " , fileAbsPath, e);
83 } else if (destDir.exists() && !overlay) {
87 return destDir.mkdirs();
91 * copy from directory.
92 * @param srcDirName source directory name
93 * @param destDirName destination directory name
94 * @param overlay overwrite or not
96 * @throws IOException e
98 public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
100 File srcDir = new File(srcDirName);
101 if (!srcDir.exists() || !srcDir.isDirectory()) {
105 String fullDestDirName = destDirName;
106 if (!fullDestDirName.endsWith(File.separator)) {
107 fullDestDirName += File.separator;
110 if (!createDestDir(fullDestDirName, overlay))
115 return simpleCopyDirectory(srcDirName, fullDestDirName);
120 * @param srcFileName source file name
121 * @param destFileName target file name
124 public static boolean copyByte(File srcFile, File destFile)
127 InputStream in = new FileInputStream(srcFile);
128 OutputStream out = new FileOutputStream(destFile);
131 byte[] buffer = new byte[1024];
133 for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
134 out.write(buffer, 0, byteread);
138 } catch (IOException e) {
139 LOGGER.error("IOException in copyFile", e);
146 * @param srcFileName source file name
147 * @param destFileName target file name
148 * @param overlay overwrite or not
151 public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
152 File srcFile = new File(srcFileName);
154 if (!srcFile.exists() || !srcFile.isFile()) {
155 String message = "Source file : " + srcFileName + " not exist !";
156 LOGGER.error(message);
160 File destFile = new File(destFileName);
161 if (destFile.exists() && overlay) {
162 String fileAbsPath = destFile.getAbsolutePath();
164 Files.delete(Paths.get(destFileName));
165 } catch (IOException e) {
166 LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
168 } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
172 return copyByte(srcFile, destFile);
177 * @param destDirName target directory name
180 public static boolean createDir(String destDirName) {
181 String useDestDirName = destDirName;
182 if (!useDestDirName.endsWith(File.separator)) {
183 useDestDirName += File.separator;
185 File dir = new File(useDestDirName);
187 String fileAbsPath = dir.getAbsolutePath();
189 Files.delete(Paths.get(useDestDirName));
190 } catch (IOException e) {
191 LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
198 public static String getHttpServerAbsolutePath() {
199 return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + HttpServerPathConfig.getHttpServerPath();
204 * @param dir file to delete
207 public static boolean deleteDir(File dir) {
208 if (dir.isDirectory()) {
209 String[] children = dir.list();
210 for (int i = 0; i < children.length; i++) {
211 boolean success = deleteDir(new File(dir, children[i]));
217 boolean isFileDeleted=false;
218 String fileAbsPath = dir.getAbsolutePath();
220 Files.delete(Paths.get(dir.getPath()));
222 } catch (IOException e) {
223 LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
225 return isFileDeleted;
228 public static String getAppDeployPath()
230 return Thread.currentThread().getContextClassLoader().getResource("/").getPath();