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 */
017package org.apache.camel.component.file.strategy;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.component.file.GenericFile;
021import org.apache.camel.component.file.GenericFileEndpoint;
022import org.apache.camel.component.file.GenericFileOperationFailedException;
023import org.apache.camel.component.file.GenericFileOperations;
024import org.apache.camel.util.ExchangeHelper;
025
026public class GenericFileDeleteProcessStrategy<T> extends GenericFileProcessStrategySupport<T> {
027
028    private GenericFileRenamer<T> failureRenamer;
029    private GenericFileRenamer<T> beginRenamer;
030
031    @Override
032    public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
033
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 commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
054
055        // special for file lock strategy as we must release that lock first before we can delete the file
056        boolean releaseEager = exclusiveReadLockStrategy instanceof FileLockExclusiveReadLockStrategy;
057
058        if (releaseEager) {
059            exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange);
060        }
061
062        try {
063            deleteLocalWorkFile(exchange);
064            operations.releaseRetrievedFileResources(exchange);
065
066            int retries = 3;
067            boolean deleted = false;
068
069            while (retries > 0 && !deleted) {
070                retries--;
071
072                if (operations.deleteFile(file.getAbsoluteFilePath())) {
073                    // file is deleted
074                    deleted = true;
075                    break;
076                }
077
078                // some OS can report false when deleting but the file is still deleted
079                // use exists to check instead
080                boolean exits = operations.existsFile(file.getAbsoluteFilePath());
081                if (!exits) {
082                    deleted = true;
083                } else {
084                    log.trace("File was not deleted at this attempt will try again in 1 sec.: {}", file);
085                    // sleep a bit and try again
086                    Thread.sleep(1000);
087                }
088            }
089            if (!deleted) {
090                throw new GenericFileOperationFailedException("Cannot delete file: " + file);
091            }
092        } finally {
093            // must release lock last
094            if (!releaseEager && exclusiveReadLockStrategy != null) {
095                exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange);
096            }
097        }
098    }
099
100    @Override
101    public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
102        try {
103            deleteLocalWorkFile(exchange);
104            operations.releaseRetrievedFileResources(exchange);
105
106            // moved the failed file if specifying the moveFailed option
107            if (failureRenamer != null) {
108                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
109                Exchange copy = ExchangeHelper.createCopy(exchange, true);
110                file.bindToExchange(copy);
111                // must preserve message id
112                copy.getIn().setMessageId(exchange.getIn().getMessageId());
113                copy.setExchangeId(exchange.getExchangeId());
114
115                GenericFile<T> newName = failureRenamer.renameFile(copy, file);
116                renameFile(operations, file, newName);
117            }
118        } finally {
119            // must release lock last
120            if (exclusiveReadLockStrategy != null) {
121                exclusiveReadLockStrategy.releaseExclusiveReadLockOnRollback(operations, file, exchange);
122            }
123        }
124    }
125
126    public GenericFileRenamer<T> getFailureRenamer() {
127        return failureRenamer;
128    }
129
130    public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) {
131        this.failureRenamer = failureRenamer;
132    }
133
134    public GenericFileRenamer<T> getBeginRenamer() {
135        return beginRenamer;
136    }
137
138    public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) {
139        this.beginRenamer = beginRenamer;
140    }
141}