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