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 021 import org.apache.camel.Exchange; 022 import org.apache.camel.component.file.FileComponent; 023 import org.apache.camel.component.file.GenericFile; 024 import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy; 025 import org.apache.camel.component.file.GenericFileOperations; 026 import org.apache.camel.util.ExchangeHelper; 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 030 /** 031 * Acquires read lock to the given file using a marker file so other Camel consumers wont acquire the same file. 032 * This is the default behavior in Camel 1.x. 033 */ 034 public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusiveReadLockStrategy<File> { 035 private static final transient Log LOG = LogFactory.getLog(MarkerFileExclusiveReadLockStrategy.class); 036 037 @SuppressWarnings("unchecked") 038 public boolean acquireExclusiveReadLock(GenericFileOperations<File> fileGenericFileOperations, 039 GenericFile<File> file, Exchange exchange) throws Exception { 040 041 String lockFileName = file.getAbsoluteFilePath() + FileComponent.DEFAULT_LOCK_FILE_POSTFIX; 042 if (LOG.isTraceEnabled()) { 043 LOG.trace("Locking the file: " + file + " using the lock file name: " + lockFileName); 044 } 045 046 // create a plain file as marker filer for locking (do not use FileLock) 047 File lock = new File(lockFileName); 048 boolean acquired = lock.createNewFile(); 049 if (acquired) { 050 exchange.setProperty("CamelFileLock", lock); 051 exchange.setProperty("CamelFileLockName", lockFileName); 052 } 053 054 return acquired; 055 } 056 057 public void releaseExclusiveReadLock(GenericFileOperations<File> operations, 058 GenericFile<File> file, Exchange exchange) throws Exception { 059 060 File lock = ExchangeHelper.getMandatoryProperty(exchange, "CamelFileLock", File.class); 061 String lockFileName = ExchangeHelper.getMandatoryProperty(exchange, "CamelFileLockName", String.class); 062 063 if (LOG.isTraceEnabled()) { 064 LOG.trace("Unlocking file: " + lockFileName); 065 } 066 067 boolean deleted = lock.delete(); 068 if (LOG.isTraceEnabled()) { 069 LOG.trace("Lock file: " + lockFileName + " was deleted: " + deleted); 070 } 071 } 072 073 public void setTimeout(long timeout) { 074 // noop 075 } 076 077 }