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 import org.apache.camel.util.UuidGenerator;
028
029 /**
030 * The default implementation of {@link UnitOfWork}
031 *
032 * @version $Revision: 670436 $
033 */
034 public class DefaultUnitOfWork implements UnitOfWork {
035 private static final UuidGenerator DEFAULT_ID_GENERATOR = new UuidGenerator();
036
037 private String id;
038 private List<Synchronization> synchronizations;
039 private List<AsyncCallback> asyncCallbacks;
040 private CountDownLatch latch;
041
042 public DefaultUnitOfWork() {
043 }
044
045 public synchronized void addSynchronization(Synchronization synchronization) {
046 if (synchronizations == null) {
047 synchronizations = new ArrayList<Synchronization>();
048 }
049 synchronizations.add(synchronization);
050 }
051
052 public synchronized void removeSynchronization(Synchronization synchronization) {
053 if (synchronizations != null) {
054 synchronizations.remove(synchronization);
055 }
056 }
057
058 public void reset() {
059 }
060
061 public void done(Exchange exchange) {
062 if (synchronizations != null) {
063 boolean failed = exchange.isFailed();
064 for (Synchronization synchronization : synchronizations) {
065 if (failed) {
066 synchronization.onFailure(exchange);
067 } else {
068 synchronization.onComplete(exchange);
069 }
070 }
071 }
072 }
073
074 public boolean isSynchronous() {
075 return asyncCallbacks == null || asyncCallbacks.isEmpty();
076 }
077
078 /**
079 * Returns the unique ID of this unit of work, lazily creating one if it does not yet have one
080 *
081 * @return
082 */
083 public String getId() {
084 if (id == null) {
085 id = DEFAULT_ID_GENERATOR.generateId();
086 }
087 return id;
088 }
089
090 /**
091 * Register some asynchronous processing step
092 */
093 /*
094 public synchronized AsyncCallback addAsyncStep() {
095 AsyncCallback answer = new AsyncCallback() {
096 public void done(boolean doneSynchronously) {
097 latch.countDown();
098 }
099 };
100 if (latch == null) {
101 latch = new CountDownLatch(1);
102 }
103 else {
104 // TODO increment latch!
105 }
106 if (asyncCallbacks == null) {
107 asyncCallbacks = new ArrayList<AsyncCallback>();
108 }
109 asyncCallbacks.add(answer);
110 return answer;
111 }
112 */
113 }