1   /**
2    * Copyright (c) 2000-2008 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.portlet.wiki.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
28  import com.liferay.portal.kernel.cache.PortalCache;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portlet.wiki.model.WikiPageDisplay;
32  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
33  
34  import java.util.Map;
35  import java.util.Set;
36  import java.util.concurrent.ConcurrentHashMap;
37  
38  import javax.portlet.PortletURL;
39  
40  import org.apache.commons.lang.time.StopWatch;
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="WikiCacheUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
50  public class WikiCacheUtil {
51  
52      public static final String CACHE_NAME = WikiCacheUtil.class.getName();
53  
54      public static void clearCache(long nodeId, String title) {
55          String groupKey = _encodeGroupKey(nodeId, title);
56  
57          MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
58      }
59  
60      public static WikiPageDisplay getDisplay(
61              long nodeId, String title, PortletURL viewPageURL,
62              PortletURL editPageURL, String attachmentURLPrefix)
63          throws PortalException, SystemException {
64  
65          StopWatch stopWatch = null;
66  
67          if (_log.isDebugEnabled()) {
68              stopWatch = new StopWatch();
69  
70              stopWatch.start();
71          }
72  
73          String key = _encodeKey(nodeId, title, viewPageURL);
74          String groupKey = _encodeGroupKey(nodeId, title);
75  
76          WikiPageDisplay pageDisplay = (WikiPageDisplay)MultiVMPoolUtil.get(
77              _cache, key);
78  
79          if (pageDisplay == null) {
80              pageDisplay = _getPageDisplay(
81                  nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
82  
83              MultiVMPoolUtil.put(_cache, key, _groups, groupKey, pageDisplay);
84          }
85  
86          if (_log.isDebugEnabled()) {
87              _log.debug(
88                  "getDisplay for {" + nodeId + ", " + title + ", " +
89                      viewPageURL + ", " + editPageURL + "} takes " +
90                          stopWatch.getTime() + " ms");
91          }
92  
93          return pageDisplay;
94      }
95  
96      private static String _encodeGroupKey(long nodeId, String title) {
97          return _encodeKey(nodeId, title, null);
98      }
99  
100     private static String _encodeKey(
101         long nodeId, String title, PortletURL viewPageURL) {
102 
103         StringMaker sm = new StringMaker();
104 
105         sm.append(CACHE_NAME);
106         sm.append(StringPool.POUND);
107         sm.append(nodeId);
108         sm.append(title);
109 
110         if (viewPageURL != null) {
111             sm.append(viewPageURL);
112         }
113 
114         return sm.toString();
115     }
116 
117     private static WikiPageDisplay _getPageDisplay(
118         long nodeId, String title, PortletURL viewPageURL,
119         PortletURL editPageURL, String attachmentURLPrefix) {
120 
121         try {
122             if (_log.isInfoEnabled()) {
123                 _log.info(
124                     "Get page display for {" + nodeId + ", " + title + ", " +
125                         viewPageURL + ", " + editPageURL + "}");
126             }
127 
128             return WikiPageLocalServiceUtil.getPageDisplay(
129                 nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
130         }
131         catch (Exception e) {
132             if (_log.isWarnEnabled()) {
133                 _log.warn(
134                     "Unable to get page display for {" + nodeId + ", " + title +
135                         ", " + viewPageURL + ", " + editPageURL + "}");
136             }
137 
138             return null;
139         }
140     }
141 
142     private static Log _log = LogFactory.getLog(WikiUtil.class);
143 
144     private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
145 
146     private static Map<String, Set<String>> _groups =
147         new ConcurrentHashMap<String, Set<String>>();
148 
149 }