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.impl;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.camel.Exchange;
023import org.apache.camel.spi.SubUnitOfWork;
024import org.apache.camel.spi.SubUnitOfWorkCallback;
025
026/**
027 * A default implementation of {@link org.apache.camel.spi.SubUnitOfWork} combined
028 * with a {@link SubUnitOfWorkCallback} to gather callbacks into this {@link SubUnitOfWork} state
029 */
030public class DefaultSubUnitOfWork implements SubUnitOfWork, SubUnitOfWorkCallback {
031
032    private List<Exception> failedExceptions;
033    private boolean failed;
034
035    @Override
036    public void onExhausted(Exchange exchange) {
037        if (exchange.getException() != null) {
038            addFailedException(exchange.getException());
039            failed = true;
040        }
041    }
042
043    @Override
044    public void onDone(Exchange exchange) {
045        if (exchange.getException() != null) {
046            addFailedException(exchange.getException());
047            failed = true;
048        }
049    }
050
051    @Override
052    public boolean isFailed() {
053        return failed;
054    }
055
056    @Override
057    public List<Exception> getExceptions() {
058        return failedExceptions;
059    }
060
061    private void addFailedException(Exception exception) {
062        if (failedExceptions == null) {
063            failedExceptions = new ArrayList<>();
064        }
065        if (!failedExceptions.contains(exception)) {
066            // avoid adding the same exception multiple times
067            failedExceptions.add(exception);
068        }
069    }
070
071}