From 842f21e9ebff30504898e7f82c4aa5369be09879 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Wed, 14 May 2008 12:46:37 +0000 Subject: [PATCH] change: - Move 'HighwaterMark' stuff in its own package. SVN: 6037 --- .../highwatermark/FileWithHighwaterMark.java | 106 ++++++++++++++++++ .../HighwaterMarkSelfTestable.java | 5 +- .../HighwaterMarkWatcher.java | 29 +++-- .../FileWithHighwaterMarkTest.java | 79 +++++++++++++ .../HighwaterMarkWatcherTest.java | 7 +- 5 files changed, 206 insertions(+), 20 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java rename common/source/java/ch/systemsx/cisd/common/{utilities => highwatermark}/HighwaterMarkSelfTestable.java (92%) rename common/source/java/ch/systemsx/cisd/common/{utilities => highwatermark}/HighwaterMarkWatcher.java (92%) create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java rename common/sourceTest/java/ch/systemsx/cisd/common/{utilities => highwatermark}/HighwaterMarkWatcherTest.java (96%) diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java new file mode 100644 index 00000000000..744cb5b5e97 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java @@ -0,0 +1,106 @@ +/* + * Copyright 2008 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.highwatermark; + +import java.io.File; +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; + +import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; +import ch.systemsx.cisd.common.utilities.AbstractHashable; +import ch.systemsx.cisd.common.utilities.FileUtilities; +import ch.systemsx.cisd.common.utilities.PropertyUtils; + +/** + * A {@link File} with a <i>high water mark</i> attached to it. + * + * @author Christian Ribeaud + */ +public final class FileWithHighwaterMark extends AbstractHashable +{ + + /** + * The property name under which you must find a <code>long</code> for the high water mark (in + * <i>kilobytes</i>). + */ + public static final String HIGHWATER_MARK_PROPERTY_KEY = "highwater-mark"; + + private final File path; + + private final long highwaterMark; + + /** + * @param file the file path. + * @param highwaterMark the high water mark. <code>-1</code> means that the system will not be + * watching. + */ + public FileWithHighwaterMark(final File file, final long highwaterMark) + { + this.path = file; + this.highwaterMark = highwaterMark; + } + + /** + * @param path the file path. + */ + public FileWithHighwaterMark(final File path) + { + this(path, -1); + } + + /** + * Instantiates a new <code>FileWithHighwaterMark</code> from given <var>properties</var>. + * + * @param filePropertyKey the property key under which the file path can be found. + * @throws ConfigurationFailureException if given <var>filePropertyKey</var> could not be found + * in given <var>properties</var>. + */ + public final static FileWithHighwaterMark fromProperties(final Properties properties, + final String filePropertyKey) throws ConfigurationFailureException + { + assert properties != null : "Unspecified properties"; + assert StringUtils.isNotBlank(filePropertyKey) : "File property key is blank"; + final String filePath = PropertyUtils.getMandatoryProperty(properties, filePropertyKey); + final long highwaterMark = + PropertyUtils.getLong(properties, filePropertyKey.concat(".").concat( + HIGHWATER_MARK_PROPERTY_KEY), -1L); + return new FileWithHighwaterMark(new File(filePath), highwaterMark); + } + + /** + * Returns the file path. + */ + public final File getFile() + { + return path; + } + + /** Return the canonical path of the encapsulated <code>path</code>. */ + public final String getCanonicalPath() + { + return FileUtilities.getCanonicalPath(getFile()); + } + + /** + * Returns the high water mark for this file. + */ + public final long getHighwaterMark() + { + return highwaterMark; + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkSelfTestable.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkSelfTestable.java similarity index 92% rename from common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkSelfTestable.java rename to common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkSelfTestable.java index 3231cb0bdb2..2e1bd91120c 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkSelfTestable.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkSelfTestable.java @@ -14,14 +14,15 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.utilities; +package ch.systemsx.cisd.common.highwatermark; import java.io.File; import java.io.IOException; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; -import ch.systemsx.cisd.common.utilities.HighwaterMarkWatcher.HighwaterMarkState; +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.HighwaterMarkState; +import ch.systemsx.cisd.common.utilities.ISelfTestable; /** * An <code>ISelfTestable</code> implementation based on {@link HighwaterMarkWatcher}. diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcher.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java similarity index 92% rename from common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcher.java rename to common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java index 9fffdb2b0f9..5cfa283666b 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcher.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.utilities; +package ch.systemsx.cisd.common.highwatermark; import java.io.File; import java.io.IOException; @@ -29,6 +29,7 @@ import org.apache.log4j.Logger; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.common.utilities.FileUtilities; /** * A high water mark watcher. @@ -97,7 +98,7 @@ public final class HighwaterMarkWatcher implements Runnable public final static boolean isBelow(final HighwaterMarkState highwaterMarkState) { assert highwaterMarkState != null : "Unspecified WatermarkState"; - return highwaterMarkState.freeSpace < highwaterMarkState.highwaterMark; + return highwaterMarkState.freeSpace < highwaterMarkState.getHighwaterMark(); } /** @@ -153,7 +154,8 @@ public final class HighwaterMarkWatcher implements Runnable { assert file != null : "Unspecified file"; final long freeSpaceInKb = freeSpaceProvider.freeSpaceKb(file); - return new HighwaterMarkState(file, highwaterMarkInKb, freeSpaceInKb); + return new HighwaterMarkState(new FileWithHighwaterMark(file, highwaterMarkInKb), + freeSpaceInKb); } /** @@ -188,7 +190,7 @@ public final class HighwaterMarkWatcher implements Runnable if (operationLog.isDebugEnabled()) { operationLog.debug(String.format("Amount of available space on '%s' is: %s.", - FileUtilities.getCanonicalPath(state.path), + state.fileWithHighwaterMark.getCanonicalPath(), displayKilobyteValue(state.freeSpace))); } } catch (final IOException ex) @@ -219,22 +221,19 @@ public final class HighwaterMarkWatcher implements Runnable public final static class HighwaterMarkState { - private final File path; + private final FileWithHighwaterMark fileWithHighwaterMark; private final long freeSpace; - private final long highwaterMark; - - HighwaterMarkState(final File path, final long highwaterMark, final long freeSpace) + HighwaterMarkState(final FileWithHighwaterMark fileWithHighwaterMark, final long freeSpace) { - this.path = path; - this.highwaterMark = highwaterMark; + this.fileWithHighwaterMark = fileWithHighwaterMark; this.freeSpace = freeSpace; } public final File getPath() { - return path; + return fileWithHighwaterMark.getFile(); } /** Returns the free space (in <i>kilobytes</i>). */ @@ -246,7 +245,7 @@ public final class HighwaterMarkWatcher implements Runnable /** Returns the high water mark (in <i>kilobytes</i>). */ public final long getHighwaterMark() { - return highwaterMark; + return fileWithHighwaterMark.getHighwaterMark(); } } @@ -275,7 +274,7 @@ public final class HighwaterMarkWatcher implements Runnable /** Returns the canonical path. */ public final String getPath() { - return FileUtilities.getCanonicalPath(highwaterMarkState.path); + return highwaterMarkState.fileWithHighwaterMark.getCanonicalPath(); } public final long getFreeSpace() @@ -285,7 +284,7 @@ public final class HighwaterMarkWatcher implements Runnable public final long getHighwaterMark() { - return highwaterMarkState.highwaterMark; + return highwaterMarkState.fileWithHighwaterMark.getHighwaterMark(); } } @@ -339,7 +338,7 @@ public final class HighwaterMarkWatcher implements Runnable * * @author Christian Ribeaud */ - static interface IFreeSpaceProvider + public static interface IFreeSpaceProvider { /** diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java new file mode 100644 index 00000000000..c72ce214cb0 --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2008 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.highwatermark; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.fail; + +import java.io.File; +import java.util.Properties; + +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; + +/** + * Test cases for the {@link FileWithHighwaterMarkTest}. + * + * @author Christian Ribeaud + */ +public final class FileWithHighwaterMarkTest +{ + + @Test + public final void testFromProperties() + { + boolean fail = true; + try + { + FileWithHighwaterMark.fromProperties(null, ""); + } catch (final AssertionError e) + { + fail = false; + } + assertFalse(fail); + final String filePropertyKey = "file"; + try + { + FileWithHighwaterMark.fromProperties(new Properties(), filePropertyKey); + fail(""); + } catch (final ConfigurationFailureException e) + { + } + final Properties properties = new Properties(); + final String path = "/my/path"; + properties.setProperty(filePropertyKey, path); + FileWithHighwaterMark fileWithHighwaterMark = + FileWithHighwaterMark.fromProperties(properties, filePropertyKey); + // Default value is -1 + assertEquals(new File(path), fileWithHighwaterMark.getFile()); + assertEquals(-1, fileWithHighwaterMark.getHighwaterMark()); + // 100Kb + properties.setProperty(filePropertyKey + "." + + FileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "100"); + fileWithHighwaterMark = FileWithHighwaterMark.fromProperties(properties, filePropertyKey); + assertEquals(new File(path), fileWithHighwaterMark.getFile()); + assertEquals(100, fileWithHighwaterMark.getHighwaterMark()); + // Meaningless value + properties.setProperty(filePropertyKey + "." + + FileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "notANumber"); + fileWithHighwaterMark = FileWithHighwaterMark.fromProperties(properties, filePropertyKey); + assertEquals(new File(path), fileWithHighwaterMark.getFile()); + assertEquals(-1, fileWithHighwaterMark.getHighwaterMark()); + } +} \ No newline at end of file diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcherTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java similarity index 96% rename from common/sourceTest/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcherTest.java rename to common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java index e5f110e822a..8c7e21586a9 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcherTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcherTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.utilities; +package ch.systemsx.cisd.common.highwatermark; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; @@ -32,9 +32,10 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher; +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.HighwaterMarkState; +import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.IFreeSpaceProvider; import ch.systemsx.cisd.common.logging.BufferedAppender; -import ch.systemsx.cisd.common.utilities.HighwaterMarkWatcher.HighwaterMarkState; -import ch.systemsx.cisd.common.utilities.HighwaterMarkWatcher.IFreeSpaceProvider; /** * Test cases for the {@link HighwaterMarkWatcher}. -- GitLab