diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java deleted file mode 100644 index 43d618498a3cd26e992bbebf2b24c8504c333638..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMark.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.io.Serializable; -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 implements Serializable -{ - - static final String SEP = "-"; - - public static final int DEFAULT_HIGHWATER_MARK = -1; - - private static final long serialVersionUID = 1L; - - /** - * 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 highwaterMarkInKb; - - /** - * @param file the file path. - * @param highwaterMarkInKb the high water mark in <i>kilobytes</i>. <code>-1</code> means - * that the system will not be watching. - */ - public FileWithHighwaterMark(final File file, final long highwaterMarkInKb) - { - this.path = file; - this.highwaterMarkInKb = highwaterMarkInKb; - } - - /** - * @param path the file path. - */ - public FileWithHighwaterMark(final File path) - { - this(path, DEFAULT_HIGHWATER_MARK); - } - - /** - * 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 highwaterMarkInKb = - PropertyUtils.getLong(properties, filePropertyKey.concat(SEP).concat( - HIGHWATER_MARK_PROPERTY_KEY), -1L); - return new FileWithHighwaterMark(new File(filePath), highwaterMarkInKb); - } - - /** - * 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 highwaterMarkInKb; - } -} diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkDirectoryScanningHandler.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkDirectoryScanningHandler.java index 18f9be1d112a5dabb3eeafb34fa4803ed424fb83..df02dcc5e31f7fb7b949390e883e9a4e36649b8d 100644 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkDirectoryScanningHandler.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkDirectoryScanningHandler.java @@ -25,7 +25,7 @@ import ch.systemsx.cisd.common.utilities.DirectoryScanningTimerTask.IScannedStor /** * A <code>DirectoryScanningHandlerInterceptor</code> extension which bases its decision on the - * encapsulated {@link HighwaterMarkWatcher} and {@link FileWithHighwaterMark}s. + * encapsulated {@link HighwaterMarkWatcher} and {@link HostAwareFileWithHighwaterMark}s. * <p> * Note that the decision has precedence over encapsulated {@link IDirectoryScanningHandler} * implementation and might short-circuit it. diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java index 1ae3cc404b0add851526984233f924e84f40032c..efb75a5693dde79f086148cc8c78c9bbc8592ae8 100644 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/HighwaterMarkWatcher.java @@ -200,7 +200,7 @@ public final class HighwaterMarkWatcher implements Runnable { throw new EnvironmentFailureException(errorMsg); } - return new HighwaterMarkState(new FileWithHighwaterMark(file, highwaterMarkInKb), + return new HighwaterMarkState(new HostAwareFileWithHighwaterMark(file, highwaterMarkInKb), freeSpaceInKb); } @@ -236,7 +236,7 @@ public final class HighwaterMarkWatcher implements Runnable if (operationLog.isDebugEnabled()) { operationLog.debug(String.format("Free space on '%s': %s, highwater mark: %s.", - state.fileWithHighwaterMark.getCanonicalPath(), + state.hostAwareFileWithHighwaterMark.getCanonicalPath(), displayKilobyteValue(state.freeSpace), displayKilobyteValue(highwaterMarkInKb))); } @@ -255,19 +255,20 @@ public final class HighwaterMarkWatcher implements Runnable { private static final long serialVersionUID = 1L; - private final FileWithHighwaterMark fileWithHighwaterMark; + private final HostAwareFileWithHighwaterMark hostAwareFileWithHighwaterMark; private final long freeSpace; - HighwaterMarkState(final FileWithHighwaterMark fileWithHighwaterMark, final Long freeSpace) + HighwaterMarkState(final HostAwareFileWithHighwaterMark hostAwareFileWithHighwaterMark, + final Long freeSpace) { - this.fileWithHighwaterMark = fileWithHighwaterMark; + this.hostAwareFileWithHighwaterMark = hostAwareFileWithHighwaterMark; this.freeSpace = freeSpace; } public final File getPath() { - return fileWithHighwaterMark.getFile(); + return hostAwareFileWithHighwaterMark.getFile(); } /** Returns the free space (in <i>kilobytes</i>). */ @@ -279,7 +280,7 @@ public final class HighwaterMarkWatcher implements Runnable /** Returns the high water mark (in <i>kilobytes</i>). */ public final long getHighwaterMark() { - return fileWithHighwaterMark.getHighwaterMark(); + return hostAwareFileWithHighwaterMark.getHighwaterMark(); } } @@ -308,7 +309,7 @@ public final class HighwaterMarkWatcher implements Runnable /** Returns the canonical path. */ public final String getPath() { - return highwaterMarkState.fileWithHighwaterMark.getCanonicalPath(); + return highwaterMarkState.hostAwareFileWithHighwaterMark.getCanonicalPath(); } public final long getFreeSpace() @@ -318,7 +319,7 @@ public final class HighwaterMarkWatcher implements Runnable public final long getHighwaterMark() { - return highwaterMarkState.fileWithHighwaterMark.getHighwaterMark(); + return highwaterMarkState.hostAwareFileWithHighwaterMark.getHighwaterMark(); } } diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/HostAwareFileWithHighwaterMark.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/HostAwareFileWithHighwaterMark.java new file mode 100644 index 0000000000000000000000000000000000000000..f5ec28f67d2d50b12c7a429e6d7a055ea52e6c8c --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/HostAwareFileWithHighwaterMark.java @@ -0,0 +1,172 @@ +/* + * 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.io.Serializable; +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. + * <p> + * This file could be located on an external host. + * </p> + * + * @author Christian Ribeaud + */ +public final class HostAwareFileWithHighwaterMark extends AbstractHashable implements Serializable +{ + private static final long serialVersionUID = 1L; + + static final String SEP = "-"; + + public static final char HOST_FILE_SEP = ':'; + + public static final int DEFAULT_HIGHWATER_MARK = -1; + + /** + * 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 String host; + + private final File path; + + private final long highwaterMarkInKb; + + /** + * @param hostOrNull the host on which given <var>file</var> is located. + * @param file the file path. + * @param highwaterMarkInKb the high water mark in <i>kilobytes</i>. <code>-1</code> means + * that the system will not be watching. + */ + public HostAwareFileWithHighwaterMark(final String hostOrNull, final File file, + final long highwaterMarkInKb) + { + this.host = hostOrNull; + this.path = file; + this.highwaterMarkInKb = highwaterMarkInKb; + } + + /** + * @param path the file path. + * @param highwaterMarkInKb the high water mark in <i>kilobytes</i>. <code>-1</code> means + * that the system will not be watching. + */ + public HostAwareFileWithHighwaterMark(final File path, final long highwaterMarkInKb) + { + this(null, path, highwaterMarkInKb); + } + + /** + * @param path the file path. + */ + public HostAwareFileWithHighwaterMark(final File path) + { + this(path, DEFAULT_HIGHWATER_MARK); + } + + /** + * @param hostOrNull the host on which given <var>file</var> is located. + * @param path the file path. + */ + public HostAwareFileWithHighwaterMark(final String hostOrNull, final File path) + { + this(hostOrNull, path, DEFAULT_HIGHWATER_MARK); + } + + /** + * Instantiates a new <code>FileWithHighwaterMark</code> from given <var>properties</var>. + * Some examples: + * + * <pre> + * <host-file-property-key> = /temp + * <host-file-property-key> = localhost:/temp + * <host-file-property-key>-highwater-mark = 123456 + * </pre> + * + * @param hostFilePropertyKey the property key under which the host/file path can be found. + * @throws ConfigurationFailureException if given <var>hostFilePropertyKey</var> could not be + * found in given <var>properties</var>. + */ + public final static HostAwareFileWithHighwaterMark fromProperties(final Properties properties, + final String hostFilePropertyKey) throws ConfigurationFailureException + { + assert properties != null : "Unspecified properties"; + assert StringUtils.isNotBlank(hostFilePropertyKey) : "Host-file property key is blank"; + final String hostFile = PropertyUtils.getMandatoryProperty(properties, hostFilePropertyKey); + final String filePath; + String hostNameOrNull = null; + final int index = hostFile.indexOf(HOST_FILE_SEP); + if (index > -1) + { + hostNameOrNull = hostFile.substring(0, index); + filePath = hostFile.substring(index + 1); + } else + { + filePath = hostFile; + } + final long highwaterMarkInKb = + PropertyUtils.getLong(properties, hostFilePropertyKey.concat(SEP).concat( + HIGHWATER_MARK_PROPERTY_KEY), -1L); + return new HostAwareFileWithHighwaterMark(hostNameOrNull, new File(filePath), + highwaterMarkInKb); + } + + /** Returns the host on which {@link #getFile()} is located on. */ + public final String tryGetHost() + { + return host; + } + + /** + * Returns the file path. + */ + public final File getFile() + { + return path; + } + + /** + * Returns the high water mark for this file. + */ + public final long getHighwaterMark() + { + return highwaterMarkInKb; + } + + /** Return the canonical path of the encapsulated <code>path</code>. */ + public final String getCanonicalPath() + { + if (tryGetHost() == null) + { + return FileUtilities.getCanonicalPath(getFile()); + } else + { + return tryGetHost() + HOST_FILE_SEP + getFile(); + } + } +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HostAwareFileWithHighwaterMarkTest.java similarity index 52% rename from common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java rename to common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HostAwareFileWithHighwaterMarkTest.java index 7c49ac89eb2ad55db3d7ef9c35f09c466a72e0d7..07d88dda4da0ffcc0386d80b6c4b78d1e85a8ae2 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/FileWithHighwaterMarkTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/highwatermark/HostAwareFileWithHighwaterMarkTest.java @@ -28,11 +28,11 @@ import org.testng.annotations.Test; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; /** - * Test cases for the {@link FileWithHighwaterMarkTest}. + * Test cases for the {@link HostAwareFileWithHighwaterMark}. * * @author Christian Ribeaud */ -public final class FileWithHighwaterMarkTest +public final class HostAwareFileWithHighwaterMarkTest { @Test @@ -41,7 +41,7 @@ public final class FileWithHighwaterMarkTest boolean fail = true; try { - FileWithHighwaterMark.fromProperties(null, ""); + HostAwareFileWithHighwaterMark.fromProperties(null, ""); } catch (final AssertionError e) { fail = false; @@ -50,7 +50,7 @@ public final class FileWithHighwaterMarkTest final String filePropertyKey = "file"; try { - FileWithHighwaterMark.fromProperties(new Properties(), filePropertyKey); + HostAwareFileWithHighwaterMark.fromProperties(new Properties(), filePropertyKey); fail(""); } catch (final ConfigurationFailureException e) { @@ -58,22 +58,40 @@ public final class FileWithHighwaterMarkTest final Properties properties = new Properties(); final String path = "/my/path"; properties.setProperty(filePropertyKey, path); - FileWithHighwaterMark fileWithHighwaterMark = - FileWithHighwaterMark.fromProperties(properties, filePropertyKey); + HostAwareFileWithHighwaterMark fileWithHighwaterMark = + HostAwareFileWithHighwaterMark.fromProperties(properties, filePropertyKey); // Default value is -1 assertEquals(new File(path), fileWithHighwaterMark.getFile()); assertEquals(-1, fileWithHighwaterMark.getHighwaterMark()); // 100Kb - properties.setProperty(filePropertyKey + FileWithHighwaterMark.SEP - + FileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "100"); - fileWithHighwaterMark = FileWithHighwaterMark.fromProperties(properties, filePropertyKey); + properties.setProperty(filePropertyKey + HostAwareFileWithHighwaterMark.SEP + + HostAwareFileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "100"); + fileWithHighwaterMark = + HostAwareFileWithHighwaterMark.fromProperties(properties, filePropertyKey); assertEquals(new File(path), fileWithHighwaterMark.getFile()); assertEquals(100, fileWithHighwaterMark.getHighwaterMark()); // Meaningless value - properties.setProperty(filePropertyKey + FileWithHighwaterMark.SEP - + FileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "notANumber"); - fileWithHighwaterMark = FileWithHighwaterMark.fromProperties(properties, filePropertyKey); + properties.setProperty(filePropertyKey + HostAwareFileWithHighwaterMark.SEP + + HostAwareFileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "notANumber"); + fileWithHighwaterMark = + HostAwareFileWithHighwaterMark.fromProperties(properties, filePropertyKey); assertEquals(new File(path), fileWithHighwaterMark.getFile()); assertEquals(-1, fileWithHighwaterMark.getHighwaterMark()); } + + @Test + public final void testFromPropertiesWithHost() + { + final Properties properties = new Properties(); + final String hostFilePath = "localhost:/my/path"; + final String key = "key"; + properties.setProperty(key, hostFilePath); + properties.setProperty(key + HostAwareFileWithHighwaterMark.SEP + + HostAwareFileWithHighwaterMark.HIGHWATER_MARK_PROPERTY_KEY, "123"); + final HostAwareFileWithHighwaterMark fileWithHighwaterMark = + HostAwareFileWithHighwaterMark.fromProperties(properties, key); + assertEquals(fileWithHighwaterMark.getHighwaterMark(), 123L); + assertEquals(fileWithHighwaterMark.tryGetHost(), "localhost"); + assertEquals(fileWithHighwaterMark.getFile(), new File("/my/path")); + } } \ No newline at end of file