KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > queue > CachingPersistenceStrategy


1 /*
2  * $Id: CachingPersistenceStrategy.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util.queue;
12
13 import org.apache.commons.collections.map.ReferenceMap;
14
15 import java.io.IOException JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 public class CachingPersistenceStrategy implements QueuePersistenceStrategy
21 {
22
23     private QueuePersistenceStrategy ps;
24     private Map objects;
25
26     public CachingPersistenceStrategy(QueuePersistenceStrategy ps)
27     {
28         this.ps = ps;
29         this.objects = Collections.synchronizedMap(new ReferenceMap());
30     }
31
32     public void open() throws IOException JavaDoc
33     {
34         ps.open();
35     }
36
37     public void close() throws IOException JavaDoc
38     {
39         objects.clear();
40         ps.close();
41     }
42
43     public Object JavaDoc load(String JavaDoc queue, Object JavaDoc id) throws IOException JavaDoc
44     {
45         // XXX: is this something wanted as a check?
46
// Object obj = objects.get(id);
47
return ps.load(queue, id);
48     }
49
50     public void remove(String JavaDoc queue, Object JavaDoc id) throws IOException JavaDoc
51     {
52         objects.remove(id);
53         ps.remove(queue, id);
54     }
55
56     public List JavaDoc restore() throws IOException JavaDoc
57     {
58         return ps.restore();
59     }
60
61     public Object JavaDoc store(String JavaDoc queue, Object JavaDoc obj) throws IOException JavaDoc
62     {
63         Object JavaDoc id = ps.store(queue, obj);
64         objects.put(id, obj);
65         return id;
66     }
67
68     public boolean isTransient()
69     {
70         return ps.isTransient();
71     }
72 }
73
Popular Tags