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     */
017    package org.apache.camel.util;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.spi.Synchronization;
025    import org.slf4j.Logger;
026    
027    /**
028     * @version 
029     */
030    public final class UnitOfWorkHelper {
031    
032        private UnitOfWorkHelper() {
033        }
034    
035        public static void doneSynchronizations(Exchange exchange, List<Synchronization> synchronizations, Logger log) {
036            boolean failed = exchange.isFailed();
037    
038            if (synchronizations != null && !synchronizations.isEmpty()) {
039                // work on a copy of the list to avoid any modification which may cause ConcurrentModificationException
040                List<Synchronization> copy = new ArrayList<Synchronization>(synchronizations);
041    
042                // reverse so we invoke it FILO style instead of FIFO
043                Collections.reverse(copy);
044                // and honor if any was ordered by sorting it accordingly
045                Collections.sort(copy, new OrderedComparator());
046    
047                // invoke synchronization callbacks
048                for (Synchronization synchronization : copy) {
049                    try {
050                        if (failed) {
051                            log.trace("Invoking synchronization.onFailure: {} with {}", synchronization, exchange);
052                            synchronization.onFailure(exchange);
053                        } else {
054                            log.trace("Invoking synchronization.onComplete: {} with {}", synchronization, exchange);
055                            synchronization.onComplete(exchange);
056                        }
057                    } catch (Throwable e) {
058                        // must catch exceptions to ensure all synchronizations have a chance to run
059                        log.warn("Exception occurred during onCompletion. This exception will be ignored.", e);
060                    }
061                }
062            }
063        }
064    
065    }