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> commitRenamer;
030    
031        public GenericFileRenameProcessStrategy() {
032        }
033    
034        @Override
035        public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
036            // must invoke super
037            boolean result = super.begin(operations, endpoint, exchange, file);
038            if (!result) {
039                return false;
040            }
041    
042            if (beginRenamer != null) {
043                GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
044                GenericFile<T> to = renameFile(operations, file, newName);
045                if (to != null) {
046                    to.bindToExchange(exchange);
047                }
048            }
049    
050            return true;
051        }
052    
053        @Override
054        public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
055            // must invoke super
056            super.commit(operations, endpoint, exchange, file);
057    
058            if (commitRenamer != null) {
059                GenericFile<T> newName = commitRenamer.renameFile(exchange, file);
060                renameFile(operations, file, newName);
061            }
062        }
063    
064        private GenericFile<T> renameFile(GenericFileOperations<T> operations, GenericFile<T> from, GenericFile<T> to) throws IOException {
065            // deleting any existing files before renaming
066            try {
067                operations.deleteFile(to.getAbsoluteFilePath());
068            } catch (GenericFileOperationFailedException e) {
069                // ignore the file does not exists
070            }
071            
072            // make parent folder if missing
073            boolean mkdir = operations.buildDirectory(to.getParent(), to.isAbsolute());
074            
075            if (!mkdir) {
076                throw new GenericFileOperationFailedException("Cannot create directory: " + to.getParent() + " (could be because of denied permissions)");
077            }
078    
079            if (log.isDebugEnabled()) {
080                log.debug("Renaming file: " + from + " to: " + to);
081            }
082            boolean renamed = operations.renameFile(from.getAbsoluteFilePath(), to.getAbsoluteFilePath());
083            if (!renamed) {
084                throw new GenericFileOperationFailedException("Cannot rename file: " + from + " to: " + to);
085            }
086    
087            return to;
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    }