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.GenericFileExclusiveReadLockStrategy;
025 import org.apache.camel.component.file.GenericFileOperations;
026 import org.apache.camel.component.file.GenericFileProcessStrategy;
027 import org.apache.camel.util.FileUtil;
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 void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception {
036 if (exclusiveReadLockStrategy != null) {
037 exclusiveReadLockStrategy.prepareOnStartup(operations, endpoint);
038 }
039 }
040
041 public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
042 // if we use exclusive read then acquire the exclusive read (waiting until we got it)
043 if (exclusiveReadLockStrategy != null) {
044 boolean lock = exclusiveReadLockStrategy.acquireExclusiveReadLock(operations, file, exchange);
045 if (!lock) {
046 // do not begin since we could not get the exclusive read lock
047 return false;
048 }
049 }
050
051 return true;
052 }
053
054 public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
055 if (exclusiveReadLockStrategy != null) {
056 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange);
057 }
058
059 deleteLocalWorkFile(exchange);
060 }
061
062 public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
063 if (exclusiveReadLockStrategy != null) {
064 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange);
065 }
066
067 deleteLocalWorkFile(exchange);
068 }
069
070 public GenericFileExclusiveReadLockStrategy<T> getExclusiveReadLockStrategy() {
071 return exclusiveReadLockStrategy;
072 }
073
074 public void setExclusiveReadLockStrategy(GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy) {
075 this.exclusiveReadLockStrategy = exclusiveReadLockStrategy;
076 }
077
078 private void deleteLocalWorkFile(Exchange exchange) {
079 // delete local work file, if it was used (eg by ftp component)
080 File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class);
081 if (local != null && local.exists()) {
082 boolean deleted = FileUtil.deleteFile(local);
083 if (log.isTraceEnabled()) {
084 log.trace("Local work file: " + local + " was deleted: " + deleted);
085 }
086 }
087 }
088 }
089