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