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.portal.cms.servlet;
022:
023: import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
024: import com.liferay.portal.kernel.cache.PortalCache;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.theme.ThemeDisplay;
029: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <a href="CMSServletUtil.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: * @author Raymond Augé
039: *
040: */
041: public class CMSServletUtil {
042:
043: public static final String CACHE_NAME = CMSServletUtil.class
044: .getName();
045:
046: public static String LANGUAGE = "_LANGUAGE_";
047:
048: public static void clearCache() {
049: _cache.removeAll();
050: }
051:
052: public static String getContent(long groupId, String articleId,
053: String languageId, ThemeDisplay themeDisplay) {
054:
055: if (Validator.isNull(articleId)) {
056: return null;
057: }
058:
059: articleId = articleId.trim().toUpperCase();
060:
061: String key = _encodeKey(articleId, languageId);
062:
063: String content = (String) MultiVMPoolUtil.get(_cache, key);
064:
065: if (content == null) {
066: try {
067: content = JournalArticleLocalServiceUtil
068: .getArticleContent(groupId, articleId,
069: languageId, themeDisplay);
070: } catch (Exception e) {
071: if (_log.isWarnEnabled()) {
072: _log.warn(e.getMessage());
073: }
074: }
075:
076: if (content != null) {
077: MultiVMPoolUtil.put(_cache, key, content);
078: }
079: }
080:
081: return content;
082: }
083:
084: private static String _encodeKey(String articleId, String languageId) {
085: StringMaker sm = new StringMaker();
086:
087: sm.append(CACHE_NAME);
088: sm.append(StringPool.POUND);
089: sm.append(articleId);
090: sm.append(StringPool.POUND);
091: sm.append(languageId);
092:
093: return sm.toString();
094: }
095:
096: private static Log _log = LogFactory.getLog(CMSServletUtil.class);
097:
098: private static PortalCache _cache = MultiVMPoolUtil
099: .getCache(CACHE_NAME);
100:
101: }
|