1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.cache.transactional;
16  
17  import com.liferay.portal.kernel.cache.PortalCache;
18  import com.liferay.portal.kernel.util.InitialThreadLocal;
19  import com.liferay.portal.util.PropsValues;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * <a href="TransactionalPortalCacheHelper.java.html"><b><i>View Source</i></b>
28   * </a>
29   *
30   * @author Shuyang Zhou
31   */
32  public class TransactionalPortalCacheHelper {
33  
34      public static void begin() {
35          if (!PropsValues.TRANSACTIONAL_CACHE_ENABLED) {
36              return;
37          }
38  
39          _pushPortalCacheMap();
40      }
41  
42      public static void commit() {
43          if (!PropsValues.TRANSACTIONAL_CACHE_ENABLED) {
44              return;
45          }
46  
47          Map<PortalCache, Map<String, Object>> portalCacheMap =
48              _popPortalCacheMap();
49  
50          for (Map.Entry<PortalCache, Map<String, Object>> portalCacheMapEntry :
51                  portalCacheMap.entrySet()) {
52  
53              PortalCache portalCache = portalCacheMapEntry.getKey();
54  
55              Map<String, Object> uncommittedMap = portalCacheMapEntry.getValue();
56  
57              for (Map.Entry<String, Object> uncommittedMapEntry :
58                      uncommittedMap.entrySet()) {
59  
60                  portalCache.put(
61                      uncommittedMapEntry.getKey(),
62                      uncommittedMapEntry.getValue());
63              }
64          }
65  
66          portalCacheMap.clear();
67      }
68  
69      public static boolean isEnabled() {
70          if (!PropsValues.TRANSACTIONAL_CACHE_ENABLED) {
71              return false;
72          }
73  
74          List<Map<PortalCache, Map<String, Object>>> portalCacheList =
75              _portalCacheListThreadLocal.get();
76  
77          return !portalCacheList.isEmpty();
78      }
79  
80      public static void rollback() {
81          if (!PropsValues.TRANSACTIONAL_CACHE_ENABLED) {
82              return;
83          }
84  
85          Map<PortalCache, Map<String, Object>> portalCacheMap =
86              _popPortalCacheMap();
87  
88          portalCacheMap.clear();
89      }
90  
91      protected static Object get(PortalCache portalCache, String key) {
92          Map<PortalCache, Map<String, Object>> portalCacheMap =
93              _peekPortalCacheMap();
94  
95          Map<String, Object> uncommittedMap = portalCacheMap.get(portalCache);
96  
97          if (uncommittedMap == null) {
98              return null;
99          }
100 
101         return uncommittedMap.get(key);
102     }
103 
104     protected static void put(
105         PortalCache portalCache, String key, Object value) {
106 
107         Map<PortalCache, Map<String, Object>> portalCacheMap =
108             _peekPortalCacheMap();
109 
110         Map<String, Object> uncommittedMap = portalCacheMap.get(portalCache);
111 
112         if (uncommittedMap == null) {
113             uncommittedMap = new HashMap<String, Object>();
114 
115             portalCacheMap.put(portalCache, uncommittedMap);
116         }
117 
118         uncommittedMap.put(key, value);
119     }
120 
121     protected static void remove(PortalCache portalCache, String key) {
122         Map<PortalCache, Map<String, Object>> portalCacheMap =
123             _peekPortalCacheMap();
124 
125         Map<String, Object> uncommittedMap = portalCacheMap.get(portalCache);
126 
127         if (uncommittedMap != null) {
128             uncommittedMap.remove(key);
129         }
130     }
131 
132     protected static void removeAll(PortalCache portalCache) {
133         Map<PortalCache, Map<String, Object>> portalCacheMap =
134             _peekPortalCacheMap();
135 
136         Map<String, Object> uncommittedMap = portalCacheMap.get(portalCache);
137 
138         if (uncommittedMap != null) {
139             uncommittedMap.clear();
140         }
141     }
142 
143     private static Map<PortalCache, Map<String, Object>> _peekPortalCacheMap() {
144         List<Map<PortalCache, Map<String, Object>>> portalCacheList =
145             _portalCacheListThreadLocal.get();
146 
147         return portalCacheList.get(portalCacheList.size() - 1);
148     }
149 
150     private static Map<PortalCache, Map<String, Object>> _popPortalCacheMap() {
151         List<Map<PortalCache, Map<String, Object>>> portalCacheList =
152             _portalCacheListThreadLocal.get();
153 
154         return portalCacheList.remove(portalCacheList.size() - 1);
155     }
156 
157     private static void _pushPortalCacheMap() {
158         List<Map<PortalCache, Map<String, Object>>> portalCacheList =
159             _portalCacheListThreadLocal.get();
160 
161         portalCacheList.add(new HashMap<PortalCache, Map<String, Object>>());
162     }
163 
164     private static ThreadLocal<List<Map<PortalCache, Map<String, Object>>>>
165         _portalCacheListThreadLocal =
166             new InitialThreadLocal<List<Map<PortalCache, Map<String, Object>>>>(
167                 new ArrayList<Map<PortalCache, Map<String, Object>>>());
168 
169 }