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 operations.releaseRetrievedFileResources(exchange); 097 098 if (commitRenamer != null) { 099 // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name 100 Exchange copy = ExchangeHelper.createCopy(exchange, true); 101 FileEndpoint fe = null; 102 if (endpoint instanceof FileEndpoint) { 103 fe = (FileEndpoint)endpoint; 104 file.bindToExchange(copy, fe.isProbeContentType()); 105 } else { 106 file.bindToExchange(copy); 107 } 108 // must preserve message id 109 copy.getIn().setMessageId(exchange.getIn().getMessageId()); 110 copy.setExchangeId(exchange.getExchangeId()); 111 112 GenericFile<T> newName = commitRenamer.renameFile(copy, file); 113 renameFile(operations, file, newName); 114 } 115 } finally { 116 deleteLocalWorkFile(exchange); 117 // must release lock last 118 if (exclusiveReadLockStrategy != null) { 119 exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange); 120 } 121 } 122 } 123 124 public GenericFileRenamer<T> getBeginRenamer() { 125 return beginRenamer; 126 } 127 128 public void setBeginRenamer(GenericFileRenamer<T> beginRenamer) { 129 this.beginRenamer = beginRenamer; 130 } 131 132 public GenericFileRenamer<T> getCommitRenamer() { 133 return commitRenamer; 134 } 135 136 public void setCommitRenamer(GenericFileRenamer<T> commitRenamer) { 137 this.commitRenamer = commitRenamer; 138 } 139 140 public GenericFileRenamer<T> getFailureRenamer() { 141 return failureRenamer; 142 } 143 144 public void setFailureRenamer(GenericFileRenamer<T> failureRenamer) { 145 this.failureRenamer = failureRenamer; 146 } 147}