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