View Javadoc

1   /***
2    * @(#)EhCacheServiceImpl.java
3    *  
4    * JFoxSOAF, Service-Oriented Application Framework
5    * 
6    * Copyright(c) JFoxSOAF Team
7    * 
8    * Licensed under the GNU LGPL, Version 2.1 (the "License"); 
9    * you may not use this file except in compliance with the License. 
10   * You may obtain a copy of the License at  
11   * 
12   * http://www.gnu.org/copyleft/lesser.html
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, 
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17   * See the License for the specific language governing permissions and 
18   * limitations under the License. 
19   * 
20   * For more information, please visit:
21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22   * http://www.huihoo.org/jfox/jfoxsoaf
23   */
24  
25  package org.huihoo.jfox.soaf.services.cache;
26  
27  import java.io.IOException;
28  import java.io.Serializable;
29  
30  import net.sf.ehcache.Cache;
31  import net.sf.ehcache.CacheException;
32  import net.sf.ehcache.Element;
33  
34  import org.huihoo.jfox.soaf.exception.CacheServiceException;
35  
36  /***
37   * <p>
38   * Support for ehcache.
39   * </p>
40   * 
41   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
42   * @version $Revision: 1.1 $ $Date: 2005/05/22 06:48:52 $
43   * @version Revision: 1.0
44   */
45  
46  public class EhCacheServiceImpl implements CacheService {
47  
48      private Cache cache;
49  
50      /***
51       * Creates a new pluggable cache based on a cache name.
52       * 
53       * @param cache The underlying EhCache instance to use.
54       */
55      public EhCacheServiceImpl(Cache cache) {
56          this.cache = cache;
57      }
58  
59      /***
60       * @see org.huihoo.jfox.soaf.services.cache.CacheService#get(java.lang.Object)
61       */
62      public Object get(Object key) throws CacheServiceException {
63          try {
64              if (key == null) {
65                  return null;
66              } else {
67                  Element element = cache.get((Serializable) key);
68                  if (element == null) {
69                      return null;
70                  } else {
71                      return element.getValue();
72                  }
73              }
74          } catch (CacheException e) {
75              throw new CacheServiceException(e);
76          }
77      }
78  
79      /***
80       * @see org.huihoo.jfox.soaf.services.cache.CacheService#put(java.lang.Object,
81       *      java.lang.Object)
82       */
83      public void put(Object key, Object value) throws CacheServiceException {
84          try {
85              Element element = new Element((Serializable) key,
86                      (Serializable) value);
87              cache.put(element);
88          } catch (IllegalArgumentException e) {
89              throw new CacheServiceException(e);
90          } catch (IllegalStateException e) {
91              throw new CacheServiceException(e);
92          }
93  
94      }
95  
96      /***
97       * @see org.huihoo.jfox.soaf.services.cache.CacheService#remove(java.lang.Object)
98       */
99      public void remove(Object key) throws CacheServiceException {
100         try {
101             cache.remove((Serializable) key);
102         } catch (ClassCastException e) {
103             throw new CacheServiceException(e);
104         } catch (IllegalStateException e) {
105             throw new CacheServiceException(e);
106         }
107 
108     }
109 
110     /***
111      * @see org.huihoo.jfox.soaf.services.cache.CacheService#clear()
112      */
113     public void clear() throws CacheServiceException {
114         try {
115             cache.removeAll();
116         } catch (IllegalStateException e) {
117             throw new CacheServiceException(e);
118         } catch (IOException e) {
119             throw new CacheServiceException(e);
120         }
121 
122     }
123 
124 }