001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.file;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.LoggingLevel;
021
022/**
023 * Strategy for acquiring exclusive read locks for files to be consumed. After
024 * granting the read lock it is released, we just want to make sure that when
025 * we start consuming the file its not currently in progress of being written by
026 * third party.
027 * <p/>
028 * Camel supports out of the box the following strategies:
029 * <ul>
030 * <li>FileRenameExclusiveReadLockStrategy waiting until its possible to rename the file.</li>
031 * <li>FileLockExclusiveReadLockStrategy acquiring a RW file lock for the duration of the processing.</li>
032 * <li>MarkerFileExclusiveReadLockStrategy using a marker file for acquiring read lock.</li>
033 * <li>FileChangedExclusiveReadLockStrategy using a file changed detection for acquiring read lock.</li>
034 * </ul>
035 */
036public interface GenericFileExclusiveReadLockStrategy<T> {
037
038    /**
039     * Allows custom logic to be run on startup preparing the strategy, such as removing old lock files etc.
040     *
041     * @param operations generic file operations
042     * @param endpoint   the endpoint
043     * @throws Exception can be thrown in case of errors
044     */
045    void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception;
046
047    /**
048     * Acquires exclusive read lock to the file.
049     *
050     * @param operations generic file operations
051     * @param file       the file
052     * @param exchange   the exchange
053     * @return <tt>true</tt> if read lock was acquired. If <tt>false</tt> Camel
054     *         will skip the file and try it on the next poll
055     * @throws Exception can be thrown in case of errors
056     */
057    boolean acquireExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception;
058
059    /**
060     * Releases the exclusive read lock granted by the <tt>acquireExclusiveReadLock</tt> method.
061     *
062     * @param operations generic file operations
063     * @param file       the file
064     * @param exchange   the exchange
065     * @throws Exception can be thrown in case of errors
066     */
067    void releaseExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception;
068
069    /**
070     * Sets an optional timeout period.
071     * <p/>
072     * If the readlock could not be granted within the time period then the wait is stopped and the
073     * <tt>acquireExclusiveReadLock</tt> method returns <tt>false</tt>.
074     *
075     * @param timeout period in millis
076     */
077    void setTimeout(long timeout);
078
079    /**
080     * Sets the check interval period.
081     * <p/>
082     * The check interval is used for sleeping between attempts to acquire read lock.
083     * Setting a high value allows to cater for <i>slow writes</i> in case the producer
084     * of the file is slow.
085     * <p/>
086     * The default period is 1000 millis.
087     *
088     * @param checkInterval interval in millis
089     */
090    void setCheckInterval(long checkInterval);
091
092    /**
093     * Sets logging level used when a read lock could not be acquired.
094     * <p/>
095     * Logging level used when a read lock could not be acquired.
096     * <p/>
097     * The default logging level is WARN
098     * @param readLockLoggingLevel LoggingLevel
099     */
100    void setReadLockLoggingLevel(LoggingLevel readLockLoggingLevel);
101
102    /**
103     * Sets whether marker file should be used or not.
104     *
105     * @param markerFile <tt>true</tt> to use marker files.
106     */
107    void setMarkerFiler(boolean markerFile);
108
109}