1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.util;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portlet.wiki.PageContentException;
24  import com.liferay.portlet.wiki.model.WikiPage;
25  import com.liferay.portlet.wiki.model.WikiPageDisplay;
26  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
27  
28  import java.util.Map;
29  
30  import javax.portlet.PortletURL;
31  
32  import org.apache.commons.lang.time.StopWatch;
33  
34  /**
35   * <a href="WikiCacheUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Jorge Ferrer
38   */
39  public class WikiCacheUtil {
40  
41      public static final String CACHE_NAME = WikiCacheUtil.class.getName();
42  
43      public static void clearCache(long nodeId) {
44          _cache.removeAll();
45      }
46  
47      public static void clearCache(long nodeId, String title) {
48          clearCache(nodeId);
49      }
50  
51      public static WikiPageDisplay getDisplay(
52          long nodeId, String title, PortletURL viewPageURL,
53          PortletURL editPageURL, String attachmentURLPrefix) {
54  
55          StopWatch stopWatch = null;
56  
57          if (_log.isDebugEnabled()) {
58              stopWatch = new StopWatch();
59  
60              stopWatch.start();
61          }
62  
63          String key = _encodeKey(nodeId, title, viewPageURL.toString());
64  
65          WikiPageDisplay pageDisplay = (WikiPageDisplay)_cache.get(key);
66  
67          if (pageDisplay == null) {
68              pageDisplay = _getPageDisplay(
69                  nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
70  
71              _cache.put(key, pageDisplay);
72          }
73  
74          if (_log.isDebugEnabled()) {
75              _log.debug(
76                  "getDisplay for {" + nodeId + ", " + title + ", " +
77                      viewPageURL + ", " + editPageURL + "} takes " +
78                          stopWatch.getTime() + " ms");
79          }
80  
81          return pageDisplay;
82      }
83  
84      public static Map<String, Boolean> getOutgoingLinks(WikiPage page)
85          throws PageContentException {
86  
87          String key = _encodeKey(
88              page.getNodeId(), page.getTitle(), _OUTGOING_LINKS);
89  
90          Map<String, Boolean> links = (Map<String, Boolean>)_cache.get(key);
91  
92          if (links == null) {
93              links = WikiUtil.getLinks(page);
94  
95              _cache.put(key, links);
96          }
97  
98          return links;
99      }
100 
101     private static String _encodeKey(
102         long nodeId, String title, String postfix) {
103 
104         StringBundler sb = new StringBundler(6);
105 
106         sb.append(CACHE_NAME);
107         sb.append(StringPool.POUND);
108         sb.append(nodeId);
109         sb.append(title);
110 
111         if (postfix != null) {
112             sb.append(StringPool.POUND);
113             sb.append(postfix);
114         }
115 
116         return sb.toString();
117     }
118 
119     private static WikiPageDisplay _getPageDisplay(
120         long nodeId, String title, PortletURL viewPageURL,
121         PortletURL editPageURL, String attachmentURLPrefix) {
122 
123         try {
124             if (_log.isInfoEnabled()) {
125                 _log.info(
126                     "Get page display for {" + nodeId + ", " + title + ", " +
127                         viewPageURL + ", " + editPageURL + "}");
128             }
129 
130             return WikiPageLocalServiceUtil.getPageDisplay(
131                 nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
132         }
133         catch (Exception e) {
134             if (_log.isWarnEnabled()) {
135                 _log.warn(
136                     "Unable to get page display for {" + nodeId + ", " + title +
137                         ", " + viewPageURL + ", " + editPageURL + "}");
138             }
139 
140             return null;
141         }
142     }
143 
144     private static final String _OUTGOING_LINKS = "OUTGOING_LINKS";
145 
146     private static Log _log = LogFactoryUtil.getLog(WikiUtil.class);
147 
148     private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
149 
150 }