1
2
3
4
5
6
7 package org.mule.module.spring.objectstore;
8
9 import org.mule.api.store.ObjectAlreadyExistsException;
10 import org.mule.api.store.ObjectDoesNotExistException;
11 import org.mule.api.store.ObjectStore;
12 import org.mule.api.store.ObjectStoreException;
13 import org.mule.config.i18n.CoreMessages;
14
15 import java.io.Serializable;
16
17 import org.springmodules.cache.CachingModel;
18 import org.springmodules.cache.provider.CacheProviderFacade;
19
20
21
22
23 public class SpringCacheObjectStore<T extends Serializable> implements ObjectStore<T>
24 {
25
26 private CacheProviderFacade cacheProvider;
27 private CachingModel cachingModel;
28
29 public boolean contains(Serializable key) throws ObjectStoreException
30 {
31 synchronized (cacheProvider)
32 {
33 assertKeyNotNull(key);
34
35 try
36 {
37 return cacheProvider.getFromCache(key, cachingModel) != null;
38 }
39 catch (Exception e)
40 {
41 throw new ObjectStoreException(e);
42 }
43 }
44 }
45
46 public void store(Serializable key, T value) throws ObjectStoreException
47 {
48 synchronized (cacheProvider)
49 {
50 if (contains(key))
51 {
52 throw new ObjectAlreadyExistsException();
53 }
54
55 ObjectStoreValue<T> wrappedValue = new ObjectStoreValue<T>(value);
56 cacheProvider.putInCache(key, cachingModel, wrappedValue);
57 }
58 }
59
60 public T retrieve(Serializable key) throws ObjectStoreException
61 {
62 assertKeyNotNull(key);
63
64 synchronized (cacheProvider)
65 {
66 ObjectStoreValue cachedValue = (ObjectStoreValue) cacheProvider.getFromCache(key, cachingModel);
67
68 if (cachedValue == null)
69 {
70 throw new ObjectDoesNotExistException(CoreMessages.objectNotFound(key));
71 }
72 else
73 {
74 return (T) cachedValue.getValue();
75 }
76 }
77 }
78
79 public T remove(Serializable key) throws ObjectStoreException
80 {
81 synchronized (cacheProvider)
82 {
83 if (contains(key))
84 {
85 ObjectStoreValue objectStoreValue = (ObjectStoreValue) cacheProvider.getFromCache(key, cachingModel);
86 cacheProvider.removeFromCache(key, cachingModel);
87
88 return (T) objectStoreValue.getValue();
89 }
90 else
91 {
92 throw new ObjectDoesNotExistException(CoreMessages.objectNotFound(key));
93 }
94 }
95 }
96
97 public CacheProviderFacade getCacheProvider()
98 {
99 return cacheProvider;
100 }
101
102 public void setCacheProvider(CacheProviderFacade cacheProvider)
103 {
104 this.cacheProvider = cacheProvider;
105 }
106
107 public CachingModel getCachingModel()
108 {
109 return cachingModel;
110 }
111
112 public void setCachingModel(CachingModel cachingModel)
113 {
114 this.cachingModel = cachingModel;
115 }
116
117 private void assertKeyNotNull(Serializable key) throws ObjectStoreException
118 {
119 if (key == null)
120 {
121 throw new ObjectStoreException(CoreMessages.objectIsNull("id"));
122 }
123 }
124
125
126
127
128
129
130
131
132 public static class ObjectStoreValue<T extends Serializable> implements Serializable
133 {
134
135 private final T value;
136
137 public ObjectStoreValue(T value)
138 {
139 this.value = value;
140 }
141
142 public T getValue()
143 {
144 return value;
145 }
146 }
147 }