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);
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 new File(destDirName).delete();
78 } else if (destDir.exists() && !overlay) {
82 return destDir.mkdirs();
86 * copy from directory.
87 * @param srcDirName source directory name
88 * @param destDirName destination directory name
89 * @param overlay overwrite or not
91 * @throws IOException e
93 public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
95 File srcDir = new File(srcDirName);
96 if (!srcDir.exists() || !srcDir.isDirectory()) {
100 String fullDestDirName = destDirName;
101 if (!fullDestDirName.endsWith(File.separator)) {
102 fullDestDirName += File.separator;
105 if (!createDestDir(fullDestDirName, overlay))
110 return simpleCopyDirectory(srcDirName, fullDestDirName);
115 * @param srcFileName source file name
116 * @param destFileName target file name
119 public static boolean copyByte(File srcFile, File destFile)
122 InputStream in = new FileInputStream(srcFile);
123 OutputStream out = new FileOutputStream(destFile);
126 byte[] buffer = new byte[1024];
128 for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
129 out.write(buffer, 0, byteread);
133 } catch (IOException e) {
134 LOGGER.error("IOException in copyFile", e);
141 * @param srcFileName source file name
142 * @param destFileName target file name
143 * @param overlay overwrite or not
146 public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
147 File srcFile = new File(srcFileName);
149 if (!srcFile.exists() || !srcFile.isFile()) {
150 String message = "Source file : " + srcFileName + " not exist !";
151 LOGGER.error(message);
155 File destFile = new File(destFileName);
156 if (destFile.exists() && overlay) {
157 new File(destFileName).delete();
158 } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
162 return copyByte(srcFile, destFile);
167 * @param destDirName target directory name
170 public static boolean createDir(String destDirName) {
171 String useDestDirName = destDirName;
172 if (!useDestDirName.endsWith(File.separator)) {
173 useDestDirName += File.separator;
175 File dir = new File(useDestDirName);
183 public static String getHttpServerAbsolutePath() {
184 return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + HttpServerPathConfig.getHttpServerPath();
189 * @param dir file to delete
192 public static boolean deleteDir(File dir) {
193 if (dir.isDirectory()) {
194 String[] children = dir.list();
195 for (int i = 0; i < children.length; i++) {
196 boolean success = deleteDir(new File(dir, children[i]));
205 public static String getAppDeployPath()
207 return Thread.currentThread().getContextClassLoader().getResource("/").getPath();