View Javadoc

1   /*
2    * $Id: SpringCacheObjectStore.java 22607 2011-08-08 02:13:54Z pablo.kraan $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Implements an {@link ObjectStore} using Spring module cache.
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      * Provides a place holder to store values inside a Spring cache to be able
136      * to save null values. This is required because the base API does not have
137      * a method to detect whether a given key is present in the cache or not.
138      *
139      * @param <T> the type of elements stored in the cache.
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 }