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) { throw new UnsupportedOperationException(); }
050        if (oldExchange == null) {
051            if (cache.putIfAbsent(key, newExchange) != null) {
052                throw new OptimisticLockingException();
053            }
054        } else {
055            if (!cache.replace(key, oldExchange, newExchange)) {
056                throw new OptimisticLockingException();
057            }
058        }
059        return oldExchange;
060    }
061
062    public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
063        if (optimisticLocking) { throw new UnsupportedOperationException(); }
064        return cache.put(key, exchange);
065    }
066
067    public Exchange get(CamelContext camelContext, String key) {
068        return cache.get(key);
069    }
070
071    public void remove(CamelContext camelContext, String key, Exchange exchange) {
072        if (optimisticLocking) {
073            if (!cache.remove(key, exchange)) {
074                throw new OptimisticLockingException();
075            }
076        } else {
077            cache.remove(key);
078        }
079    }
080
081    public void confirm(CamelContext camelContext, String exchangeId) {
082        // noop
083    }
084
085    public Set<String> getKeys() {
086        // do not allow edits to the set
087        return Collections.unmodifiableSet(cache.keySet());
088    }
089
090    @Override
091    protected void doStart() throws Exception {
092    }
093
094    @Override
095    protected void doStop() throws Exception {
096        cache.clear();
097    }
098
099}