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.IOException;
021
022 import org.apache.camel.Exchange;
023 import org.apache.camel.component.file.GenericFile;
024 import org.apache.camel.component.file.GenericFileEndpoint;
025 import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
026 import org.apache.camel.component.file.GenericFileOperationFailedException;
027 import org.apache.camel.component.file.GenericFileOperations;
028 import org.apache.camel.component.file.GenericFileProcessStrategy;
029 import org.apache.camel.util.FileUtil;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 /**
034 * Base class for implementations of {@link GenericFileProcessStrategy}.
035 */
036 public abstract class GenericFileProcessStrategySupport<T> implements GenericFileProcessStrategy<T> {
037 protected final transient Logger log = LoggerFactory.getLogger(getClass());
038 protected GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy;
039
040 public void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception {
041 if (exclusiveReadLockStrategy != null) {
042 exclusiveReadLockStrategy.prepareOnStartup(operations, endpoint);
043 }
044 }
045
046 public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
047 // if we use exclusive read then acquire the exclusive read (waiting until we got it)
048 if (exclusiveReadLockStrategy != null) {
049 boolean lock = exclusiveReadLockStrategy.acquireExclusiveReadLock(operations, file, exchange);
050 if (!lock) {
051 // do not begin since we could not get the exclusive read lock
052 return false;
053 }
054 }
055
056 return true;
057 }
058
059 public void abort(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
060 if (exclusiveReadLockStrategy != null) {
061 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange);
062 }
063
064 deleteLocalWorkFile(exchange);
065 }
066
067 public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
068 if (exclusiveReadLockStrategy != null) {
069 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange);
070 }
071
072 deleteLocalWorkFile(exchange);
073 }
074
075 public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
076 if (exclusiveReadLockStrategy != null) {
077 exclusiveReadLockStrategy.releaseExclusiveReadLock(operations, file, exchange);
078 }
079
080 deleteLocalWorkFile(exchange);
081 }
082
083 public GenericFileExclusiveReadLockStrategy<T> getExclusiveReadLockStrategy() {
084 return exclusiveReadLockStrategy;
085 }
086
087 public void setExclusiveReadLockStrategy(GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy) {
088 this.exclusiveReadLockStrategy = exclusiveReadLockStrategy;
089 }
090
091 protected GenericFile<T> renameFile(GenericFileOperations<T> operations, GenericFile<T> from, GenericFile<T> to) throws IOException {
092 // deleting any existing files before renaming
093 try {
094 operations.deleteFile(to.getAbsoluteFilePath());
095 } catch (GenericFileOperationFailedException e) {
096 // ignore the file does not exists
097 }
098
099 // make parent folder if missing
100 boolean mkdir = operations.buildDirectory(to.getParent(), to.isAbsolute());
101
102 if (!mkdir) {
103 throw new GenericFileOperationFailedException("Cannot create directory: " + to.getParent() + " (could be because of denied permissions)");
104 }
105
106 log.debug("Renaming file: {} to: {}", from, to);
107 boolean renamed = operations.renameFile(from.getAbsoluteFilePath(), to.getAbsoluteFilePath());
108 if (!renamed) {
109 throw new GenericFileOperationFailedException("Cannot rename file: " + from + " to: " + to);
110 }
111
112 return to;
113 }
114
115 private void deleteLocalWorkFile(Exchange exchange) {
116 // delete local work file, if it was used (eg by ftp component)
117 File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class);
118 if (local != null && local.exists()) {
119 boolean deleted = FileUtil.deleteFile(local);
120 log.trace("Local work file: {} was deleted: {}", local, deleted);
121 }
122 }
123 }
124