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