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.io.Serializable;
030    
031    import java.util.Map;
032    
033    import javax.portlet.PortletURL;
034    
035    import org.apache.commons.lang.time.StopWatch;
036    
037    /**
038     * @author Jorge Ferrer
039     */
040    public class WikiCacheUtil {
041    
042            public static void clearCache(long nodeId) {
043                    _portalCache.removeAll();
044            }
045    
046            public static void clearCache(long nodeId, String title) {
047                    clearCache(nodeId);
048            }
049    
050            public static WikiPageDisplay getDisplay(
051                    long nodeId, String title, PortletURL viewPageURL,
052                    PortletURL editPageURL, String attachmentURLPrefix) {
053    
054                    StopWatch stopWatch = new StopWatch();
055    
056                    stopWatch.start();
057    
058                    String key = _encodeKey(nodeId, title, viewPageURL.toString());
059    
060                    WikiPageDisplay pageDisplay = (WikiPageDisplay)_portalCache.get(key);
061    
062                    if (pageDisplay == null) {
063                            pageDisplay = _getPageDisplay(
064                                    nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
065    
066                            _portalCache.put(key, pageDisplay);
067                    }
068    
069                    if (_log.isDebugEnabled()) {
070                            _log.debug(
071                                    "getDisplay for {" + nodeId + ", " + title + ", " +
072                                            viewPageURL + ", " + editPageURL + "} takes " +
073                                                    stopWatch.getTime() + " ms");
074                    }
075    
076                    return pageDisplay;
077            }
078    
079            public static Map<String, Boolean> getOutgoingLinks(WikiPage page)
080                    throws PageContentException {
081    
082                    String key = _encodeKey(
083                            page.getNodeId(), page.getTitle(), _OUTGOING_LINKS);
084    
085                    Map<String, Boolean> links = (Map<String, Boolean>)_portalCache.get(
086                            key);
087    
088                    if (links == null) {
089                            links = WikiUtil.getLinks(page);
090    
091                            _portalCache.put(key, (Serializable)links);
092                    }
093    
094                    return links;
095            }
096    
097            private static String _encodeKey(
098                    long nodeId, String title, String postfix) {
099    
100                    StringBundler sb = new StringBundler(6);
101    
102                    sb.append(_CACHE_NAME);
103                    sb.append(StringPool.POUND);
104                    sb.append(StringUtil.toHexString(nodeId));
105                    sb.append(title);
106    
107                    if (postfix != null) {
108                            sb.append(StringPool.POUND);
109                            sb.append(postfix);
110                    }
111    
112                    return sb.toString();
113            }
114    
115            private static WikiPageDisplay _getPageDisplay(
116                    long nodeId, String title, PortletURL viewPageURL,
117                    PortletURL editPageURL, String attachmentURLPrefix) {
118    
119                    try {
120                            if (_log.isInfoEnabled()) {
121                                    _log.info(
122                                            "Get page display for {" + nodeId + ", " + title + ", " +
123                                                    viewPageURL + ", " + editPageURL + "}");
124                            }
125    
126                            return WikiPageLocalServiceUtil.getPageDisplay(
127                                    nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
128                    }
129                    catch (Exception e) {
130                            if (_log.isWarnEnabled()) {
131                                    _log.warn(
132                                            "Unable to get page display for {" + nodeId + ", " + title +
133                                                    ", " + viewPageURL + ", " + editPageURL + "}");
134                            }
135    
136                            return null;
137                    }
138            }
139    
140            private static final String _CACHE_NAME = WikiCacheUtil.class.getName();
141    
142            private static final String _OUTGOING_LINKS = "OUTGOING_LINKS";
143    
144            private static Log _log = LogFactoryUtil.getLog(WikiCacheUtil.class);
145    
146            private static PortalCache<String, Serializable> _portalCache =
147                    MultiVMPoolUtil.getCache(_CACHE_NAME);
148    
149    }