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: 36321 $ 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 058 public void done(Exchange exchange) { 059 if (synchronizations != null) { 060 boolean failed = exchange.isFailed(); 061 for (Synchronization synchronization : synchronizations) { 062 if (failed) { 063 synchronization.onFailure(exchange); 064 } else { 065 synchronization.onComplete(exchange); 066 } 067 } 068 } 069 } 070 071 public boolean isSynchronous() { 072 return asyncCallbacks == null || asyncCallbacks.isEmpty(); 073 } 074 075 /** 076 * Register some asynchronous processing step 077 */ 078 /* 079 public synchronized AsyncCallback addAsyncStep() { 080 AsyncCallback answer = new AsyncCallback() { 081 public void done(boolean doneSynchronously) { 082 latch.countDown(); 083 } 084 }; 085 if (latch == null) { 086 latch = new CountDownLatch(1); 087 } 088 else { 089 // TODO increment latch! 090 } 091 if (asyncCallbacks == null) { 092 asyncCallbacks = new ArrayList<AsyncCallback>(); 093 } 094 asyncCallbacks.add(answer); 095 return answer; 096 } 097 */ 098 }