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.activemq.transport.reliable;
018    
019    import java.io.IOException;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    /**
027     * 
028     * 
029     */
030    public class DefaultReplayBuffer implements ReplayBuffer {
031    
032        private static final Logger LOG = LoggerFactory.getLogger(DefaultReplayBuffer.class);
033    
034        private final int size;
035        private ReplayBufferListener listener;
036        private Map<Integer, Object> map;
037        private int lowestCommandId = 1;
038        private Object lock = new Object();
039    
040        public DefaultReplayBuffer(int size) {
041            this.size = size;
042            map = createMap(size);
043        }
044    
045        public void addBuffer(int commandId, Object buffer) {
046            if (LOG.isDebugEnabled()) {
047                LOG.debug("Adding command ID: " + commandId + " to replay buffer: " + this + " object: " + buffer);
048            }
049            synchronized (lock) {
050                int max = size - 1;
051                while (map.size() >= max) {
052                    // lets find things to evict
053                    Object evictedBuffer = map.remove(Integer.valueOf(++lowestCommandId));
054                    onEvictedBuffer(lowestCommandId, evictedBuffer);
055                }
056                map.put(Integer.valueOf(commandId), buffer);
057            }
058        }
059    
060        public void setReplayBufferListener(ReplayBufferListener bufferPoolAdapter) {
061            this.listener = bufferPoolAdapter;
062        }
063    
064        public void replayMessages(int fromCommandId, int toCommandId, Replayer replayer) throws IOException {
065            if (replayer == null) {
066                throw new IllegalArgumentException("No Replayer parameter specified");
067            }
068            if (LOG.isDebugEnabled()) {
069                LOG.debug("Buffer: " + this + " replaying messages from: " + fromCommandId + " to: " + toCommandId);
070            }
071            for (int i = fromCommandId; i <= toCommandId; i++) {
072                Object buffer = null;
073                synchronized (lock) {
074                    buffer = map.get(Integer.valueOf(i));
075                }
076                replayer.sendBuffer(i, buffer);
077            }
078        }
079    
080        protected Map<Integer, Object> createMap(int maximumSize) {
081            return new HashMap<Integer, Object>(maximumSize);
082        }
083    
084        protected void onEvictedBuffer(int commandId, Object buffer) {
085            if (listener != null) {
086                listener.onBufferDiscarded(commandId, buffer);
087            }
088        }
089    }