NIFI-1491 Deprecated existing method and added new one throwing eception

Reviewed and amended (to add javadoc @deprecation link and to carry forward other deprecated methods) by Tony Kurc (tkurc@apache.org). This closes #227
This commit is contained in:
Pierre Villard 2016-02-16 13:08:43 +01:00 committed by Tony Kurc
parent 784f2a2c20
commit 8f0116544a
2 changed files with 85 additions and 3 deletions

View File

@ -103,7 +103,10 @@ public class FileUtils {
* @param directory to delete contents of * @param directory to delete contents of
* @param filter if null then no filter is used * @param filter if null then no filter is used
* @param logger to notify * @param logger to notify
* @deprecated As of release 0.6.0, replaced by
* {@link #deleteFilesInDirectory(File, FilenameFilter, Logger)}
*/ */
@Deprecated
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger) { public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger) {
FileUtils.deleteFilesInDir(directory, filter, logger, false); FileUtils.deleteFilesInDir(directory, filter, logger, false);
} }
@ -117,7 +120,10 @@ public class FileUtils {
* @param filter if null then no filter is used * @param filter if null then no filter is used
* @param logger to notify * @param logger to notify
* @param recurse true if should recurse * @param recurse true if should recurse
* @deprecated As of release 0.6.0, replaced by
* {@link #deleteFilesInDirectory(File, FilenameFilter, Logger, boolean)}
*/ */
@Deprecated
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse) { public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse) {
FileUtils.deleteFilesInDir(directory, filter, logger, recurse, false); FileUtils.deleteFilesInDir(directory, filter, logger, recurse, false);
} }
@ -133,11 +139,18 @@ public class FileUtils {
* @param recurse will look for contents of sub directories. * @param recurse will look for contents of sub directories.
* @param deleteEmptyDirectories default is false; if true will delete * @param deleteEmptyDirectories default is false; if true will delete
* directories found that are empty * directories found that are empty
* @deprecated As of release 0.6.0, replaced by
* {@link #deleteFilesInDirectory(File, FilenameFilter, Logger, boolean, boolean)}
*/ */
@Deprecated
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) { public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) {
// ensure the specified directory is actually a directory and that it exists // ensure the specified directory is actually a directory and that it exists
if (null != directory && directory.isDirectory()) { if (null != directory && directory.isDirectory()) {
final File ingestFiles[] = directory.listFiles(); final File ingestFiles[] = directory.listFiles();
if (ingestFiles == null) {
// null if abstract pathname does not denote a directory, or if an I/O error occurs
logger.error("Unable to list directory content in: " + directory.getAbsolutePath());
}
for (File ingestFile : ingestFiles) { for (File ingestFile : ingestFiles) {
boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName()); boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
if (ingestFile.isFile() && process) { if (ingestFile.isFile() && process) {
@ -153,6 +166,74 @@ public class FileUtils {
} }
} }
/**
* Deletes all files (not directories..) in the given directory (non
* recursive) that match the given filename filter. If any file cannot be
* deleted then this is printed at warn to the given logger.
*
* @param directory to delete contents of
* @param filter if null then no filter is used
* @param logger to notify
* @throws IOException if abstract pathname does not denote a directory,
* or if an I/O error occurs
*/
public static void deleteFilesInDirectory(final File directory, final FilenameFilter filter, final Logger logger) throws IOException {
FileUtils.deleteFilesInDirectory(directory, filter, logger, false);
}
/**
* Deletes all files (not directories) in the given directory (recursive)
* that match the given filename filter. If any file cannot be deleted then
* this is printed at warn to the given logger.
*
* @param directory to delete contents of
* @param filter if null then no filter is used
* @param logger to notify
* @param recurse true if should recurse
* @throws IOException if abstract pathname does not denote a directory,
* or if an I/O error occurs
*/
public static void deleteFilesInDirectory(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse) throws IOException {
FileUtils.deleteFilesInDirectory(directory, filter, logger, recurse, false);
}
/**
* Deletes all files (not directories) in the given directory (recursive)
* that match the given filename filter. If any file cannot be deleted then
* this is printed at warn to the given logger.
*
* @param directory to delete contents of
* @param filter if null then no filter is used
* @param logger to notify
* @param recurse will look for contents of sub directories.
* @param deleteEmptyDirectories default is false; if true will delete
* directories found that are empty
* @throws IOException if abstract pathname does not denote a directory,
* or if an I/O error occurs
*/
public static void deleteFilesInDirectory(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) throws IOException {
// ensure the specified directory is actually a directory and that it exists
if (null != directory && directory.isDirectory()) {
final File ingestFiles[] = directory.listFiles();
if (ingestFiles == null) {
// null if abstract pathname does not denote a directory, or if an I/O error occurs
throw new IOException("Unable to list directory content in: " + directory.getAbsolutePath());
}
for (File ingestFile : ingestFiles) {
boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
if (ingestFile.isFile() && process) {
FileUtils.deleteFile(ingestFile, logger, 3);
}
if (ingestFile.isDirectory() && recurse) {
FileUtils.deleteFilesInDirectory(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
if (deleteEmptyDirectories && ingestFile.list().length == 0) {
FileUtils.deleteFile(ingestFile, logger, 3);
}
}
}
}
}
/** /**
* Deletes given files. * Deletes given files.
* *
@ -167,8 +248,9 @@ public class FileUtils {
} }
public static void deleteFile(final File file, final boolean recurse) throws IOException { public static void deleteFile(final File file, final boolean recurse) throws IOException {
if (file.isDirectory() && recurse) { final File[] list = file.listFiles();
FileUtils.deleteFiles(Arrays.asList(file.listFiles()), recurse); if (file.isDirectory() && recurse && list != null) {
FileUtils.deleteFiles(Arrays.asList(list), recurse);
} }
//now delete the file itself regardless of whether it is plain file or a directory //now delete the file itself regardless of whether it is plain file or a directory
if (!FileUtils.deleteFile(file, null, 5)) { if (!FileUtils.deleteFile(file, null, 5)) {

View File

@ -95,7 +95,7 @@ public class NiFi {
// the working directory, we can be assured that it will attempt to extract the // the working directory, we can be assured that it will attempt to extract the
// war every time the application starts. // war every time the application starts.
File webWorkingDir = properties.getWebWorkingDirectory(); File webWorkingDir = properties.getWebWorkingDirectory();
FileUtils.deleteFilesInDir(webWorkingDir, null, logger, true, true); FileUtils.deleteFilesInDirectory(webWorkingDir, null, logger, true, true);
FileUtils.deleteFile(webWorkingDir, logger, 3); FileUtils.deleteFile(webWorkingDir, logger, 3);
detectTimingIssues(); detectTimingIssues();