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);
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 copyInDirectory(String srcDirName, String destDirName, File[] files, boolean overlay)
48 for (int i = 0; i < files.length; i++) {
50 if (files[i].isFile()) {
51 flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
53 if (files[i].isDirectory()) {
54 flag = copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay);
57 String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
58 LOGGER.error(message);
66 * copy from directory.
67 * @param srcDirName source directory name
68 * @param destDirName destination directory name
69 * @param overlay overwrite or not
71 * @throws IOException e
73 public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
75 File srcDir = new File(srcDirName);
76 if (!srcDir.exists() || !srcDir.isDirectory()) {
80 String useDestDirName = destDirName;
81 if (!useDestDirName.endsWith(File.separator)) {
82 useDestDirName += File.separator;
84 File destDir = new File(useDestDirName);
85 if (destDir.exists() && overlay) {
86 new File(useDestDirName).delete();
87 } else if (destDir.exists() && !overlay) {
91 if (!destDir.mkdirs()) {
95 File[] files = srcDir.listFiles();
97 return copyInDirectory(srcDirName, destDirName, files, overlay);
102 * @param srcFileName source file name
103 * @param destFileName target file name
106 public static boolean copyByte(File srcFile, File destFile)
109 InputStream in = new FileInputStream(srcFile);
110 OutputStream out = new FileOutputStream(destFile);
113 byte[] buffer = new byte[1024];
115 for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
116 out.write(buffer, 0, byteread);
120 } catch (IOException e) {
121 LOGGER.error("IOException in copyFile", e);
128 * @param srcFileName source file name
129 * @param destFileName target file name
130 * @param overlay overwrite or not
133 public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
134 File srcFile = new File(srcFileName);
136 if (!srcFile.exists() || !srcFile.isFile()) {
137 String message = "Source file : " + srcFileName + " not exist !";
138 LOGGER.error(message);
142 File destFile = new File(destFileName);
143 if (destFile.exists() && overlay) {
144 new File(destFileName).delete();
145 } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
149 return copyByte(srcFile, destFile);
154 * @param destDirName target directory name
157 public static boolean createDir(String destDirName) {
158 String useDestDirName = destDirName;
159 if (!useDestDirName.endsWith(File.separator)) {
160 useDestDirName += File.separator;
162 File dir = new File(useDestDirName);
170 public static String getHttpServerAbsolutePath() {
171 return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + HttpServerPathConfig.getHttpServerPath();
176 * @param dir file to delete
179 public static boolean deleteDir(File dir) {
180 if (dir.isDirectory()) {
181 String[] children = dir.list();
182 for (int i = 0; i < children.length; i++) {
183 boolean success = deleteDir(new File(dir, children[i]));
192 public static String getAppDeployPath()
194 return Thread.currentThread().getContextClassLoader().getResource("/").getPath();