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