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            // must invoke super
035            boolean result = super.begin(operations, endpoint, exchange, file);
036            if (!result) {
037                return false;
038            }
039    
040            if (beginRenamer != null) {
041                GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
042                GenericFile<T> to = renameFile(operations, file, newName);
043                if (to != null) {
044                    to.bindToExchange(exchange);
045                }
046            }
047    
048            return true;
049        }
050    
051        @Override
052        public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
053            // must invoke super
054            super.rollback(operations, endpoint, exchange, file);
055    
056            if (failureRenamer != null) {
057                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
058                Exchange copy = exchange.copy();
059                file.bindToExchange(copy);
060                // must preserve message id
061                copy.getIn().setMessageId(exchange.getIn().getMessageId());
062                copy.setExchangeId(exchange.getExchangeId());
063    
064                GenericFile<T> newName = failureRenamer.renameFile(copy, file);
065                renameFile(operations, file, newName);
066            }
067        }
068    
069        @Override
070        public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
071            // must invoke super
072            super.commit(operations, endpoint, exchange, file);
073    
074            if (commitRenamer != null) {
075                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
076                Exchange copy = exchange.copy();
077                file.bindToExchange(copy);
078                // must preserve message id
079                copy.getIn().setMessageId(exchange.getIn().getMessageId());
080                copy.setExchangeId(exchange.getExchangeId());
081    
082                GenericFile<T> newName = commitRenamer.renameFile(copy, file);
083                renameFile(operations, file, newName);
084            }
085        }
086    
087        public GenericFileRenamer<T> getBeginRenamer() {
088            return beginRenamer;
089        }
090    
091        public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) {
092            this.beginRenamer = beginRenamer;
093        }
094    
095        public GenericFileRenamer<T> getCommitRenamer() {
096            return commitRenamer;
097        }
098    
099        public void setCommitRenamer(GenericFileRenamer<T> commitRenamer) {
100            this.commitRenamer = commitRenamer;
101        }
102    
103        public GenericFileRenamer<T> getFailureRenamer() {
104            return failureRenamer;
105        }
106    
107        public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) {
108            this.failureRenamer = failureRenamer;
109        }
110    }