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: 45123 $ 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 public String getId() { 079 if (id == null) { 080 id = DEFAULT_ID_GENERATOR.generateId(); 081 } 082 return id; 083 } 084 085 /** 086 * Register some asynchronous processing step 087 */ 088 /* 089 public synchronized AsyncCallback addAsyncStep() { 090 AsyncCallback answer = new AsyncCallback() { 091 public void done(boolean doneSynchronously) { 092 latch.countDown(); 093 } 094 }; 095 if (latch == null) { 096 latch = new CountDownLatch(1); 097 } 098 else { 099 // TODO increment latch! 100 } 101 if (asyncCallbacks == null) { 102 asyncCallbacks = new ArrayList<AsyncCallback>(); 103 } 104 asyncCallbacks.add(answer); 105 return answer; 106 } 107 */ 108 }