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.impl;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    import java.util.concurrent.CountDownLatch;
022    
023    import org.apache.camel.AsyncCallback;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.spi.Synchronization;
026    import org.apache.camel.spi.UnitOfWork;
027    
028    /**
029     * The default implementation of {@link UnitOfWork}
030     *
031     * @version $Revision: 41910 $
032     */
033    public class DefaultUnitOfWork implements UnitOfWork {
034        private List<Synchronization> synchronizations;
035        private List<AsyncCallback> asyncCallbacks;
036        private CountDownLatch latch;
037    
038        public DefaultUnitOfWork() {
039        }
040    
041        public synchronized void addSynchronization(Synchronization synchronization) {
042            if (synchronizations == null) {
043                synchronizations = new ArrayList<Synchronization>();
044            }
045            synchronizations.add(synchronization);
046        }
047    
048        public synchronized void removeSynchronization(Synchronization synchronization) {
049            if (synchronizations != null) {
050                synchronizations.remove(synchronization);
051            }
052        }
053    
054        public void reset() {
055        }
056    
057        public void done(Exchange exchange) {
058            if (synchronizations != null) {
059                boolean failed = exchange.isFailed();
060                for (Synchronization synchronization : synchronizations) {
061                    if (failed) {
062                        synchronization.onFailure(exchange);
063                    } else {
064                        synchronization.onComplete(exchange);
065                    }
066                }
067            }
068        }
069    
070        public boolean isSynchronous() {
071            return asyncCallbacks == null || asyncCallbacks.isEmpty();
072        }
073    
074        /**
075         * Register some asynchronous processing step
076         */
077        /*
078        public synchronized AsyncCallback addAsyncStep() {
079            AsyncCallback answer = new AsyncCallback() {
080                public void done(boolean doneSynchronously) {
081                    latch.countDown();
082                }
083            };
084            if (latch == null) {
085                latch = new CountDownLatch(1);
086            }
087            else {
088                // TODO increment latch!
089            }
090            if (asyncCallbacks == null) {
091                asyncCallbacks = new ArrayList<AsyncCallback>();
092            }
093            asyncCallbacks.add(answer);
094            return answer;
095        }
096        */
097    }