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 FileStategySupport implements FileStrategy {
035        private static final transient Log LOG = LogFactory.getLog(FileStategySupport.class);
036        private boolean lockFile;
037    
038        protected FileStategySupport() {
039            this(true);
040        }
041    
042        protected FileStategySupport(boolean lockFile) {
043            this.lockFile = lockFile;
044        }
045    
046        public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
047            if (isLockFile()) {
048                String lockFileName = file.getAbsoluteFile() + ".lock";
049                if (LOG.isDebugEnabled()) {
050                    LOG.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
051                }
052    
053                FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
054                FileLock lock = channel.lock();
055                if (lock != null) {
056                    exchange.setProperty("org.apache.camel.fileChannel", channel);
057                    exchange.setProperty("org.apache.camel.file.lock", lock);
058                    exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
059                    return true;
060                }
061                return false;
062            }
063            return true;
064        }
065    
066        public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
067            if (isLockFile()) {
068                Channel channel = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.fileChannel", Channel.class);
069                String lockfile = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name", String.class);
070                if (LOG.isDebugEnabled()) {
071                    LOG.debug("Unlocking file: " + file);
072                }
073                channel.close();
074                File lock = new File(lockfile);
075                lock.delete();
076            }
077        }
078    
079        public boolean isLockFile() {
080            return lockFile;
081        }
082    
083        public void setLockFile(boolean lockFile) {
084            this.lockFile = lockFile;
085        }
086    }