diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/WatermarkSelfTestable.java b/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkSelfTestable.java similarity index 57% rename from common/source/java/ch/systemsx/cisd/common/utilities/WatermarkSelfTestable.java rename to common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkSelfTestable.java index bcbe634b415952f8f4b43a16d51f3241ef50a946..3231cb0bdb294077f68ca964b3cef5df0e7352a0 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/WatermarkSelfTestable.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkSelfTestable.java @@ -21,24 +21,25 @@ import java.io.IOException; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; -import ch.systemsx.cisd.common.utilities.WatermarkWatcher.WatermarkState; +import ch.systemsx.cisd.common.utilities.HighwaterMarkWatcher.HighwaterMarkState; /** - * An <code>ISelfTestable</code> implementation based on {@link WatermarkWatcher}. + * An <code>ISelfTestable</code> implementation based on {@link HighwaterMarkWatcher}. * * @author Christian Ribeaud */ -public final class WatermarkSelfTestable implements ISelfTestable +public final class HighwaterMarkSelfTestable implements ISelfTestable { private final File path; - private final WatermarkWatcher watermarkWatcher; + private final HighwaterMarkWatcher highwaterMarkWatcher; - public WatermarkSelfTestable(final File path, final WatermarkWatcher watermarkWatcher) + public HighwaterMarkSelfTestable(final File path, + final HighwaterMarkWatcher highwaterMarkWatcher) { this.path = path; - this.watermarkWatcher = watermarkWatcher; + this.highwaterMarkWatcher = highwaterMarkWatcher; } // @@ -49,16 +50,19 @@ public final class WatermarkSelfTestable implements ISelfTestable { try { - final WatermarkState watermarkState = watermarkWatcher.getWatermarkState(path); - if (WatermarkWatcher.isBelow(watermarkState)) + final HighwaterMarkState highwaterMarkState = + highwaterMarkWatcher.getHighwaterMarkState(path); + if (HighwaterMarkWatcher.isBelow(highwaterMarkState)) { final String freeSpaceDisplayed = - WatermarkWatcher.displayKilobyteValue(watermarkState.getFreeSpace()); - final String watermarkDisplayed = - WatermarkWatcher.displayKilobyteValue(watermarkState.getWatermark()); + HighwaterMarkWatcher + .displayKilobyteValue(highwaterMarkState.getFreeSpace()); + final String highwaterMarkDisplayed = + HighwaterMarkWatcher.displayKilobyteValue(highwaterMarkState + .getHighwaterMark()); throw ConfigurationFailureException.fromTemplate( - "Free space (%s) lies below given watermark (%s).", freeSpaceDisplayed, - watermarkDisplayed); + "Free space (%s) lies below given high water mark (%s).", + freeSpaceDisplayed, highwaterMarkDisplayed); } } catch (IOException ex) { diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/WatermarkWatcher.java b/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcher.java similarity index 66% rename from common/source/java/ch/systemsx/cisd/common/utilities/WatermarkWatcher.java rename to common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcher.java index 3114db75a41bd1720664ac348bf481d2430bcc26..9fffdb2b0f975b570b511d7af56d6cbe1f141b45 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/WatermarkWatcher.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcher.java @@ -31,7 +31,7 @@ import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; /** - * A watermark watcher. + * A high water mark watcher. * <p> * This class is thread-safe. * </p> @@ -39,53 +39,53 @@ import ch.systemsx.cisd.common.logging.LogFactory; * @see FileSystemUtils * @author Christian Ribeaud */ -public final class WatermarkWatcher implements Runnable +public final class HighwaterMarkWatcher implements Runnable { private final static IFreeSpaceProvider DEFAULT_FREE_SPACE_PROVIDER = new DefaultFreeSpaceProvider(); private static final Logger operationLog = - LogFactory.getLogger(LogCategory.OPERATION, WatermarkWatcher.class); + LogFactory.getLogger(LogCategory.OPERATION, HighwaterMarkWatcher.class); /** - * The watermark value in <i>kilobytes</i>. + * The high water mark value in <i>kilobytes</i>. */ - private final long watermarkInKb; + private final long highwaterMarkInKb; private final EventListenerList listenerList = new EventListenerList(); /** The path to get free space for. Is not <code>null</code>, not empty on Unix. */ private File path; - /** The last {@link WatermarkState} computed. */ - private WatermarkState watermarkState; + /** The last {@link HighwaterMarkState} computed. */ + private HighwaterMarkState highwaterMarkState; private final IFreeSpaceProvider freeSpaceProvider; /** - * @param watermark the watermark value in kilobytes. If negative, then {@link #run()} always - * returns without doing anything. + * @param highwaterMarkInKb the high water mark value in kilobytes. If negative, then + * {@link #run()} always returns without doing anything. */ - public WatermarkWatcher(final long watermark) + public HighwaterMarkWatcher(final long highwaterMarkInKb) { - this(watermark, DEFAULT_FREE_SPACE_PROVIDER); + this(highwaterMarkInKb, DEFAULT_FREE_SPACE_PROVIDER); } - WatermarkWatcher(final long watermarkInKb, final IFreeSpaceProvider freeSpaceProvider) + HighwaterMarkWatcher(final long highwaterMarkInKb, final IFreeSpaceProvider freeSpaceProvider) { assert freeSpaceProvider != null : "Unspecified IFreeSpaceProvider"; - this.watermarkInKb = watermarkInKb; + this.highwaterMarkInKb = highwaterMarkInKb; this.freeSpaceProvider = freeSpaceProvider; addChangeListener(new NotificationLogChangeListener()); } - private final void fireChangeListeners(final WatermarkEvent watermarkEvent) + private final void fireChangeListeners(final HighwaterMarkEvent highwaterMarkEvent) { final ChangeListener[] listeners = listenerList.getListeners(ChangeListener.class); for (final ChangeListener changeListener : listeners) { - changeListener.stateChanged(watermarkEvent); + changeListener.stateChanged(highwaterMarkEvent); } } @@ -94,14 +94,14 @@ public final class WatermarkWatcher implements Runnable return FileUtils.byteCountToDisplaySize(value * FileUtils.ONE_KB); } - public final static boolean isBelow(final WatermarkState watermarkState) + public final static boolean isBelow(final HighwaterMarkState highwaterMarkState) { - assert watermarkState != null : "Unspecified WatermarkState"; - return watermarkState.freeSpace < watermarkState.watermark; + assert highwaterMarkState != null : "Unspecified WatermarkState"; + return highwaterMarkState.freeSpace < highwaterMarkState.highwaterMark; } /** - * Adds a <code>ChangeListener</code> to this watermark watcher. + * Adds a <code>ChangeListener</code> to this high water mark watcher. */ public final synchronized void addChangeListener(final ChangeListener changeListener) { @@ -110,7 +110,7 @@ public final class WatermarkWatcher implements Runnable } /** - * Removes given <code>ChangeListener</code> from this watermark watcher. + * Removes given <code>ChangeListener</code> from this high water mark watcher. */ public final synchronized void removeChangeListener(final ChangeListener changeListener) { @@ -119,11 +119,11 @@ public final class WatermarkWatcher implements Runnable } /** - * Whether the free space is below the watermark or not. + * Whether the free space is below the high water mark or not. */ public final synchronized boolean isBelow() { - return watermarkState == null ? false : isBelow(watermarkState); + return highwaterMarkState == null ? false : isBelow(highwaterMarkState); } /** @@ -147,21 +147,21 @@ public final class WatermarkWatcher implements Runnable } /** - * Analyzes given <var>path</var> and returns a {@link WatermarkState}. + * Analyzes given <var>path</var> and returns a {@link HighwaterMarkState}. */ - public final WatermarkState getWatermarkState(final File file) throws IOException + public final HighwaterMarkState getHighwaterMarkState(final File file) throws IOException { assert file != null : "Unspecified file"; final long freeSpaceInKb = freeSpaceProvider.freeSpaceKb(file); - return new WatermarkState(file, watermarkInKb, freeSpaceInKb); + return new HighwaterMarkState(file, highwaterMarkInKb, freeSpaceInKb); } /** - * Returns the watermark (in <i>kilobytes</i>) specified in the constructor. + * Returns the high water mark (in <i>kilobytes</i>) specified in the constructor. */ - public final long getWatermark() + public final long getHighwaterMark() { - return watermarkInKb; + return highwaterMarkInKb; } // @@ -171,19 +171,19 @@ public final class WatermarkWatcher implements Runnable public final synchronized void run() { assert path != null : "Unspecified path"; - if (watermarkInKb < 0) + if (highwaterMarkInKb < 0) { return; } try { - final WatermarkState state = getWatermarkState(path); + final HighwaterMarkState state = getHighwaterMarkState(path); final boolean newBelowValue = isBelow(state); final boolean stateChanged = isBelow() != newBelowValue; - watermarkState = state; + highwaterMarkState = state; if (stateChanged) { - fireChangeListeners(new WatermarkEvent(this, state)); + fireChangeListeners(new HighwaterMarkEvent(this, state)); } if (operationLog.isDebugEnabled()) { @@ -194,7 +194,8 @@ public final class WatermarkWatcher implements Runnable } catch (final IOException ex) { operationLog.error( - "The watermark watcher can not work properly due to an I/O exception.", ex); + "The high water mark watcher can not work properly due to an I/O exception.", + ex); } } @@ -216,18 +217,18 @@ public final class WatermarkWatcher implements Runnable } } - public final static class WatermarkState + public final static class HighwaterMarkState { private final File path; private final long freeSpace; - private final long watermark; + private final long highwaterMark; - WatermarkState(final File path, final long watermark, final long freeSpace) + HighwaterMarkState(final File path, final long highwaterMark, final long freeSpace) { this.path = path; - this.watermark = watermark; + this.highwaterMark = highwaterMark; this.freeSpace = freeSpace; } @@ -242,49 +243,49 @@ public final class WatermarkWatcher implements Runnable return freeSpace; } - /** Returns the watermark (in <i>kilobytes</i>). */ - public final long getWatermark() + /** Returns the high water mark (in <i>kilobytes</i>). */ + public final long getHighwaterMark() { - return watermark; + return highwaterMark; } } - public final static class WatermarkEvent extends ChangeEvent + public final static class HighwaterMarkEvent extends ChangeEvent { private static final long serialVersionUID = 1L; - private final WatermarkState watermarkState; + private final HighwaterMarkState highwaterMarkState; - WatermarkEvent(final Object source, final WatermarkState watermarkState) + HighwaterMarkEvent(final Object source, final HighwaterMarkState highwaterMarkState) { super(source); - this.watermarkState = watermarkState; + this.highwaterMarkState = highwaterMarkState; } /** - * Whether the free space is below or reaches the watermark. + * Whether the free space is below or reaches the high water mark. */ public final boolean isBelow() { - return WatermarkWatcher.isBelow(watermarkState); + return HighwaterMarkWatcher.isBelow(highwaterMarkState); } /** Returns the canonical path. */ public final String getPath() { - return FileUtilities.getCanonicalPath(watermarkState.path); + return FileUtilities.getCanonicalPath(highwaterMarkState.path); } public final long getFreeSpace() { - return watermarkState.freeSpace; + return highwaterMarkState.freeSpace; } - public final long getWatermark() + public final long getHighwaterMark() { - return watermarkState.watermark; + return highwaterMarkState.highwaterMark; } } @@ -298,11 +299,11 @@ public final class WatermarkWatcher implements Runnable { static final String INFO_LOG_FORMAT = "The amount of available space (%s) on '%s' " - + "is again sufficient (greater than the specified watermark: %s)."; + + "is again sufficient (greater than the specified high water mark: %s)."; static final String WARNING_LOG_FORMAT = "The amount of available space (%s) on '%s' " - + "is lower than the specified watermark (%s)."; + + "is lower than the specified high water mark (%s)."; private static final Logger notificationLog = LogFactory.getLogger(LogCategory.NOTIFY, NotificationLogChangeListener.class); @@ -317,18 +318,18 @@ public final class WatermarkWatcher implements Runnable public final void stateChanged(final ChangeEvent e) { - final WatermarkEvent event = (WatermarkEvent) e; + final HighwaterMarkEvent event = (HighwaterMarkEvent) e; final String path = event.getPath(); - final String watermarkDisplayed = displayKilobyteValue(event.getWatermark()); + final String highwaterMarkDisplayed = displayKilobyteValue(event.getHighwaterMark()); final String freeSpaceDisplayed = displayKilobyteValue(event.getFreeSpace()); if (event.isBelow()) { notificationLog.warn(String.format(WARNING_LOG_FORMAT, freeSpaceDisplayed, path, - watermarkDisplayed)); + highwaterMarkDisplayed)); } else { notificationLog.info(String.format(INFO_LOG_FORMAT, freeSpaceDisplayed, path, - watermarkDisplayed)); + highwaterMarkDisplayed)); } } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/WatermarkWatcherTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcherTest.java similarity index 66% rename from common/sourceTest/java/ch/systemsx/cisd/common/utilities/WatermarkWatcherTest.java rename to common/sourceTest/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcherTest.java index 1fde994686ed03dae56d5682d88a0ddbdf52b278..e5f110e822a33f406df139ac7648a34e2d10d6b5 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/WatermarkWatcherTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/HighwaterMarkWatcherTest.java @@ -33,15 +33,15 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import ch.systemsx.cisd.common.logging.BufferedAppender; -import ch.systemsx.cisd.common.utilities.WatermarkWatcher.IFreeSpaceProvider; -import ch.systemsx.cisd.common.utilities.WatermarkWatcher.WatermarkState; +import ch.systemsx.cisd.common.utilities.HighwaterMarkWatcher.HighwaterMarkState; +import ch.systemsx.cisd.common.utilities.HighwaterMarkWatcher.IFreeSpaceProvider; /** - * Test cases for the {@link WatermarkWatcher}. + * Test cases for the {@link HighwaterMarkWatcher}. * * @author Christian Ribeaud */ -public final class WatermarkWatcherTest +public final class HighwaterMarkWatcherTest { private static final File DEFAULT_PATH = new File("/my/path"); @@ -49,7 +49,7 @@ public final class WatermarkWatcherTest private Mockery context; - private WatermarkWatcher watermarkWatcher; + private HighwaterMarkWatcher highwaterMarkWatcher; private IFreeSpaceProvider freeSpaceProvider; @@ -61,12 +61,12 @@ public final class WatermarkWatcherTest context = new Mockery(); logRecorder = new BufferedAppender("%m", Level.INFO); freeSpaceProvider = context.mock(IFreeSpaceProvider.class); - watermarkWatcher = createWatermarkWatcher(DEFAULT_WATERMARK); + highwaterMarkWatcher = createHighwaterMarkWatcher(DEFAULT_WATERMARK); } - private final WatermarkWatcher createWatermarkWatcher(final long watermark) + private final HighwaterMarkWatcher createHighwaterMarkWatcher(final long highwaterMark) { - return new WatermarkWatcher(watermark, freeSpaceProvider); + return new HighwaterMarkWatcher(highwaterMark, freeSpaceProvider); } @AfterMethod @@ -78,8 +78,8 @@ public final class WatermarkWatcherTest context.assertIsSatisfied(); } - @DataProvider(name = "watermarks") - public final Object[][] getWatermarks() + @DataProvider(name = "highwaterMarks") + public final Object[][] getHighwaterMarks() { return new Object[][] { @@ -88,11 +88,11 @@ public final class WatermarkWatcherTest { 100 } }; } - @Test(dataProvider = "watermarks") - public final void testConstructor(final long watermark) + @Test(dataProvider = "highwaterMarks") + public final void testConstructor(final long highwaterMark) { - final WatermarkWatcher watcher = new WatermarkWatcher(watermark); - assertEquals(watermark, watcher.getWatermark()); + final HighwaterMarkWatcher watcher = new HighwaterMarkWatcher(highwaterMark); + assertEquals(highwaterMark, watcher.getHighwaterMark()); context.assertIsSatisfied(); } @@ -102,13 +102,13 @@ public final class WatermarkWatcherTest boolean fail = true; try { - watermarkWatcher.run(); + highwaterMarkWatcher.run(); } catch (final AssertionError e) { fail = false; } assertFalse(fail); - final WatermarkWatcher watcher = new WatermarkWatcher(-1); + final HighwaterMarkWatcher watcher = new HighwaterMarkWatcher(-1); watcher.setPath(DEFAULT_PATH); // -1 means infinity, so no call to IFreeSpaceProvider watcher.run(); @@ -127,8 +127,8 @@ public final class WatermarkWatcherTest @Test(dataProvider = "freeSpaces") public final void testIsBelow(final long freeSpace, final boolean isBelow) throws IOException { - assertFalse(watermarkWatcher.isBelow()); - watermarkWatcher.setPath(DEFAULT_PATH); + assertFalse(highwaterMarkWatcher.isBelow()); + highwaterMarkWatcher.setPath(DEFAULT_PATH); context.checking(new Expectations() { { @@ -136,25 +136,25 @@ public final class WatermarkWatcherTest will(returnValue(freeSpace)); } }); - watermarkWatcher.run(); - assertEquals(isBelow, watermarkWatcher.isBelow()); + highwaterMarkWatcher.run(); + assertEquals(isBelow, highwaterMarkWatcher.isBelow()); context.assertIsSatisfied(); } @Test public final void testIsBelowWithNegativeValue() throws IOException { - final WatermarkWatcher watcher = createWatermarkWatcher(-1); + final HighwaterMarkWatcher watcher = createHighwaterMarkWatcher(-1); watcher.setPath(DEFAULT_PATH); watcher.run(); - assertEquals(false, watermarkWatcher.isBelow()); + assertEquals(false, highwaterMarkWatcher.isBelow()); context.assertIsSatisfied(); } @Test public final void testIsBelowWithZero() throws IOException { - final WatermarkWatcher watcher = createWatermarkWatcher(0); + final HighwaterMarkWatcher watcher = createHighwaterMarkWatcher(0); watcher.setPath(DEFAULT_PATH); context.checking(new Expectations() { @@ -164,7 +164,7 @@ public final class WatermarkWatcherTest } }); watcher.run(); - assertEquals(false, watermarkWatcher.isBelow()); + assertEquals(false, highwaterMarkWatcher.isBelow()); context.assertIsSatisfied(); } @@ -196,32 +196,34 @@ public final class WatermarkWatcherTest } }); // Space becomes tight. So inform the administrator. - watermarkWatcher.setPathAndRun(DEFAULT_PATH); + highwaterMarkWatcher.setPathAndRun(DEFAULT_PATH); assertEquals(String.format( - WatermarkWatcher.NotificationLogChangeListener.WARNING_LOG_FORMAT, WatermarkWatcher - .displayKilobyteValue(freeSpaces[i]), DEFAULT_PATH, WatermarkWatcher - .displayKilobyteValue(DEFAULT_WATERMARK)), logRecorder.getLogContent()); + HighwaterMarkWatcher.NotificationLogChangeListener.WARNING_LOG_FORMAT, + HighwaterMarkWatcher.displayKilobyteValue(freeSpaces[i]), DEFAULT_PATH, + HighwaterMarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK)), logRecorder + .getLogContent()); // Space still "red". Do not inform the administrator. He already knows it. logRecorder.resetLogContent(); - watermarkWatcher.setPathAndRun(DEFAULT_PATH); + highwaterMarkWatcher.setPathAndRun(DEFAULT_PATH); assertEquals("", logRecorder.getLogContent()); // Space becomes again OK. So inform the administrator. i++; logRecorder.resetLogContent(); - watermarkWatcher.setPathAndRun(DEFAULT_PATH); - assertEquals(String.format(WatermarkWatcher.NotificationLogChangeListener.INFO_LOG_FORMAT, - WatermarkWatcher.displayKilobyteValue(freeSpaces[i]), DEFAULT_PATH, - WatermarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK)), logRecorder + highwaterMarkWatcher.setPathAndRun(DEFAULT_PATH); + assertEquals(String.format( + HighwaterMarkWatcher.NotificationLogChangeListener.INFO_LOG_FORMAT, + HighwaterMarkWatcher.displayKilobyteValue(freeSpaces[i]), DEFAULT_PATH, + HighwaterMarkWatcher.displayKilobyteValue(DEFAULT_WATERMARK)), logRecorder .getLogContent()); // Space still OK. Do not inform the administrator. logRecorder.resetLogContent(); - watermarkWatcher.setPathAndRun(DEFAULT_PATH); + highwaterMarkWatcher.setPathAndRun(DEFAULT_PATH); assertEquals("", logRecorder.getLogContent()); context.assertIsSatisfied(); } @Test - public final void testWatermarkState() throws IOException + public final void testHighwaterMarkState() throws IOException { final long freeSpace = 123L; context.checking(new Expectations() @@ -231,11 +233,12 @@ public final class WatermarkWatcherTest will(returnValue(freeSpace)); } }); - final WatermarkState watermarkState = watermarkWatcher.getWatermarkState(DEFAULT_PATH); - assertEquals(watermarkState.getFreeSpace(), freeSpace); - assertEquals(watermarkState.getPath(), DEFAULT_PATH); - assertEquals(watermarkState.getWatermark(), DEFAULT_WATERMARK); - assertFalse(WatermarkWatcher.isBelow(watermarkState)); + final HighwaterMarkState highwaterMarkState = + highwaterMarkWatcher.getHighwaterMarkState(DEFAULT_PATH); + assertEquals(highwaterMarkState.getFreeSpace(), freeSpace); + assertEquals(highwaterMarkState.getPath(), DEFAULT_PATH); + assertEquals(highwaterMarkState.getHighwaterMark(), DEFAULT_WATERMARK); + assertFalse(HighwaterMarkWatcher.isBelow(highwaterMarkState)); context.assertIsSatisfied(); } } \ No newline at end of file