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.component.file.FileEndpoint;
026    import org.apache.camel.component.file.FileExchange;
027    import org.apache.camel.util.ExchangeHelper;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * @version $Revision: 1.1 $
033     */
034    public abstract class FileProcessStrategySupport implements FileProcessStrategy {
035        public static final String DEFAULT_LOCK_FILE_POSTFIX = ".cameLock";
036        
037        private static final transient Log LOG = LogFactory.getLog(FileProcessStrategySupport.class);
038        private boolean lockFile;
039        private FileRenamer lockFileRenamer;
040    
041        protected FileProcessStrategySupport() {
042            this(true);
043        }
044    
045        protected FileProcessStrategySupport(boolean lockFile) {
046            this(lockFile, new DefaultFileRenamer(null, DEFAULT_LOCK_FILE_POSTFIX));
047        }
048    
049        protected FileProcessStrategySupport(boolean lockFile, FileRenamer lockFileRenamer) {
050            this.lockFile = lockFile;
051            this.lockFileRenamer = lockFileRenamer;
052        }
053    
054        public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
055            if (isLockFile()) {
056                File newFile = lockFileRenamer.renameFile(file);
057                String lockFileName = newFile.getAbsolutePath();
058                if (LOG.isDebugEnabled()) {
059                    LOG.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
060                }
061    
062                FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
063                FileLock lock = channel.lock();
064                if (lock != null) {
065                    exchange.setProperty("org.apache.camel.fileChannel", channel);
066                    exchange.setProperty("org.apache.camel.file.lock", lock);
067                    exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
068                    return true;
069                }
070                return false;
071            }
072            return true;
073        }
074    
075        public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
076            if (isLockFile()) {
077                Channel channel = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.fileChannel", Channel.class);
078                String lockfile = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name", String.class);
079                if (LOG.isDebugEnabled()) {
080                    LOG.debug("Unlocking file: " + file);
081                }
082                channel.close();
083                File lock = new File(lockfile);
084                lock.delete();
085            }
086        }
087    
088        public boolean isLockFile() {
089            return lockFile;
090        }
091    
092        public void setLockFile(boolean lockFile) {
093            this.lockFile = lockFile;
094        }
095    
096        public FileRenamer getLockFileRenamer() {
097            return lockFileRenamer;
098        }
099    
100        public void setLockFileRenamer(FileRenamer lockFileRenamer) {
101            this.lockFileRenamer = lockFileRenamer;
102        }
103    }