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.GenericFile; 023 import org.apache.camel.component.file.GenericFileEndpoint; 024 import org.apache.camel.component.file.GenericFileExchange; 025 import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy; 026 import org.apache.camel.component.file.GenericFileOperations; 027 import org.apache.camel.component.file.GenericFileProcessStrategy; 028 import org.apache.commons.logging.Log; 029 import org.apache.commons.logging.LogFactory; 030 031 public abstract class GenericFileProcessStrategySupport<T> implements GenericFileProcessStrategy<T> { 032 protected final transient Log log = LogFactory.getLog(getClass()); 033 protected GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy; 034 035 public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, GenericFileExchange<T> exchange, GenericFile<T> file) throws Exception { 036 // is we use excluse read then acquire the exclusive read (waiting until we got it) 037 if (exclusiveReadLockStrategy != null) { 038 boolean lock = exclusiveReadLockStrategy.acquireExclusiveReadLock(operations, file, exchange); 039 if (!lock) { 040 // do not begin sice we could not get the exclusive read lcok 041 return false; 042 } 043 } 044 045 return true; 046 } 047 048 public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, GenericFileExchange<T> exchange, GenericFile<T> file) throws Exception { 049 if (exclusiveReadLockStrategy != null) { 050 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange); 051 } 052 053 deleteLocalWorkFile(exchange); 054 } 055 056 public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, GenericFileExchange<T> exchange, GenericFile<T> file) throws Exception { 057 if (exclusiveReadLockStrategy != null) { 058 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange); 059 } 060 061 deleteLocalWorkFile(exchange); 062 } 063 064 public GenericFileExclusiveReadLockStrategy<T> getExclusiveReadLockStrategy() { 065 return exclusiveReadLockStrategy; 066 } 067 068 public void setExclusiveReadLockStrategy(GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy) { 069 this.exclusiveReadLockStrategy = exclusiveReadLockStrategy; 070 } 071 072 private void deleteLocalWorkFile(GenericFileExchange<T> exchange) { 073 // delete local work file, if it was used (eg by ftp component) 074 File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class); 075 if (local != null && local.exists()) { 076 if (log.isTraceEnabled()) { 077 log.trace("Deleting lock work file: " + local); 078 } 079 local.delete(); 080 } 081 } 082 } 083