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.FileEndpoint; 021import org.apache.camel.component.file.GenericFile; 022import org.apache.camel.component.file.GenericFileEndpoint; 023import org.apache.camel.component.file.GenericFileOperations; 024import org.apache.camel.util.ExchangeHelper; 025 026public class GenericFileRenameProcessStrategy<T> extends GenericFileProcessStrategySupport<T> { 027 private GenericFileRenamer<T> beginRenamer; 028 private GenericFileRenamer<T> failureRenamer; 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 // okay we got the file then execute the begin renamer 043 if (beginRenamer != null) { 044 GenericFile<T> newName = beginRenamer.renameFile(exchange, file); 045 GenericFile<T> to = renameFile(operations, file, newName); 046 FileEndpoint fe = null; 047 if (endpoint instanceof FileEndpoint) { 048 fe = (FileEndpoint)endpoint; 049 if (to != null) { 050 to.bindToExchange(exchange, fe.isProbeContentType()); 051 } 052 } else { 053 if (to != null) { 054 to.bindToExchange(exchange); 055 } 056 } 057 058 } 059 060 return true; 061 } 062 063 @Override 064 public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception { 065 try { 066 operations.releaseRetrievedFileResources(exchange); 067 068 if (failureRenamer != null) { 069 // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name 070 Exchange copy = ExchangeHelper.createCopy(exchange, true); 071 FileEndpoint fe = null; 072 if (endpoint instanceof FileEndpoint) { 073 fe = (FileEndpoint)endpoint; 074 file.bindToExchange(copy, fe.isProbeContentType()); 075 } else { 076 file.bindToExchange(copy); 077 } 078 // must preserve message id 079 copy.getIn().setMessageId(exchange.getIn().getMessageId()); 080 copy.setExchangeId(exchange.getExchangeId()); 081 082 GenericFile<T> newName = failureRenamer.renameFile(copy, file); 083 renameFile(operations, file, newName); 084 } 085 } finally { 086 if (exclusiveReadLockStrategy != null) { 087 exclusiveReadLockStrategy.releaseExclusiveReadLockOnRollback(operations, file, exchange); 088 } 089 deleteLocalWorkFile(exchange); 090 } 091 } 092 093 @Override 094 public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception { 095 try { 096 if (commitRenamer != null) { 097 // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name 098 Exchange copy = ExchangeHelper.createCopy(exchange, true); 099 FileEndpoint fe = null; 100 if (endpoint instanceof FileEndpoint) { 101 fe = (FileEndpoint)endpoint; 102 file.bindToExchange(copy, fe.isProbeContentType()); 103 } else { 104 file.bindToExchange(copy); 105 } 106 // must preserve message id 107 copy.getIn().setMessageId(exchange.getIn().getMessageId()); 108 copy.setExchangeId(exchange.getExchangeId()); 109 110 GenericFile<T> newName = commitRenamer.renameFile(copy, file); 111 renameFile(operations, file, newName); 112 } 113 } finally { 114 // must invoke super 115 super.commit(operations, endpoint, exchange, file); 116 } 117 } 118 119 public GenericFileRenamer<T> getBeginRenamer() { 120 return beginRenamer; 121 } 122 123 public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) { 124 this.beginRenamer = beginRenamer; 125 } 126 127 public GenericFileRenamer<T> getCommitRenamer() { 128 return commitRenamer; 129 } 130 131 public void setCommitRenamer(GenericFileRenamer<T> commitRenamer) { 132 this.commitRenamer = commitRenamer; 133 } 134 135 public GenericFileRenamer<T> getFailureRenamer() { 136 return failureRenamer; 137 } 138 139 public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) { 140 this.failureRenamer = failureRenamer; 141 } 142}