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.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
26 import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class ToolUtil {
32 private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtil.class);
35 * copy from directory.
36 * @param srcDirName source directory name
37 * @param destDirName destination directory name
38 * @param overlay overwrite or not
40 * @throws IOException e
42 public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
44 File srcDir = new File(srcDirName);
45 if (!srcDir.exists()) {
47 } else if (!srcDir.isDirectory()) {
51 if (!destDirName.endsWith(File.separator)) {
52 destDirName = destDirName + File.separator;
54 File destDir = new File(destDirName);
55 if (destDir.exists()) {
57 new File(destDirName).delete();
62 if (!destDir.mkdirs()) {
67 File[] files = srcDir.listFiles();
68 for (int i = 0; i < files.length; i++) {
69 if (files[i].isFile()) {
70 flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
74 } else if (files[i].isDirectory()) {
75 flag = copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay);
82 String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
83 LOGGER.error(message);
92 * @param srcFileName source file name
93 * @param destFileName target file name
94 * @param overlay overwrite or not
97 public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
98 File srcFile = new File(srcFileName);
100 if (!srcFile.exists()) {
101 String message = "Source file : " + srcFileName + " not exist !";
102 LOGGER.error(message);
104 } else if (!srcFile.isFile()) {
108 File destFile = new File(destFileName);
109 if (destFile.exists()) {
111 new File(destFileName).delete();
114 if (!destFile.getParentFile().exists()) {
115 if (!destFile.getParentFile().mkdirs()) {
122 InputStream in = null;
123 OutputStream out = null;
126 in = new FileInputStream(srcFile);
127 out = new FileOutputStream(destFile);
128 byte[] buffer = new byte[1024];
130 while ((byteread = in.read(buffer)) != -1) {
131 out.write(buffer, 0, byteread);
134 } catch (FileNotFoundException e1) {
136 } catch (IOException e1) {
146 } catch (IOException e1) {
147 e1.printStackTrace();
154 * @param destDirName target directory name
157 public static boolean createDir(String destDirName) {
158 File dir = new File(destDirName);
162 if (!destDirName.endsWith(File.separator)) {
163 destDirName = destDirName + File.separator;
172 public static String getHttpServerAbsolutePath() {
173 return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + HttpServerPathConfig.getHttpServerPath();
178 * @param dir file to delete
181 public static boolean deleteDir(File dir) {
182 if (dir.isDirectory()) {
183 String[] children = dir.list();
184 for (int i = 0; i < children.length; i++) {
185 boolean success = deleteDir(new File(dir, children[i]));
194 public static String getAppDeployPath()
196 return Thread.currentThread().getContextClassLoader().getResource("/").getPath();