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.strategy;
018    
019    import java.io.File;
020    import java.io.RandomAccessFile;
021    import java.nio.channels.Channel;
022    import java.nio.channels.FileChannel;
023    import java.nio.channels.FileLock;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.component.file.FileComponent;
027    import org.apache.camel.component.file.GenericFile;
028    import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
029    import org.apache.camel.component.file.GenericFileOperations;
030    import org.apache.camel.util.ExchangeHelper;
031    import org.apache.camel.util.ObjectHelper;
032    import org.apache.commons.logging.Log;
033    import org.apache.commons.logging.LogFactory;
034    
035    /**
036     * Acquires read lock to the given file using a marker file so other Camel consumers wont acquire the same file.
037     * This is the default behaviour in Camel 1.x.
038     */
039    public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusiveReadLockStrategy<File> {
040        private static final transient Log LOG = LogFactory.getLog(MarkerFileExclusiveReadLockStrategy.class);
041    
042        @SuppressWarnings("unchecked")
043        public boolean acquireExclusiveReadLock(GenericFileOperations<File> fileGenericFileOperations,
044                                                GenericFile<File> file, Exchange exchange) throws Exception {
045    
046            String lockFileName = file.getAbsoluteFilePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX;
047            if (LOG.isTraceEnabled()) {
048                LOG.trace("Locking the file: " + file + " using the lock file name: " + lockFileName);
049            }
050    
051            FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
052            FileLock lock = channel.lock();
053            if (lock != null) {
054                exchange.setProperty("CamelFileLock", lock);
055                exchange.setProperty("CamelFileLockName", lockFileName);
056                return true;
057            } else {
058                return false;
059            }
060        }
061    
062        public void releaseExclusiveReadLock(GenericFileOperations<File> fileGenericFileOperations,
063                                             GenericFile<File> fileGenericFile, Exchange exchange) throws Exception {
064            FileLock lock = ExchangeHelper.getMandatoryProperty(exchange, "CamelFileLock", FileLock.class);
065            String lockFileName = ExchangeHelper.getMandatoryProperty(exchange, "CamelFileLockName", String.class);
066            Channel channel = lock.channel();
067    
068            if (LOG.isTraceEnabled()) {
069                LOG.trace("Unlocking file: " + lockFileName);
070            }
071            try {
072                lock.release();
073            } finally {
074                // must close channel
075                ObjectHelper.close(channel, "Closing channel", LOG);
076                
077                File lockfile = new File(lockFileName);
078                boolean deleted = lockfile.delete();
079                if (LOG.isTraceEnabled()) {
080                    LOG.trace("Lock file: " + lockFileName + " was deleted: " + deleted);
081                }
082            }
083        }
084    
085        public void setTimeout(long timeout) {
086            // noop
087        }
088    
089    }