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;
25 import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class ToolUtil {
31 private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtil.class);
36 * copy from directory.
37 * @param srcDirName source directory name
38 * @param destDirName destination directory name
39 * @param overlay overwrite or not
41 * @throws IOException e
43 public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
45 File srcDir = new File(srcDirName);
46 if (!srcDir.exists() || !srcDir.isDirectory()) {
50 String useDestDirName = destDirName;
51 if (!useDestDirName.endsWith(File.separator)) {
52 useDestDirName += File.separator;
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())) {
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);
69 String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
70 LOGGER.error(message);
80 * @param srcFileName source file name
81 * @param destFileName target file name
82 * @param overlay overwrite or not
85 public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
86 File srcFile = new File(srcFileName);
88 if (!srcFile.exists() || !srcFile.isFile()) {
89 String message = "Source file : " + srcFileName + " not exist !";
90 LOGGER.error(message);
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()) {
104 InputStream in = new FileInputStream(srcFile);
105 OutputStream out = new FileOutputStream(destFile);
107 byte[] buffer = new byte[1024];
109 while ((byteread = in.read(buffer)) != -1) {
110 out.write(buffer, 0, byteread);
113 } catch (IOException e) {
114 LOGGER.error("IOException in copyFile", e);
122 * @param destDirName target directory name
125 public static boolean createDir(String destDirName) {
126 String useDestDirName = destDirName;
127 if (!useDestDirName.endsWith(File.separator)) {
128 useDestDirName += File.separator;
130 File dir = new File(useDestDirName);
138 public static String getHttpServerAbsolutePath() {
139 return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + HttpServerPathConfig.getHttpServerPath();
144 * @param dir file to delete
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]));
160 public static String getAppDeployPath()
162 return Thread.currentThread().getContextClassLoader().getResource("/").getPath();