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.component.file.GenericFile; 022 import org.apache.camel.component.file.GenericFileEndpoint; 023 import org.apache.camel.component.file.GenericFileExchange; 024 import org.apache.camel.component.file.GenericFileOperationFailedException; 025 import org.apache.camel.component.file.GenericFileOperations; 026 027 public class FileRenameProcessStrategy<T> extends GenericFileProcessStrategySupport<T> { 028 private GenericFileRenamer<T> beginRenamer; 029 private GenericFileRenamer<T> commitRenamer; 030 031 public FileRenameProcessStrategy() { 032 } 033 034 @Override 035 public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, GenericFileExchange<T> 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 exchange.setGenericFile(to); 046 } 047 048 return true; 049 } 050 051 @Override 052 public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, GenericFileExchange<T> exchange, GenericFile<T> file) throws Exception { 053 // must invoke super 054 super.commit(operations, endpoint, exchange, file); 055 056 if (commitRenamer != null) { 057 GenericFile<T> newName = commitRenamer.renameFile(exchange, file); 058 renameFile(operations, file, newName); 059 } 060 } 061 062 private GenericFile<T> renameFile(GenericFileOperations<T> operations, GenericFile<T> from, GenericFile<T> to) throws IOException { 063 // deleting any existing files before renaming 064 try { 065 operations.deleteFile(to.getAbsoluteFilePath()); 066 } catch (GenericFileOperationFailedException e) { 067 // ignore the file does not exists 068 } 069 070 // make parent folder if missing 071 boolean mkdir = operations.buildDirectory(to.getParent(), to.isAbsolute()); 072 073 if (!mkdir) { 074 throw new GenericFileOperationFailedException("Cannot create directory: " + to.getParent() + " (could be because of denied permissions)"); 075 } 076 077 if (log.isDebugEnabled()) { 078 log.debug("Renaming file: " + from + " to: " + to); 079 } 080 boolean renamed = operations.renameFile(from.getAbsoluteFilePath(), to.getAbsoluteFilePath()); 081 if (!renamed) { 082 throw new GenericFileOperationFailedException("Cannot rename file: " + from + " to: " + to); 083 } 084 085 return to; 086 } 087 088 public GenericFileRenamer<T> getBeginRenamer() { 089 return beginRenamer; 090 } 091 092 public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) { 093 this.beginRenamer = beginRenamer; 094 } 095 096 public GenericFileRenamer<T> getCommitRenamer() { 097 return commitRenamer; 098 } 099 100 public void setCommitRenamer(GenericFileRenamer<T> commitRenamer) { 101 this.commitRenamer = commitRenamer; 102 } 103 104 }