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 */ 017package org.apache.camel.processor.aggregate; 018 019import java.util.Collections; 020import java.util.Set; 021import java.util.concurrent.ConcurrentHashMap; 022import java.util.concurrent.ConcurrentMap; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.Exchange; 026import org.apache.camel.spi.OptimisticLockingAggregationRepository; 027import org.apache.camel.support.ServiceSupport; 028 029/** 030 * A memory based {@link org.apache.camel.spi.AggregationRepository} which stores {@link Exchange}s in memory only. 031 * 032 * Supports both optimistic locking and non-optimistic locking modes. Defaults to non-optimistic locking mode. 033 * 034 * @version 035 */ 036public class MemoryAggregationRepository extends ServiceSupport implements OptimisticLockingAggregationRepository { 037 private final ConcurrentMap<String, Exchange> cache = new ConcurrentHashMap<String, Exchange>(); 038 private final boolean optimisticLocking; 039 040 public MemoryAggregationRepository() { 041 this(false); 042 } 043 044 public MemoryAggregationRepository(boolean optimisticLocking) { 045 this.optimisticLocking = optimisticLocking; 046 } 047 048 public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) { 049 if (!optimisticLocking) { 050 throw new UnsupportedOperationException(); 051 } 052 if (oldExchange == null) { 053 if (cache.putIfAbsent(key, newExchange) != null) { 054 throw new OptimisticLockingException(); 055 } 056 } else { 057 if (!cache.replace(key, oldExchange, newExchange)) { 058 throw new OptimisticLockingException(); 059 } 060 } 061 return oldExchange; 062 } 063 064 public Exchange add(CamelContext camelContext, String key, Exchange exchange) { 065 if (optimisticLocking) { 066 throw new UnsupportedOperationException(); 067 } 068 return cache.put(key, exchange); 069 } 070 071 public Exchange get(CamelContext camelContext, String key) { 072 return cache.get(key); 073 } 074 075 public void remove(CamelContext camelContext, String key, Exchange exchange) { 076 if (optimisticLocking) { 077 if (!cache.remove(key, exchange)) { 078 throw new OptimisticLockingException(); 079 } 080 } else { 081 cache.remove(key); 082 } 083 } 084 085 public void confirm(CamelContext camelContext, String exchangeId) { 086 // noop 087 } 088 089 public Set<String> getKeys() { 090 // do not allow edits to the set 091 return Collections.unmodifiableSet(cache.keySet()); 092 } 093 094 @Override 095 protected void doStart() throws Exception { 096 } 097 098 @Override 099 protected void doStop() throws Exception { 100 cache.clear(); 101 } 102 103}