All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.component.file.strategy.GenericFileRenameProcessStrategy Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.file.strategy;

import org.apache.camel.Exchange;
import org.apache.camel.component.file.FileEndpoint;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileOperations;
import org.apache.camel.util.ExchangeHelper;

public class GenericFileRenameProcessStrategy extends GenericFileProcessStrategySupport {
    private GenericFileRenamer beginRenamer;
    private GenericFileRenamer failureRenamer;
    private GenericFileRenamer commitRenamer;

    public GenericFileRenameProcessStrategy() {
    }

    @Override
    public boolean begin(GenericFileOperations operations, GenericFileEndpoint endpoint, Exchange exchange, GenericFile file) throws Exception {
        // must invoke super
        boolean result = super.begin(operations, endpoint, exchange, file);
        if (!result) {
            return false;
        }

        // okay we got the file then execute the begin renamer
        if (beginRenamer != null) {
            GenericFile newName = beginRenamer.renameFile(exchange, file);
            GenericFile to = renameFile(operations, file, newName);
            FileEndpoint fe = null;
            if (endpoint instanceof FileEndpoint) {
                fe = (FileEndpoint)endpoint;
                if (to != null) {
                    to.bindToExchange(exchange, fe.isProbeContentType());
                }
            } else {
                if (to != null) {
                    to.bindToExchange(exchange);
                }
            }
            
        }

        return true;
    }

    @Override
    public void rollback(GenericFileOperations operations, GenericFileEndpoint endpoint, Exchange exchange, GenericFile file) throws Exception {
        try {
            operations.releaseRetrievedFileResources(exchange);

            if (failureRenamer != null) {
                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
                Exchange copy = ExchangeHelper.createCopy(exchange, true);
                FileEndpoint fe = null;
                if (endpoint instanceof FileEndpoint) {
                    fe = (FileEndpoint)endpoint;
                    file.bindToExchange(copy, fe.isProbeContentType());
                } else {
                    file.bindToExchange(copy);
                }
                // must preserve message id
                copy.getIn().setMessageId(exchange.getIn().getMessageId());
                copy.setExchangeId(exchange.getExchangeId());

                GenericFile newName = failureRenamer.renameFile(copy, file);
                renameFile(operations, file, newName);
            }
        } finally {
            if (exclusiveReadLockStrategy != null) {
                exclusiveReadLockStrategy.releaseExclusiveReadLockOnRollback(operations, file, exchange);
            }
            deleteLocalWorkFile(exchange);
        }
    }

    @Override
    public void commit(GenericFileOperations operations, GenericFileEndpoint endpoint, Exchange exchange, GenericFile file) throws Exception {
        try {
            if (commitRenamer != null) {
                // create a copy and bind the file to the exchange to be used by the renamer to evaluate the file name
                Exchange copy = ExchangeHelper.createCopy(exchange, true);
                FileEndpoint fe = null;
                if (endpoint instanceof FileEndpoint) {
                    fe = (FileEndpoint)endpoint;
                    file.bindToExchange(copy, fe.isProbeContentType());
                }  else {
                    file.bindToExchange(copy);
                }
                // must preserve message id
                copy.getIn().setMessageId(exchange.getIn().getMessageId());
                copy.setExchangeId(exchange.getExchangeId());

                GenericFile newName = commitRenamer.renameFile(copy, file);
                renameFile(operations, file, newName);
            }
        } finally {
            // must invoke super
            super.commit(operations, endpoint, exchange, file);
        }
    }

    public GenericFileRenamer getBeginRenamer() {
        return beginRenamer;
    }

    public void setBeginRenamer(GenericFileRenamer beginRenamer) {
        this.beginRenamer = beginRenamer;
    }

    public GenericFileRenamer getCommitRenamer() {
        return commitRenamer;
    }

    public void setCommitRenamer(GenericFileRenamer commitRenamer) {
        this.commitRenamer = commitRenamer;
    }

    public GenericFileRenamer getFailureRenamer() {
        return failureRenamer;
    }

    public void setFailureRenamer(GenericFileRenamer failureRenamer) {
        this.failureRenamer = failureRenamer;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy