001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journalcontent.util;
022:
023: import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
024: import com.liferay.portal.kernel.cache.PortalCache;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.theme.ThemeDisplay;
030: import com.liferay.portlet.journal.model.JournalArticleDisplay;
031: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
032: import com.liferay.util.CollectionFactory;
033:
034: import java.util.Map;
035:
036: import org.apache.commons.lang.time.StopWatch;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * <a href="JournalContentUtil.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: * @author Raymond Augé
045: * @author Michael Young
046: *
047: */
048: public class JournalContentUtil {
049:
050: public static final String CACHE_NAME = JournalContentUtil.class
051: .getName();
052:
053: public static String ARTICLE_SEPARATOR = "_ARTICLE_";
054:
055: public static String TEMPLATE_SEPARATOR = "_TEMPLATE_";
056:
057: public static String LANGUAGE_SEPARATOR = "_LANGUAGE_";
058:
059: public static String PAGE_SEPARATOR = "_PAGE_";
060:
061: public static void clearCache() {
062: _cache.removeAll();
063: }
064:
065: public static void clearCache(long groupId, String articleId,
066: String templateId) {
067:
068: articleId = GetterUtil.getString(articleId).toUpperCase();
069: templateId = GetterUtil.getString(templateId).toUpperCase();
070:
071: String groupKey = _encodeGroupKey(groupId, articleId,
072: templateId);
073:
074: MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
075: }
076:
077: public static String getContent(long groupId, String articleId,
078: String languageId, ThemeDisplay themeDisplay) {
079:
080: return getContent(groupId, articleId, null, languageId,
081: themeDisplay);
082: }
083:
084: public static String getContent(long groupId, String articleId,
085: String templateId, String languageId,
086: ThemeDisplay themeDisplay) {
087:
088: JournalArticleDisplay articleDisplay = getDisplay(groupId,
089: articleId, templateId, languageId, themeDisplay);
090:
091: if (articleDisplay != null) {
092: return articleDisplay.getContent();
093: } else {
094: return null;
095: }
096: }
097:
098: public static JournalArticleDisplay getDisplay(long groupId,
099: String articleId, String templateId, String languageId,
100: ThemeDisplay themeDisplay) {
101:
102: return getDisplay(groupId, articleId, templateId, languageId,
103: themeDisplay, 1, null);
104: }
105:
106: public static JournalArticleDisplay getDisplay(long groupId,
107: String articleId, String templateId, String languageId,
108: ThemeDisplay themeDisplay, int page, String xmlRequest) {
109:
110: StopWatch stopWatch = null;
111:
112: if (_log.isDebugEnabled()) {
113: stopWatch = new StopWatch();
114:
115: stopWatch.start();
116: }
117:
118: articleId = GetterUtil.getString(articleId).toUpperCase();
119: templateId = GetterUtil.getString(templateId).toUpperCase();
120:
121: String key = _encodeKey(groupId, articleId, templateId,
122: languageId, page);
123:
124: JournalArticleDisplay articleDisplay = (JournalArticleDisplay) MultiVMPoolUtil
125: .get(_cache, key);
126:
127: if (articleDisplay == null) {
128: articleDisplay = _getArticleDisplay(groupId, articleId,
129: templateId, languageId, page, xmlRequest,
130: themeDisplay);
131:
132: if ((articleDisplay != null)
133: && articleDisplay.isCacheable()) {
134: String groupKey = _encodeGroupKey(groupId, articleId,
135: templateId);
136:
137: MultiVMPoolUtil.put(_cache, key, _groups, groupKey,
138: articleDisplay);
139: }
140: }
141:
142: if (_log.isDebugEnabled()) {
143: _log.debug("getDisplay for {" + groupId + ", " + articleId
144: + ", " + templateId + ", " + languageId + ", "
145: + page + "} takes " + stopWatch.getTime() + " ms");
146: }
147:
148: return articleDisplay;
149: }
150:
151: private static String _encodeGroupKey(long groupId,
152: String articleId, String templateId) {
153:
154: return _encodeKey(groupId, articleId, templateId, null, 0);
155: }
156:
157: private static String _encodeKey(long groupId, String articleId,
158: String templateId, String languageId, int page) {
159:
160: StringMaker sm = new StringMaker();
161:
162: sm.append(CACHE_NAME);
163: sm.append(StringPool.POUND);
164: sm.append(groupId);
165: sm.append(ARTICLE_SEPARATOR);
166: sm.append(articleId);
167: sm.append(TEMPLATE_SEPARATOR);
168: sm.append(templateId);
169:
170: if (Validator.isNotNull(languageId)) {
171: sm.append(LANGUAGE_SEPARATOR);
172: sm.append(languageId);
173: }
174:
175: if (page > 0) {
176: sm.append(PAGE_SEPARATOR);
177: sm.append(page);
178: }
179:
180: return sm.toString();
181: }
182:
183: private static JournalArticleDisplay _getArticleDisplay(
184: long groupId, String articleId, String templateId,
185: String languageId, int page, String xmlRequest,
186: ThemeDisplay themeDisplay) {
187:
188: try {
189: if (_log.isInfoEnabled()) {
190: _log.info("Get article display {" + groupId + ", "
191: + articleId + ", " + templateId + "}");
192: }
193:
194: return JournalArticleLocalServiceUtil.getArticleDisplay(
195: groupId, articleId, templateId, languageId, page,
196: xmlRequest, themeDisplay);
197: } catch (Exception e) {
198: if (_log.isWarnEnabled()) {
199: _log.warn("Unable to get display for " + groupId + " "
200: + articleId + " " + languageId);
201: }
202:
203: return null;
204: }
205: }
206:
207: private static Log _log = LogFactory
208: .getLog(JournalContentUtil.class);
209:
210: private static PortalCache _cache = MultiVMPoolUtil
211: .getCache(CACHE_NAME);
212:
213: private static Map _groups = CollectionFactory.getSyncHashMap();
214:
215: }
|