Skip to content
Snippets Groups Projects
Commit 842f21e9 authored by ribeaudc's avatar ribeaudc
Browse files

change: - Move 'HighwaterMark' stuff in its own package.

SVN: 6037
parent 3b63906c
No related branches found
No related tags found
No related merge requests found
/*
* 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;
}
}
......@@ -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}.
......
......@@ -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
{
/**
......
/*
* 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
......@@ -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}.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment