001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.util;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portlet.wiki.PageContentException;
025    import com.liferay.portlet.wiki.model.WikiPage;
026    import com.liferay.portlet.wiki.model.WikiPageDisplay;
027    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
028    
029    import java.util.Map;
030    
031    import javax.portlet.PortletURL;
032    
033    import org.apache.commons.lang.time.StopWatch;
034    
035    /**
036     * @author Jorge Ferrer
037     */
038    public class WikiCacheUtil {
039    
040            public static void clearCache(long nodeId) {
041                    _portalCache.removeAll();
042            }
043    
044            public static void clearCache(long nodeId, String title) {
045                    clearCache(nodeId);
046            }
047    
048            public static WikiPageDisplay getDisplay(
049                    long nodeId, String title, PortletURL viewPageURL,
050                    PortletURL editPageURL, String attachmentURLPrefix) {
051    
052                    StopWatch stopWatch = null;
053    
054                    if (_log.isDebugEnabled()) {
055                            stopWatch = new StopWatch();
056    
057                            stopWatch.start();
058                    }
059    
060                    String key = _encodeKey(nodeId, title, viewPageURL.toString());
061    
062                    WikiPageDisplay pageDisplay = (WikiPageDisplay)_portalCache.get(key);
063    
064                    if (pageDisplay == null) {
065                            pageDisplay = _getPageDisplay(
066                                    nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
067    
068                            _portalCache.put(key, pageDisplay);
069                    }
070    
071                    if (_log.isDebugEnabled()) {
072                            _log.debug(
073                                    "getDisplay for {" + nodeId + ", " + title + ", " +
074                                            viewPageURL + ", " + editPageURL + "} takes " +
075                                                    stopWatch.getTime() + " ms");
076                    }
077    
078                    return pageDisplay;
079            }
080    
081            public static Map<String, Boolean> getOutgoingLinks(WikiPage page)
082                    throws PageContentException {
083    
084                    String key = _encodeKey(
085                            page.getNodeId(), page.getTitle(), _OUTGOING_LINKS);
086    
087                    Map<String, Boolean> links = (Map<String, Boolean>)_portalCache.get(
088                            key);
089    
090                    if (links == null) {
091                            links = WikiUtil.getLinks(page);
092    
093                            _portalCache.put(key, links);
094                    }
095    
096                    return links;
097            }
098    
099            private static String _encodeKey(
100                    long nodeId, String title, String postfix) {
101    
102                    StringBundler sb = new StringBundler(6);
103    
104                    sb.append(_CACHE_NAME);
105                    sb.append(StringPool.POUND);
106                    sb.append(StringUtil.toHexString(nodeId));
107                    sb.append(title);
108    
109                    if (postfix != null) {
110                            sb.append(StringPool.POUND);
111                            sb.append(postfix);
112                    }
113    
114                    return sb.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 final String _CACHE_NAME = WikiCacheUtil.class.getName();
143    
144            private static final String _OUTGOING_LINKS = "OUTGOING_LINKS";
145    
146            private static Log _log = LogFactoryUtil.getLog(WikiCacheUtil.class);
147    
148            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
149                    _CACHE_NAME);
150    
151    }