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.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * Base class for {@link org.apache.camel.component.file.FileProcessStrategy} implementation to extend.
034 *
035 * @version $Revision: 47012 $
036 */
037 public abstract class FileProcessStrategySupport implements FileProcessStrategy {
038 private static final transient Log LOG = LogFactory.getLog(FileProcessStrategySupport.class);
039 private boolean lockFile;
040 private FileRenamer lockFileRenamer;
041
042 protected FileProcessStrategySupport() {
043 this(true);
044 }
045
046 protected FileProcessStrategySupport(boolean lockFile) {
047 this(lockFile, new DefaultFileRenamer(null, FileEndpoint.DEFAULT_LOCK_FILE_POSTFIX));
048 }
049
050 protected FileProcessStrategySupport(boolean lockFile, FileRenamer lockFileRenamer) {
051 this.lockFile = lockFile;
052 this.lockFileRenamer = lockFileRenamer;
053 }
054
055 public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
056 if (isLockFile()) {
057 File newFile = lockFileRenamer.renameFile(exchange, file);
058 String lockFileName = newFile.getAbsolutePath();
059 if (LOG.isDebugEnabled()) {
060 LOG.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
061 }
062
063 FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
064 FileLock lock = channel.lock();
065 if (lock != null) {
066 exchange.setProperty("org.apache.camel.fileChannel", channel);
067 exchange.setProperty("org.apache.camel.file.lock", lock);
068 exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
069 return true;
070 }
071 return false;
072 }
073 return true;
074 }
075
076 public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
077 unlockFile(endpoint, exchange, file);
078 }
079
080 public void rollback(FileEndpoint endpoint, FileExchange exchange, File file) {
081 try {
082 unlockFile(endpoint, exchange, file);
083 } catch (Exception e) {
084 LOG.info("Unable to unlock file: " + file + ": " + e.getMessage(), e);
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
104 protected void unlockFile(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
105 if (isLockFile()) {
106 Channel channel = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.fileChannel", Channel.class);
107 String lockfile = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name", String.class);
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("Unlocking file: " + file);
110 }
111 channel.close();
112 File lock = new File(lockfile);
113 lock.delete();
114 }
115 }
116 }