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