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.Service;
026    import org.apache.camel.spi.Synchronization;
027    import org.apache.camel.spi.UnitOfWork;
028    import org.apache.camel.util.UuidGenerator;
029    
030    /**
031     * The default implementation of {@link UnitOfWork}
032     *
033     * @version $Revision: 1322 $
034     */
035    public class DefaultUnitOfWork implements UnitOfWork, Service {
036        private static final UuidGenerator DEFAULT_ID_GENERATOR = new UuidGenerator();
037    
038        private String id;
039        private List<Synchronization> synchronizations;
040        private List<AsyncCallback> asyncCallbacks;
041        private CountDownLatch latch;
042    
043        public DefaultUnitOfWork() {
044        }
045    
046        public void start() throws Exception {
047        }
048    
049        public void stop() throws Exception {
050            // need to clean up when we are stopping to not leak memory
051            if (synchronizations != null) {
052                synchronizations.clear();
053            }
054            if (asyncCallbacks != null) {
055                asyncCallbacks.clear();
056            }
057        }
058    
059        public synchronized void addSynchronization(Synchronization synchronization) {
060            if (synchronizations == null) {
061                synchronizations = new ArrayList<Synchronization>();
062            }
063            synchronizations.add(synchronization);
064        }
065    
066        public synchronized void removeSynchronization(Synchronization synchronization) {
067            if (synchronizations != null) {
068                synchronizations.remove(synchronization);
069            }
070        }
071    
072        /**
073         * @deprecated will be removed in Camel 2.0
074         */
075        public void reset() {
076        }
077    
078        public void done(Exchange exchange) {
079            if (synchronizations != null) {
080                boolean failed = exchange.isFailed();
081                for (Synchronization synchronization : synchronizations) {
082                    if (failed) {
083                        synchronization.onFailure(exchange);
084                    } else {
085                        synchronization.onComplete(exchange);
086                    }
087                }
088            }
089        }
090    
091        public boolean isSynchronous() {
092            return asyncCallbacks == null || asyncCallbacks.isEmpty();
093        }
094    
095        public String getId() {
096            if (id == null) {
097                id = DEFAULT_ID_GENERATOR.generateId();
098            }
099            return id;
100        }
101    
102        /**
103         * Register some asynchronous processing step
104         */
105        /*
106        public synchronized AsyncCallback addAsyncStep() {
107            AsyncCallback answer = new AsyncCallback() {
108                public void done(boolean doneSynchronously) {
109                    latch.countDown();
110                }
111            };
112            if (latch == null) {
113                latch = new CountDownLatch(1);
114            }
115            else {
116                // TODO increment latch!
117            }
118            if (asyncCallbacks == null) {
119                asyncCallbacks = new ArrayList<AsyncCallback>();
120            }
121            asyncCallbacks.add(answer);
122            return answer;
123        }
124        */
125    }