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