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.component.file.FileProcessStrategy;
028    import org.apache.camel.util.ExchangeHelper;
029    import org.apache.camel.util.ObjectHelper;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * Base class for {@link org.apache.camel.component.file.FileProcessStrategy} implementation to extend.
035     *
036     * @version $Revision: 1273 $
037     */
038    public abstract class FileProcessStrategySupport implements FileProcessStrategy {
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, FileEndpoint.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(exchange, 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.file.lock", lock);
068                    exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
069                    return true;
070                } else {
071                    return false;
072                }
073            }
074            return true;
075        }
076    
077        public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
078            unlockFile(endpoint, exchange, file);
079        }
080    
081        public void rollback(FileEndpoint endpoint, FileExchange exchange, File file) {
082            try {
083                unlockFile(endpoint, exchange, file);
084            } catch (Exception e) {
085                LOG.warn("Unable to unlock file: " + file, e);
086            }
087        }
088    
089        public boolean isLockFile() {
090            return lockFile;
091        }
092    
093        public void setLockFile(boolean lockFile) {
094            this.lockFile = lockFile;
095        }
096    
097        public FileRenamer getLockFileRenamer() {
098            return lockFileRenamer;
099        }
100    
101        public void setLockFileRenamer(FileRenamer lockFileRenamer) {
102            this.lockFileRenamer = lockFileRenamer;
103        }
104    
105        protected void unlockFile(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
106            if (isLockFile()) {
107                FileLock lock = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock", FileLock.class);
108                String lockFileName = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name", String.class);
109                Channel channel = lock.channel();
110                if (LOG.isDebugEnabled()) {
111                    LOG.debug("Unlocking file: " + file);
112                }
113                try {
114                    lock.release();
115                } finally {
116                    // must close channel
117                    ObjectHelper.close(channel, "Closing channel", LOG);
118    
119                    if (LOG.isTraceEnabled()) {
120                        LOG.trace("Deleting lock file: " + lockFileName);
121                    }
122                    File lockfile = new File(lockFileName);
123                    lockfile.delete();
124                }
125            }
126        }
127    }