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 released, 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     * <li>FileChangedExclusiveReadLockStrategy using a file changed detection for acquiring read lock.</li>
033     * </ul>
034     */
035    public interface GenericFileExclusiveReadLockStrategy<T> {
036    
037        /**
038         * Allows custom logic to be run on startup preparing the strategy, such as removing old lock files etc.
039         *
040         * @param operations generic file operations
041         * @param endpoint   the endpoint
042         * @throws Exception can be thrown in case of errors
043         */
044        void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception;
045    
046        /**
047         * Acquires exclusive read lock to the file.
048         *
049         * @param operations generic file operations
050         * @param file       the file
051         * @param exchange   the exchange
052         * @return <tt>true</tt> if read lock was acquired. If <tt>false</tt> Camel
053         *         will skip the file and try it on the next poll
054         * @throws Exception can be thrown in case of errors
055         */
056        boolean acquireExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception;
057    
058        /**
059         * Releases the exclusive read lock granted by the <tt>acquireExclusiveReadLock</tt> method.
060         *
061         * @param operations generic file operations
062         * @param file       the file
063         * @param exchange   the exchange
064         * @throws Exception can be thrown in case of errors
065         */
066        void releaseExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception;
067    
068        /**
069         * Sets an optional timeout period.
070         * <p/>
071         * If the readlock could not be granted within the time period then the wait is stopped and the
072         * <tt>acquireExclusiveReadLock</tt> method returns <tt>false</tt>.
073         *
074         * @param timeout period in millis
075         */
076        void setTimeout(long timeout);
077    
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    }