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