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