1
2
3
4
5
6
7
8
9
10
11 package org.mule.util.store;
12
13 import org.mule.api.store.ObjectAlreadyExistsException;
14 import org.mule.api.store.ObjectDoesNotExistException;
15 import org.mule.api.store.ObjectStore;
16 import org.mule.api.store.ObjectStoreException;
17 import org.mule.config.i18n.CoreMessages;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24
25
26
27
28
29 public abstract class AbstractObjectStore<T extends Serializable> implements ObjectStore<T>
30 {
31 protected final Log logger = LogFactory.getLog(getClass());
32
33 public boolean contains(Serializable key) throws ObjectStoreException
34 {
35 if (key == null)
36 {
37 throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
38 }
39 return doContains(key);
40 }
41
42 protected abstract boolean doContains(Serializable key) throws ObjectStoreException;
43
44 public void store(Serializable key, T value) throws ObjectStoreException
45 {
46 if (key == null)
47 {
48 throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
49 }
50
51 if (contains(key))
52 {
53 throw new ObjectAlreadyExistsException();
54 }
55
56 doStore(key, value);
57 }
58
59 protected abstract void doStore(Serializable key, T value) throws ObjectStoreException;
60
61 public T retrieve(Serializable key) throws ObjectStoreException
62 {
63 if (key == null)
64 {
65 throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
66 }
67
68 if (contains(key) == false)
69 {
70 String message = "Key does not exist: " + key;
71 throw new ObjectDoesNotExistException(CoreMessages.createStaticMessage(message));
72 }
73
74 return doRetrieve(key);
75 }
76
77 protected abstract T doRetrieve(Serializable key) throws ObjectStoreException;
78
79 public T remove(Serializable key) throws ObjectStoreException
80 {
81 if (key == null)
82 {
83 throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
84 }
85
86 if (contains(key) == false)
87 {
88 throw new ObjectDoesNotExistException();
89 }
90
91 return doRemove(key);
92 }
93
94 protected abstract T doRemove(Serializable key) throws ObjectStoreException;
95 }