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.GenericFileOperationFailedException;
023    import org.apache.camel.component.file.GenericFileOperations;
024    
025    public class GenericFileDeleteProcessStrategy<T> extends GenericFileProcessStrategySupport<T> {
026    
027        private GenericFileRenamer<T> failureRenamer;
028        private GenericFileRenamer<T> beginRenamer;
029    
030        @Override
031        public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
032    
033            // We need to invoke super, but to the file that we are going to use for processing, so we do super after renaming.
034            GenericFile<T> to = file;
035    
036            if (beginRenamer != null) {
037                GenericFile<T> newName = beginRenamer.renameFile(exchange, file);
038                to = renameFile(operations, file, newName);
039                if (to != null) {
040                    to.bindToExchange(exchange);
041                }
042            }
043            // must invoke super
044            boolean result = super.begin(operations, endpoint, exchange, to);
045            if (!result) {
046                return false;
047            }
048    
049            return true;
050        }
051    
052        @Override
053        public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
054            // must invoke super
055            super.commit(operations, endpoint, exchange, file);
056    
057            int retries = 3;
058            boolean deleted = false;
059    
060            while (retries > 0 && !deleted) {
061                retries--;
062    
063                if (operations.deleteFile(file.getAbsoluteFilePath())) {
064                    // file is deleted
065                    deleted = true;
066                    break;
067                }
068    
069                // some OS can report false when deleting but the file is still deleted
070                // use exists to check instead
071                boolean exits = operations.existsFile(file.getAbsoluteFilePath());
072                if (!exits) {
073                    deleted = true;
074                } else {
075                    log.trace("File was not deleted at this attempt will try again in 1 sec.: {}", file);
076                    // sleep a bit and try again
077                    Thread.sleep(1000);
078                }
079            }
080    
081            if (!deleted) {
082                throw new GenericFileOperationFailedException("Cannot delete file: " + file);
083            }
084        }
085    
086        @Override
087        public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
088            // must invoke super
089            super.rollback(operations, endpoint, exchange, file);
090    
091            // moved the failed file if specifying the moveFailed option
092            if (failureRenamer != null) {
093                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
094                Exchange copy = exchange.copy();
095                file.bindToExchange(copy);
096                // must preserve message id
097                copy.getIn().setMessageId(exchange.getIn().getMessageId());
098                copy.setExchangeId(exchange.getExchangeId());
099    
100                GenericFile<T> newName = failureRenamer.renameFile(copy, file);
101                renameFile(operations, file, newName);
102            }
103        }
104    
105        public GenericFileRenamer<T> getFailureRenamer() {
106            return failureRenamer;
107        }
108    
109        public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) {
110            this.failureRenamer = failureRenamer;
111        }
112    
113        public GenericFileRenamer<T> getBeginRenamer() {
114            return beginRenamer;
115        }
116    
117        public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) {
118            this.beginRenamer = beginRenamer;
119        }
120    }