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 org.apache.camel.Exchange;
020    import org.apache.camel.component.file.GenericFile;
021    import org.apache.camel.component.file.GenericFileEndpoint;
022    import org.apache.camel.component.file.GenericFileOperations;
023    
024    public class GenericFileRenameProcessStrategy<T> extends GenericFileProcessStrategySupport<T> {
025        private GenericFileRenamer<T> beginRenamer;
026        private GenericFileRenamer<T> failureRenamer;
027        private GenericFileRenamer<T> commitRenamer;
028    
029        public GenericFileRenameProcessStrategy() {
030        }
031    
032        @Override
033        public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
034    
035            // We need to invoke super, but to the file that we are going to use for processing, so we do super after renaming.
036            GenericFile<T> to = file;
037    
038            if (beginRenamer != null) {
039                GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
040                to = renameFile(operations, file, newName);
041                if (to != null) {
042                    to.bindToExchange(exchange);
043                }
044            }
045            // must invoke super
046            boolean result = super.begin(operations, endpoint, exchange, to);
047            if (!result) {
048                return false;
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                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
061                Exchange copy = exchange.copy();
062                file.bindToExchange(copy);
063                // must preserve message id
064                copy.getIn().setMessageId(exchange.getIn().getMessageId());
065                copy.setExchangeId(exchange.getExchangeId());
066    
067                GenericFile<T> newName = failureRenamer.renameFile(copy, file);
068                renameFile(operations, file, newName);
069            }
070        }
071    
072        @Override
073        public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
074            // must invoke super
075            super.commit(operations, endpoint, exchange, file);
076    
077            if (commitRenamer != null) {
078                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
079                Exchange copy = exchange.copy();
080                file.bindToExchange(copy);
081                // must preserve message id
082                copy.getIn().setMessageId(exchange.getIn().getMessageId());
083                copy.setExchangeId(exchange.getExchangeId());
084    
085                GenericFile<T> newName = commitRenamer.renameFile(copy, file);
086                renameFile(operations, file, newName);
087            }
088        }
089    
090        public GenericFileRenamer<T> getBeginRenamer() {
091            return beginRenamer;
092        }
093    
094        public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) {
095            this.beginRenamer = beginRenamer;
096        }
097    
098        public GenericFileRenamer<T> getCommitRenamer() {
099            return commitRenamer;
100        }
101    
102        public void setCommitRenamer(GenericFileRenamer<T> commitRenamer) {
103            this.commitRenamer = commitRenamer;
104        }
105    
106        public GenericFileRenamer<T> getFailureRenamer() {
107            return failureRenamer;
108        }
109    
110        public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) {
111            this.failureRenamer = failureRenamer;
112        }
113    }