From c554e4d0ada9e2e0e227dd1b9ca9e5282c0a3c32 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Sun, 26 Aug 2007 20:53:33 +0000 Subject: [PATCH] add: new ISimpleLogger that is based on log4j change: remove log4j dependency from FileUtilities SVN: 1493 --- .../common/logging/Log4jSimpleLogger.java | 49 +++++++++++ .../cisd/common/utilities/FileUtilities.java | 88 +++++-------------- .../FileUtilitiesDeleteRecursivelyTest.java | 10 +-- .../common/utilities/FileUtilitiesTest.java | 46 ---------- 4 files changed, 75 insertions(+), 118 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/logging/Log4jSimpleLogger.java diff --git a/common/source/java/ch/systemsx/cisd/common/logging/Log4jSimpleLogger.java b/common/source/java/ch/systemsx/cisd/common/logging/Log4jSimpleLogger.java new file mode 100644 index 00000000000..9bb8fdd5088 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/logging/Log4jSimpleLogger.java @@ -0,0 +1,49 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.logging; + +import org.apache.log4j.Logger; +import org.apache.log4j.Priority; + +/** + * A {@link ISimpleLogger} that is based on log4j. + * + * @author Bernd Rinn + */ +public class Log4jSimpleLogger implements ISimpleLogger +{ + private final Priority log4jPriority; + + private final Logger log4jLogger; + + public Log4jSimpleLogger(Priority log4jPriority, Logger log4jLogger) + { + this.log4jPriority = log4jPriority; + this.log4jLogger = log4jLogger; + } + + public void log(String message) + { + log4jLogger.log(log4jPriority, message); + } + + public void log(String messageTemplate, Object... args) + { + log4jLogger.log(log4jPriority, String.format(messageTemplate, args)); + } + +} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java index aa35b0f1c83..73f9a158dc8 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java @@ -32,12 +32,9 @@ import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; -import ch.systemsx.cisd.common.logging.LogCategory; -import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.common.logging.ISimpleLogger; /** * Some useful utility methods for files and directories. @@ -54,8 +51,6 @@ import ch.systemsx.cisd.common.logging.LogFactory; */ public final class FileUtilities { - private static final Logger machineLog = LogFactory.getLogger(LogCategory.MACHINE, FileUtilities.class); - private FileUtilities() { // Can not be instantiated. @@ -276,25 +271,28 @@ public final class FileUtilities * Deletes a directory recursively, that is deletes all files and directories within first and then the directory * itself. * <p> - * Convenience method for {@link #deleteRecursively(File, Level)} with <var>Level</var> set to {@link Level#DEBUG}. + * Convenience method for {@link #deleteRecursively(File)} with <var>logger</var> set to <code>null</code>. * * @param path Path of the file or directory to delete. * @return <code>true</code> if the path has been delete successfully, <code>false</code> otherwise. */ public static boolean deleteRecursively(File path) { - return deleteRecursively(path, Level.DEBUG); + assert path != null; + + return deleteRecursively(path, null); } - + /** * Deletes a directory recursively, that is deletes all files and directories within first and then the directory * itself. * * @param path Path of the file or directory to delete. - * @param logLevel The logLevel that should be used to log deletion of path entries. + * @param logger The logger that should be used to log deletion of path entries, or <code>null</code> if nothing + * should be logged. * @return <code>true</code> if the path has been delete successfully, <code>false</code> otherwise. */ - public static boolean deleteRecursively(File path, Level logLevel) + public static boolean deleteRecursively(File path, ISimpleLogger logger) { assert path != null; @@ -304,41 +302,24 @@ public final class FileUtilities { if (file.isDirectory()) { - deleteRecursively(file, logLevel); + deleteRecursively(file, logger); } else { - if (machineLog.isEnabledFor(logLevel)) + if (logger != null) { - machineLog.log(logLevel, String.format("Deleting file '%s'", file.getPath())); + logger.log("Deleting file '%s'", file.getPath()); } file.delete(); } } } - if (machineLog.isEnabledFor(logLevel)) + if (logger != null) { - machineLog.log(logLevel, String.format("Deleting directory '%s'", path.getPath())); + logger.log("Deleting directory '%s'", path.getPath()); } return path.delete(); } - /** - * Deletes selected parts of a directory recursively, that is deletes all files and directories within the directory - * that are accepted by the {@link FileFilter}. Any subdirectory that is accepted by the <var>filter</var> will be - * completely deleted. This holds true also for the <var>path</var> itself. - * <p> - * Convenience method for {@link #deleteRecursively(File, FileFilter, Level)} with <var>Level</var> set to - * {@link Level#DEBUG}. - * - * @param path Path of the directory to delete the selected content from. - * @param filter The {@link FileFilter} to use when deciding which paths to delete. - * @return <code>true</code> if the <var>path</var> itself has been deleted. - */ - public static boolean deleteRecursively(File path, FileFilter filter) - { - return deleteRecursively(path, filter, Level.DEBUG); - } - /** * Deletes selected parts of a directory recursively, that is deletes all files and directories within the directory * that are accepted by the {@link FileFilter}. Any subdirectory that is accepted by the <var>filter</var> will be @@ -346,25 +327,25 @@ public final class FileUtilities * * @param path Path of the directory to delete the selected content from. * @param filter The {@link FileFilter} to use when deciding which paths to delete. - * @param logLevel The logLevel that should be used to log deletion of path entries. + * @param logger The logger that should be used to log deletion of path entries, or <code>null</code> if nothing + * should be logged. * @return <code>true</code> if the <var>path</var> itself has been deleted. */ - public static boolean deleteRecursively(File path, FileFilter filter, Level logLevel) + public static boolean deleteRecursively(File path, FileFilter filter, ISimpleLogger logger) { assert path != null; assert filter != null; - assert logLevel != null; if (filter.accept(path)) { - return FileUtilities.deleteRecursively(path, logLevel); + return FileUtilities.deleteRecursively(path, logger); } else { if (path.isDirectory()) { for (File file : path.listFiles()) { - deleteRecursively(file, filter, logLevel); + deleteRecursively(file, filter, logger); } } return false; @@ -372,30 +353,7 @@ public final class FileUtilities } /** - * Moves <var>path</var> to <var>destinationDir</var>. - * - * @see File#renameTo(File) - * @param path The file or directory that will be moved. - * @param destinationDir Directory to move the <var>path</var> to. - * @return <code>true</code> if the <var>path</var> has been moved successfully, <code>false</code> otherwise. - */ - public static boolean movePath(File path, File destinationDir) - { - assert path != null; - assert destinationDir != null; - - if (machineLog.isTraceEnabled()) - { - machineLog.trace(String.format("Moving path '%s' to '%s'", path.getPath(), destinationDir.getPath()) - .toString()); - } - return path.renameTo(new File(destinationDir, path.getName())); - } - - private static final String PATH_LAST_CHANGED_TEMPLATE = "Path '%s' has last been changed at %2$tF %2$tT"; - - /** - * @return The time when any file below <var>directory</var> has last been changed in the file system. + * @return The time when any file in (or below) <var>path</var> has last been changed in the file system. * @throws CheckedExceptionTunnel of an {@link IOException} if the <var>path</var> does not exist or is not * readable. */ @@ -417,10 +375,6 @@ public final class FileUtilities lastChanged = Math.max(lastChanged, lastChanged(subDirectory)); } } - if (machineLog.isTraceEnabled()) - { - machineLog.trace(String.format(PATH_LAST_CHANGED_TEMPLATE, path, lastChanged)); - } return lastChanged; } @@ -589,7 +543,7 @@ public final class FileUtilities IOUtils.closeQuietly(resourceStream); } } - + /** * Tries to copy the resource with the given name to a temporary file. * diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesDeleteRecursivelyTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesDeleteRecursivelyTest.java index b114ad64dde..d085b5f5e83 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesDeleteRecursivelyTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesDeleteRecursivelyTest.java @@ -154,7 +154,7 @@ public class FileUtilitiesDeleteRecursivelyTest { return false; } - }); + }, null); checkDirectories(true); checkFiles(true); } @@ -169,7 +169,7 @@ public class FileUtilitiesDeleteRecursivelyTest { return true; } - }); + }, null); checkDirectories(false); checkFiles(false); assert workingDirectory.exists() == false; @@ -185,7 +185,7 @@ public class FileUtilitiesDeleteRecursivelyTest { return pathname.isFile(); } - }); + }, null); checkDirectories(true); checkFiles(false); } @@ -200,7 +200,7 @@ public class FileUtilitiesDeleteRecursivelyTest { return pathname.getName().equals("file1a") || pathname.getName().equals("file3"); } - }); + }, null); checkDirectories(true); assert file1a.exists() == false; assert file1b.exists(); @@ -219,7 +219,7 @@ public class FileUtilitiesDeleteRecursivelyTest { return pathname.getName().equals("dir1"); } - }); + }, null); assert dir1.exists() == false; assert dir1a.exists() == false; assert dir1b.exists() == false; diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java index e297a444bcc..1456444b6da 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java @@ -38,7 +38,6 @@ import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; import ch.systemsx.cisd.common.Constants; -import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.logging.LogInitializer; /** @@ -58,51 +57,6 @@ public class FileUtilitiesTest assert workingDirectory.isDirectory(); } - @Test - public void moveFile() - { - final File root = new File(workingDirectory, "move-test"); - FileUtilities.deleteRecursively(root); - root.mkdir(); - assert root.isDirectory(); - final File file = new File(root, "a"); - try - { - file.createNewFile(); - } catch (IOException e) - { - throw new CheckedExceptionTunnel(e); - } - final File destinationDir = new File(root, "d"); - destinationDir.mkdir(); - assert destinationDir.exists(); - FileUtilities.movePath(file, destinationDir); - assert file.exists() == false; - final File newFile = new File(destinationDir, "a"); - assert newFile.exists(); - FileUtilities.deleteRecursively(root); - } - - @Test - public void moveDirectory() - { - final File root = new File(workingDirectory, "move-test"); - FileUtilities.deleteRecursively(root); - root.mkdir(); - assert root.isDirectory(); - final File file = new File(root, "a"); - file.mkdir(); - assert file.isDirectory(); - final File destinationDir = new File(root, "d"); - destinationDir.mkdir(); - assert destinationDir.exists(); - FileUtilities.movePath(file, destinationDir); - assert file.exists() == false; - final File newFile = new File(destinationDir, "a"); - assert newFile.exists(); - FileUtilities.deleteRecursively(root); - } - @Test public void testLoadToStringFile() throws Exception { -- GitLab