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