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(destDirName).delete();
87 } else if ((destDir.exists() && !overlay) || (!destDir.exists() && !destDir.mkdirs())) {
91 File[] files = srcDir.listFiles();
93 return copyInDirectory(srcDirName, destDirName, files, overlay);
98 * @param srcFileName source file name
99 * @param destFileName target file name
102 public static boolean copyByte(File srcFile, File destFile)
105 InputStream in = new FileInputStream(srcFile);
106 OutputStream out = new FileOutputStream(destFile);
109 byte[] buffer = new byte[1024];
112 while ((byteread = in.read(buffer)) != -1) {
113 out.write(buffer, 0, byteread);
116 } catch (IOException e) {
117 LOGGER.error("IOException in copyFile", e);
124 * @param srcFileName source file name
125 * @param destFileName target file name
126 * @param overlay overwrite or not
129 public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
130 File srcFile = new File(srcFileName);
132 if (!srcFile.exists() || !srcFile.isFile()) {
133 String message = "Source file : " + srcFileName + " not exist !";
134 LOGGER.error(message);
138 File destFile = new File(destFileName);
139 if (destFile.exists() && overlay) {
140 new File(destFileName).delete();
141 } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
145 return copyByte(srcFile, destFile);
150 * @param destDirName target directory name
153 public static boolean createDir(String destDirName) {
154 String useDestDirName = destDirName;
155 if (!useDestDirName.endsWith(File.separator)) {
156 useDestDirName += File.separator;
158 File dir = new File(useDestDirName);
166 public static String getHttpServerAbsolutePath() {
167 return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + HttpServerPathConfig.getHttpServerPath();
172 * @param dir file to delete
175 public static boolean deleteDir(File dir) {
176 if (dir.isDirectory()) {
177 String[] children = dir.list();
178 for (int i = 0; i < children.length; i++) {
179 boolean success = deleteDir(new File(dir, children[i]));
188 public static String getAppDeployPath()
190 return Thread.currentThread().getContextClassLoader().getResource("/").getPath();