2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.utilities;
24 import org.slf4j.ext.XLogger;
25 import org.slf4j.ext.XLoggerFactory;
28 * This is common utility class with static methods for handling directories. It is an abstract class to prevent any
29 * direct instantiation and private constructor to prevent extending this class.
31 * @author Liam Fallon (liam.fallon@ericsson.com)
33 public abstract class DirectoryUtils {
34 // Get a reference to the logger
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DirectoryUtils.class);
38 * Private constructor used to prevent sub class instantiation.
40 private DirectoryUtils() {
44 * Method to get an empty temporary directory in the system temporary directory on the local machine that will be
45 * deleted on (normal) shutdown.
47 * @param nameprefix The prefix of the filename. System.nanoTime() will be appended to the pattern to create a
49 * @return The temporary directory
51 public static File getLocalTempDirectory(final String nameprefix) {
53 // Get the name of the temporary directory
54 final String tempDirName = System.getProperty("java.io.tmpdir") + "/" + nameprefix + System.nanoTime();
55 final File tempDir = new File(tempDirName);
57 // Delete the directory if it already exists
58 if (tempDir.exists()) {
65 // Add a shutdown hook that deletes the directory contents when the JVM closes
66 Runtime.getRuntime().addShutdownHook(new DirectoryDeleteShutdownHook(tempDir));
68 LOGGER.trace("creating temp directory\"{}\" : ", tempDir.getAbsolutePath());
70 } catch (final Exception e) {
71 LOGGER.debug("error creating temp directory\"{}\" : " + e.getMessage(), e);
77 * Method to recursively delete all the files in a directory.
79 * @param tempDir the directory to empty
80 * @return true if the operation succeeds, false otherwise
82 public static boolean emptyDirectory(final File tempDir) {
84 if (!tempDir.exists() || !tempDir.isDirectory()) {
88 // Walk the directory structure deleting files as we go
89 final File[] files = tempDir.listFiles();
91 for (final File directoryFile : files) {
92 // Check if this is a directory itself
93 if (directoryFile.isDirectory()) {
94 // Recurse into the sub directory and empty it
95 emptyDirectory(directoryFile);
98 // Delete the directory entry
99 directoryFile.delete();