KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MemoryPersistenceStrategy.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.safehaus.uuid.UUIDGenerator;
14
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:gnt@codehaus.org">Guillaume Nodet</a>
24  * @version $Revision: 3798 $
25  */

26 public class MemoryPersistenceStrategy implements QueuePersistenceStrategy
27 {
28
29     private UUIDGenerator gen = UUIDGenerator.getInstance();
30
31     private Map JavaDoc map = Collections.synchronizedMap(new HashMap JavaDoc());
32
33     protected Object JavaDoc getId(Object JavaDoc obj)
34     {
35         return gen.generateRandomBasedUUID();
36     }
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#store(java.lang.Object)
42      */

43     public Object JavaDoc store(String JavaDoc queue, Object JavaDoc obj) throws IOException JavaDoc
44     {
45         if (obj == null)
46         {
47             throw new IllegalArgumentException JavaDoc("Cannot store null object.");
48         }
49         Object JavaDoc id = getId(obj);
50         map.put(id, obj);
51         return id;
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#load(java.lang.Object)
58      */

59     public Object JavaDoc load(String JavaDoc queue, Object JavaDoc id) throws IOException JavaDoc
60     {
61         return map.get(id);
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#remove(java.lang.Object)
68      */

69     public void remove(String JavaDoc queue, Object JavaDoc id) throws IOException JavaDoc
70     {
71         map.remove(id);
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#restore()
78      */

79     public List JavaDoc restore() throws IOException JavaDoc
80     {
81         return new ArrayList JavaDoc();
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#open()
88      */

89     public void open() throws IOException JavaDoc
90     {
91         // nothing to do
92
}
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#close()
98      */

99     public void close() throws IOException JavaDoc
100     {
101         // nothing to do
102
}
103
104     public boolean isTransient()
105     {
106         return true;
107     }
108 }
109
Popular Tags