KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > reliable > DefaultReplayBuffer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.reliable;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  *
29  * @version $Revision: 426366 $
30  */

31 public class DefaultReplayBuffer implements ReplayBuffer {
32
33     private static final Log log = LogFactory.getLog(DefaultReplayBuffer.class);
34
35     private final int size;
36     private ReplayBufferListener listener;
37     private Map JavaDoc map;
38     private int lowestCommandId = 1;
39     private Object JavaDoc lock = new Object JavaDoc();
40
41     public DefaultReplayBuffer(int size) {
42         this.size = size;
43         map = createMap(size);
44     }
45
46     public void addBuffer(int commandId, Object JavaDoc buffer) {
47         if (log.isDebugEnabled()) {
48             log.debug("Adding command ID: " + commandId + " to replay buffer: " + this + " object: " + buffer);
49         }
50         synchronized (lock) {
51             int max = size - 1;
52             while (map.size() >= max) {
53                 // lets find things to evict
54
Object JavaDoc evictedBuffer = map.remove(new Integer JavaDoc(++lowestCommandId));
55                 onEvictedBuffer(lowestCommandId, evictedBuffer);
56             }
57             map.put(new Integer JavaDoc(commandId), buffer);
58         }
59     }
60
61     public void setReplayBufferListener(ReplayBufferListener bufferPoolAdapter) {
62         this.listener = bufferPoolAdapter;
63     }
64
65     public void replayMessages(int fromCommandId, int toCommandId, Replayer replayer) throws IOException JavaDoc {
66         if (replayer == null) {
67             throw new IllegalArgumentException JavaDoc("No Replayer parameter specified");
68         }
69         if (log.isDebugEnabled()) {
70             log.debug("Buffer: " + this + " replaying messages from: " + fromCommandId + " to: " + toCommandId);
71         }
72         for (int i = fromCommandId; i <= toCommandId; i++) {
73             Object JavaDoc buffer = null;
74             synchronized (lock) {
75                 buffer = map.get(new Integer JavaDoc(i));
76             }
77             replayer.sendBuffer(i, buffer);
78         }
79     }
80
81     protected Map JavaDoc createMap(int maximumSize) {
82         return new HashMap JavaDoc(maximumSize);
83     }
84
85     protected void onEvictedBuffer(int commandId, Object JavaDoc buffer) {
86         if (listener != null) {
87             listener.onBufferDiscarded(commandId, buffer);
88         }
89     }
90 }
91
Popular Tags