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 @Override
028 public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
029 // must invoke super
030 super.commit(operations, endpoint, exchange, file);
031
032 int retries = 3;
033 boolean deleted = false;
034
035 while (retries > 0 && !deleted) {
036 retries--;
037
038 if (operations.deleteFile(file.getAbsoluteFilePath())) {
039 // file is deleted
040 deleted = true;
041 break;
042 }
043
044 // some OS can report false when deleting but the file is still deleted
045 // use exists to check instead
046 boolean exits = operations.existsFile(file.getAbsoluteFilePath());
047 if (!exits) {
048 deleted = true;
049 } else {
050 if (log.isTraceEnabled()) {
051 log.trace("File was not deleted at this attempt will try again in 1 sec.: " + file);
052 }
053 // sleep a bit and try again
054 Thread.sleep(1000);
055 }
056 }
057
058 if (!deleted) {
059 throw new GenericFileOperationFailedException("Cannot delete file: " + file);
060 }
061 }
062
063 }