1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.cache;
24  
25  import com.liferay.portal.kernel.cache.PortalCache;
26  import com.liferay.portal.kernel.cache.SingleVMPool;
27  import com.liferay.portal.util.PropsUtil;
28  
29  import java.io.Serializable;
30  
31  import java.net.URL;
32  
33  import net.sf.ehcache.Cache;
34  import net.sf.ehcache.CacheManager;
35  import net.sf.ehcache.Element;
36  
37  /**
38   * <a href="SingleVMPoolImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Michael Young
42   *
43   */
44  public class SingleVMPoolImpl implements SingleVMPool {
45  
46      public void clear() {
47          _cacheManager.clearAll();
48      }
49  
50      public void clear(String name) {
51          PortalCache portalCache = getCache(name);
52  
53          portalCache.removeAll();
54      }
55  
56      public Object get(String name, String key) {
57          PortalCache portalCache = getCache(name);
58  
59          return get(portalCache, key);
60      }
61  
62      public Object get(PortalCache portalCache, String key) {
63          Element element = (Element)portalCache.get(key);
64  
65          if (element == null) {
66              return null;
67          }
68          else {
69              return element.getObjectValue();
70          }
71      }
72  
73      public PortalCache getCache(String name) {
74          Cache cache = _cacheManager.getCache(name);
75  
76          if (cache == null) {
77              _cacheManager.addCache(name);
78  
79              cache = _cacheManager.getCache(name);
80          }
81  
82          return new PortalCacheImpl(cache);
83      }
84  
85      public void put(String name, String key, Object obj) {
86          PortalCache portalCache = getCache(name);
87  
88          put(portalCache, key, obj);
89      }
90  
91      public void put(PortalCache portalCache, String key, Object obj) {
92          portalCache.put(key, obj);
93      }
94  
95      public void put(String name, String key, Serializable obj) {
96          PortalCache portalCache = getCache(name);
97  
98          put(portalCache, key, obj);
99      }
100 
101     public void put(PortalCache portalCache, String key, Serializable obj) {
102         portalCache.put(key, obj);
103     }
104 
105     public void remove(String name, String key) {
106         PortalCache portalCache = getCache(name);
107 
108         remove(portalCache, key);
109     }
110 
111     public void remove(PortalCache portalCache, String key) {
112         portalCache.remove(key);
113     }
114 
115     private SingleVMPoolImpl() {
116         String configLocation = PropsUtil.get(
117             PropsUtil.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
118 
119         URL url = getClass().getResource(configLocation);
120 
121         _cacheManager = new CacheManager(url);
122     }
123 
124     private CacheManager _cacheManager;
125 
126 }