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