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.MultiVMPool;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.util.CollectionFactory;
29  
30  import java.io.Serializable;
31  
32  import java.net.URL;
33  
34  import java.util.Map;
35  import java.util.Set;
36  
37  import net.sf.ehcache.Cache;
38  import net.sf.ehcache.CacheManager;
39  import net.sf.ehcache.Element;
40  
41  /**
42   * <a href="MultiVMPoolImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Michael Young
46   *
47   */
48  public class MultiVMPoolImpl implements MultiVMPool {
49  
50      public void clear() {
51          _cacheManager.clearAll();
52      }
53  
54      public void clear(String name) {
55          PortalCache portalCache = getCache(name);
56  
57          portalCache.removeAll();
58      }
59  
60      public void clearGroup(
61          Map groups, String groupKey, PortalCache portalCache) {
62  
63          if (!groups.containsKey(groupKey)) {
64              return;
65          }
66  
67          Set groupKeys = (Set)groups.get(groupKey);
68  
69          String[] keys = (String[])groupKeys.toArray(new String[0]);
70  
71          for (int i = 0; i < keys.length; i++) {
72              String key = keys[i];
73  
74              // The functionality here pretty much mimics OSCache groups. It is
75              // not necessary to remove the keys in dependent groups because they
76              // will be cleared when the group itself is cleared, resulting in a
77              // performance boost.
78  
79              portalCache.remove(key);
80          }
81  
82          groupKeys.clear();
83      }
84  
85      public Object get(String name, String key) {
86          PortalCache portalCache = getCache(name);
87  
88          return get(portalCache, key);
89      }
90  
91      public Object get(PortalCache portalCache, String key) {
92          Element element = (Element)portalCache.get(key);
93  
94          if (element == null) {
95              return null;
96          }
97          else {
98              return element.getObjectValue();
99          }
100     }
101 
102     public PortalCache getCache(String name) {
103         Cache cache = _cacheManager.getCache(name);
104 
105         if (cache == null) {
106             _cacheManager.addCache(name);
107 
108             cache = _cacheManager.getCache(name);
109         }
110 
111         return new PortalCacheImpl(cache);
112     }
113 
114     public void put(String name, String key, Object obj) {
115         PortalCache portalCache = getCache(name);
116 
117         put(portalCache, key, obj);
118     }
119 
120     public void put(PortalCache portalCache, String key, Object obj) {
121         portalCache.put(key, obj);
122     }
123 
124     public void put(
125         PortalCache portalCache, String key, Map groups, String groupKey,
126         Object obj) {
127 
128         put(portalCache, key, obj);
129 
130         updateGroup(groups, groupKey, key);
131     }
132 
133     public void put(String name, String key, Serializable obj) {
134         PortalCache portalCache = getCache(name);
135 
136         put(portalCache, key, obj);
137     }
138 
139     public void put(PortalCache portalCache, String key, Serializable obj) {
140         portalCache.put(key, obj);
141     }
142 
143     public void put(
144         PortalCache portalCache, String key, Map groups, String groupKey,
145         Serializable obj) {
146 
147         put(portalCache, key, obj);
148 
149         updateGroup(groups, groupKey, key);
150     }
151 
152     public void remove(String name, String key) {
153         PortalCache portalCache = getCache(name);
154 
155         remove(portalCache, key);
156     }
157 
158     public void remove(PortalCache portalCache, String key) {
159         portalCache.remove(key);
160     }
161 
162     public void updateGroup(Map groups, String groupKey, String key) {
163         Set groupKeys = null;
164 
165         if (groups.containsKey(groupKey)) {
166             groupKeys = (Set)groups.get(groupKey);
167         }
168         else {
169             groupKeys = CollectionFactory.getSyncHashSet();
170 
171             groups.put(groupKey, groupKeys);
172         }
173 
174         groupKeys.add(key);
175     }
176 
177     private MultiVMPoolImpl() {
178         String configLocation = PropsUtil.get(
179             PropsUtil.EHCACHE_MULTI_VM_CONFIG_LOCATION);
180 
181         URL url = getClass().getResource(configLocation);
182 
183         _cacheManager = new CacheManager(url);
184     }
185 
186     private CacheManager _cacheManager;
187 
188 }