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     */
017    package org.apache.camel.component.file;
018    
019    import org.apache.camel.Exchange;
020    
021    /**
022     * Strategy for acquiring exclusive read locks for files to be consumed. After
023     * granting the read lock it is realeased, we just want to make sure that when
024     * we start consuming the file its not currently in progress of being written by
025     * third party.
026     * <p/>
027     * Camel supports out of the box the following strategies:
028     * <ul>
029     *   <li>FileRenameExclusiveReadLockStrategy waiting until its possible to rename the file.</li>
030     *   <li>FileLockExclusiveReadLockStrategy acquiring a RW file lock for the duration of the processing.</li>
031     *   <li>MarkerFileExclusiveReadLockStrategy using a marker file for acquiring read lock.</li>
032     * </ul>
033     */
034    public interface GenericFileExclusiveReadLockStrategy<T> {
035    
036        /**
037         * Acquires exclusive read lock to the file.
038         *
039         * @param operations generic file operations
040         * @param file       the file
041         * @param exchange   the exchange
042         * @return <tt>true</tt> if read lock was acquired. If <tt>false</tt> Camel
043         *         will skip the file and try it on the next poll
044         * @throws Exception can be thrown in case of errors
045         */
046        boolean acquireExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception;
047    
048        /**
049         * Releases the exclusive read lock granted by the <tt>acquireExclusiveReadLock</tt> method.
050         *
051         * @param operations generic file operations
052         * @param file       the file
053         * @param exchange   the exchange
054         * @throws Exception can be thrown in case of errors
055         */
056        void releaseExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception;
057    
058        /**
059         * Sets an optional timeout period.
060         * <p/>
061         * If the readlock could not be granted within the timeperiod then the wait is stopped and the
062         * <tt>acquireExclusiveReadLock</tt> method returns <tt>false</tt>.
063         *
064         * @param timeout period in millis
065         */
066        void setTimeout(long timeout);
067    
068    }