Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / utilities / src / main / java / org / onap / policy / apex / model / utilities / DirectoryUtils.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.utilities;
22
23 import java.io.File;
24 import org.slf4j.ext.XLogger;
25 import org.slf4j.ext.XLoggerFactory;
26
27 /**
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.
30  *
31  * @author Liam Fallon (liam.fallon@ericsson.com)
32  */
33 public abstract class DirectoryUtils {
34     // Get a reference to the logger
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(DirectoryUtils.class);
36
37     /**
38      * Private constructor used to prevent sub class instantiation.
39      */
40     private DirectoryUtils() {
41     }
42
43     /**
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.
46      *
47      * @param nameprefix The prefix of the filename. System.nanoTime() will be appended to the pattern to create a
48      *        unique file pattern
49      * @return The temporary directory
50      */
51     public static File getLocalTempDirectory(final String nameprefix) {
52         try {
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);
56
57             // Delete the directory if it already exists
58             if (tempDir.exists()) {
59                 return null;
60             }
61
62             // Make the directory
63             tempDir.mkdirs();
64
65             // Add a shutdown hook that deletes the directory contents when the JVM closes
66             Runtime.getRuntime().addShutdownHook(new DirectoryDeleteShutdownHook(tempDir));
67
68             LOGGER.trace("creating temp directory\"{}\" : ", tempDir.getAbsolutePath());
69             return tempDir;
70         } catch (final Exception e) {
71             LOGGER.debug("error creating temp directory\"{}\" : " + e.getMessage(), e);
72             return null;
73         }
74     }
75
76     /**
77      * Method to recursively delete all the files in a directory.
78      *
79      * @param tempDir the directory to empty
80      * @return true if the operation succeeds, false otherwise
81      */
82     public static boolean emptyDirectory(final File tempDir) {
83         // Sanity check
84         if (!tempDir.exists() || !tempDir.isDirectory()) {
85             return false;
86         }
87
88         // Walk the directory structure deleting files as we go
89         final File[] files = tempDir.listFiles();
90         if (files != null) {
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);
96                 }
97
98                 // Delete the directory entry
99                 directoryFile.delete();
100             }
101         }
102
103         return true;
104     }
105 }