diff --git a/common/source/java/ch/systemsx/cisd/common/filesystem/ILastModificationChecker.java b/common/source/java/ch/systemsx/cisd/common/filesystem/ILastModificationChecker.java index 902a30646c168a45549ed7b92c54a860d220a68f..86c14bd27b22b297e2ef79990cb9b1066f10d190 100644 --- a/common/source/java/ch/systemsx/cisd/common/filesystem/ILastModificationChecker.java +++ b/common/source/java/ch/systemsx/cisd/common/filesystem/ILastModificationChecker.java @@ -24,14 +24,28 @@ public interface ILastModificationChecker * Returns the last time when there was a write access to <var>item</var>. * * @param item The {@link StoreItem} to check. - * @param stopWhenFindYounger The time measured from the beginning of the epoch. If > 0, - * the recursive search for younger file will be stopped when a file or directory - * is found that is younger than the time specified in this parameter. Supposed - * to be used when one does not care about the absolutely youngest entry, but - * only, if there are entries that are "young enough". - * @return The time (in milliseconds since the start of the epoch) when <var>resource</var> - * was last changed or error status if checking failed. + * @param stopWhenFindYounger The time measured from the beginning of the epoch. If > 0, the + * recursive search for younger file will be stopped when a file or directory is + * found that is younger than the time specified in this parameter. Supposed to be + * used when one does not care about the absolutely youngest entry, but only, if + * there are entries that are "young enough". + * @return The time (in milliseconds since the start of the epoch) when <var>resource</var> was + * last changed or error status if checking failed. */ public StatusWithResult<Long> lastChanged(StoreItem item, long stopWhenFindYounger); + /** + * Returns the last time when there was a write access to <var>item</var>. + * + * @param item The {@link StoreItem} to check. + * @param stopWhenFindYoungerRelative The age of the item. If > 0, the recursive search for + * younger file will be stopped when a file or directory is found that is younger + * than the specified age (in other words is smaller than + * <code>System.currentTimeMillis() - stopWhenYoungerRelative</code>). + * @return The time (in milliseconds since the start of the epoch) when <var>resource</var> was + * last changed or error status if checking failed. + */ + public StatusWithResult<Long> lastChangedRelative(StoreItem item, + long stopWhenFindYoungerRelative); + } \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/filesystem/LastModificationChecker.java b/common/source/java/ch/systemsx/cisd/common/filesystem/LastModificationChecker.java new file mode 100644 index 0000000000000000000000000000000000000000..b6e7a3ef88c752fe774b920ea496f0469bd92a6c --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/filesystem/LastModificationChecker.java @@ -0,0 +1,79 @@ +/* + * Copyright 2009 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.filesystem; + +import java.io.File; + +import ch.systemsx.cisd.common.exceptions.StatusWithResult; +import ch.systemsx.cisd.common.exceptions.UnknownLastChangedException; + +/** + * @author Tomasz Pylak + */ +public class LastModificationChecker implements ILastModificationChecker +{ + private final File parentDir; + + public LastModificationChecker(File parentDir) + { + this.parentDir = parentDir; + } + + public final StatusWithResult<Long> lastChanged(final StoreItem item, + final long stopWhenFindYounger) + { + try + { + long lastChanged = + FileUtilities.lastChanged(getChildFile(item), true, stopWhenFindYounger); + return StatusWithResult.<Long> create(lastChanged); + } catch (UnknownLastChangedException ex) + { + return createLastChangedError(item, ex); + } + } + + public final StatusWithResult<Long> lastChangedRelative(final StoreItem item, + final long stopWhenFindYoungerRelative) + { + try + { + long lastChanged = + FileUtilities.lastChangedRelative(getChildFile(item), true, + stopWhenFindYoungerRelative); + return StatusWithResult.<Long> create(lastChanged); + } catch (UnknownLastChangedException ex) + { + return createLastChangedError(item, ex); + } + } + + private static StatusWithResult<Long> createLastChangedError(final StoreItem item, + UnknownLastChangedException ex) + { + String errorMsg = + String.format("Could not determine \"last changed time\" of %s: %s", item, ex + .getCause()); + return StatusWithResult.<Long> createError(errorMsg); + } + + protected final File getChildFile(final StoreItem item) + { + return new File(parentDir, item.getName()); + } + +} diff --git a/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/intf/IFileStore.java b/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/intf/IFileStore.java index b56e8fa32ee0b98eb1470a0262e564b71d616ce6..7ccdfd21f5ffe109096a74443b8423e394fc5fcd 100644 --- a/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/intf/IFileStore.java +++ b/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/intf/IFileStore.java @@ -62,31 +62,10 @@ public interface IFileStore extends ISelfTestable, ILastModificationChecker */ public BooleanStatus exists(StoreItem item); - /** - * Returns the last time when there was a write access to <var>item</var>. - * - * @param item The {@link StoreItem} to check. - * @param stopWhenFindYounger The time measured from the beginning of the epoch. If > 0, the - * recursive search for younger file will be stopped when a file or directory is - * found that is younger than the time specified in this parameter. Supposed to be - * used when one does not care about the absolutely youngest entry, but only, if - * there are entries that are "young enough". - * @return The time (in milliseconds since the start of the epoch) when <var>resource</var> was - * last changed or error status if checking failed. - */ + /** @see ILastModificationChecker#lastChanged(StoreItem, long) */ public StatusWithResult<Long> lastChanged(StoreItem item, long stopWhenFindYounger); - /** - * Returns the last time when there was a write access to <var>item</var>. - * - * @param item The {@link StoreItem} to check. - * @param stopWhenFindYoungerRelative The age of the item. If > 0, the recursive search for - * younger file will be stopped when a file or directory is found that is younger - * than the specified age (in other words is smaller than - * <code>System.currentTimeMillis() - stopWhenYoungerRelative</code>). - * @return The time (in milliseconds since the start of the epoch) when <var>resource</var> was - * last changed or error status if checking failed. - */ + /** @see ILastModificationChecker#lastChangedRelative(StoreItem, long) */ public StatusWithResult<Long> lastChangedRelative(StoreItem item, long stopWhenFindYoungerRelative); diff --git a/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/store/FileStoreLocal.java b/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/store/FileStoreLocal.java index 369aee1a8084f3856e6edd69e7ff712c39d12b76..c1509da0b89d8d764ba1e36b65b471605b5f538b 100644 --- a/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/store/FileStoreLocal.java +++ b/datamover/source/java/ch/systemsx/cisd/datamover/filesystem/store/FileStoreLocal.java @@ -26,8 +26,8 @@ import org.apache.log4j.Logger; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.exceptions.Status; import ch.systemsx.cisd.common.exceptions.StatusWithResult; -import ch.systemsx.cisd.common.exceptions.UnknownLastChangedException; import ch.systemsx.cisd.common.filesystem.FileUtilities; +import ch.systemsx.cisd.common.filesystem.LastModificationChecker; import ch.systemsx.cisd.common.filesystem.StoreItem; import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher; import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark; @@ -62,6 +62,8 @@ public class FileStoreLocal extends AbstractFileStore implements IExtendedFileSt private final boolean skipAccessibilityTest; + private final LastModificationChecker lastModificationChecker; + public FileStoreLocal(final HostAwareFileWithHighwaterMark hostAwareFileWithHighwaterMark, final String description, final IFileSysOperationsFactory factory, final boolean skipAccessibilityTest) @@ -71,6 +73,7 @@ public class FileStoreLocal extends AbstractFileStore implements IExtendedFileSt this.remover = factory.getRemover(); this.mover = factory.getMover(); this.highwaterMarkWatcher = createHighwaterMarkWatcher(hostAwareFileWithHighwaterMark); + this.lastModificationChecker = new LastModificationChecker(getPath()); } private final static HighwaterMarkWatcher createHighwaterMarkWatcher( @@ -96,39 +99,13 @@ public class FileStoreLocal extends AbstractFileStore implements IExtendedFileSt public final StatusWithResult<Long> lastChanged(final StoreItem item, final long stopWhenFindYounger) { - try - { - long lastChanged = - FileUtilities.lastChanged(getChildFile(item), true, stopWhenFindYounger); - return StatusWithResult.<Long> create(lastChanged); - } catch (UnknownLastChangedException ex) - { - return createLastChangedError(item, ex); - } + return lastModificationChecker.lastChanged(item, stopWhenFindYounger); } public final StatusWithResult<Long> lastChangedRelative(final StoreItem item, final long stopWhenFindYoungerRelative) { - try - { - long lastChanged = - FileUtilities.lastChangedRelative(getChildFile(item), true, - stopWhenFindYoungerRelative); - return StatusWithResult.<Long> create(lastChanged); - } catch (UnknownLastChangedException ex) - { - return createLastChangedError(item, ex); - } - } - - private static StatusWithResult<Long> createLastChangedError(final StoreItem item, - UnknownLastChangedException ex) - { - String errorMsg = - String.format("Could not determine \"last changed time\" of %s: %s", item, ex - .getCause()); - return StatusWithResult.<Long> createError(errorMsg); + return lastModificationChecker.lastChangedRelative(item, stopWhenFindYoungerRelative); } public final BooleanStatus checkDirectoryFullyAccessible(final long timeOutMillis)