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.IOException;
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.GenericFileOperationFailedException;
025    import org.apache.camel.component.file.GenericFileOperations;
026    
027    public class GenericFileRenameProcessStrategy<T> extends GenericFileProcessStrategySupport<T> {
028        private GenericFileRenamer<T> beginRenamer;
029        private GenericFileRenamer<T> failureRenamer;
030        private GenericFileRenamer<T> commitRenamer;
031    
032        public GenericFileRenameProcessStrategy() {
033        }
034    
035        @Override
036        public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
037            // must invoke super
038            boolean result = super.begin(operations, endpoint, exchange, file);
039            if (!result) {
040                return false;
041            }
042    
043            if (beginRenamer != null) {
044                GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
045                GenericFile<T> to = renameFile(operations, file, newName);
046                if (to != null) {
047                    to.bindToExchange(exchange);
048                }
049            }
050    
051            return true;
052        }
053    
054        @Override
055        public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
056            // must invoke super
057            super.rollback(operations, endpoint, exchange, file);
058    
059            if (failureRenamer != null) {
060                GenericFile<T> newName = failureRenamer.renameFile(exchange, file);
061                renameFile(operations, file, newName);
062            }
063        }
064    
065        @Override
066        public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
067            // must invoke super
068            super.commit(operations, endpoint, exchange, file);
069    
070            if (commitRenamer != null) {
071                GenericFile<T> newName = commitRenamer.renameFile(exchange, file);
072                renameFile(operations, file, newName);
073            }
074        }
075    
076        private GenericFile<T> renameFile(GenericFileOperations<T> operations, GenericFile<T> from, GenericFile<T> to) throws IOException {
077            // deleting any existing files before renaming
078            try {
079                operations.deleteFile(to.getAbsoluteFilePath());
080            } catch (GenericFileOperationFailedException e) {
081                // ignore the file does not exists
082            }
083            
084            // make parent folder if missing
085            boolean mkdir = operations.buildDirectory(to.getParent(), to.isAbsolute());
086            
087            if (!mkdir) {
088                throw new GenericFileOperationFailedException("Cannot create directory: " + to.getParent() + " (could be because of denied permissions)");
089            }
090    
091            if (log.isDebugEnabled()) {
092                log.debug("Renaming file: " + from + " to: " + to);
093            }
094            boolean renamed = operations.renameFile(from.getAbsoluteFilePath(), to.getAbsoluteFilePath());
095            if (!renamed) {
096                throw new GenericFileOperationFailedException("Cannot rename file: " + from + " to: " + to);
097            }
098    
099            return to;
100        }
101    
102        public GenericFileRenamer<T> getBeginRenamer() {
103            return beginRenamer;
104        }
105    
106        public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) {
107            this.beginRenamer = beginRenamer;
108        }
109    
110        public GenericFileRenamer<T> getCommitRenamer() {
111            return commitRenamer;
112        }
113    
114        public void setCommitRenamer(GenericFileRenamer<T> commitRenamer) {
115            this.commitRenamer = commitRenamer;
116        }
117    
118        public GenericFileRenamer<T> getFailureRenamer() {
119            return failureRenamer;
120        }
121    
122        public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) {
123            this.failureRenamer = failureRenamer;
124        }
125    }