001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.cms.impl.interceptors;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.cms.CMSInterceptor;
026: import org.jboss.portal.cms.impl.CMSCacheException;
027: import org.jboss.portal.cms.impl.CMSTreeCacheService;
028: import org.jboss.portal.cms.impl.jcr.JCRCommand;
029: import org.jboss.portal.cms.impl.jcr.command.DeleteCommand;
030: import org.jboss.portal.cms.impl.jcr.command.FileGetCommand;
031: import org.jboss.portal.cms.impl.jcr.composite.UpdateFileCommand;
032: import org.jboss.portal.cms.model.File;
033: import org.jboss.portal.common.invocation.InvocationException;
034:
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037: import java.util.Locale;
038:
039: /** @author <a href="mailto:theute@jboss.org">Thomas Heute</a> */
040: public class CacheInterceptor extends CMSInterceptor {
041:
042: private CMSTreeCacheService cache;
043:
044: public Logger log = Logger.getLogger(CacheInterceptor.class);
045:
046: private void loadCache() {
047: try {
048: InitialContext ic = new InitialContext();
049: cache = (CMSTreeCacheService) ic
050: .lookup(CMSTreeCacheService.JNDIName);
051: } catch (NamingException e) {
052: log
053: .warn("CMSTreeCache service in not available in the JNDI Tree as: "
054: + CMSTreeCacheService.JNDIName);
055: }
056: }
057:
058: protected Object invoke(JCRCommand invocation) throws Exception,
059: InvocationException {
060: // lazy loading of cache
061: if (cache == null) {
062: loadCache();
063: }
064:
065: if (invocation instanceof DeleteCommand) {
066: // delete from cache
067: DeleteCommand dc = (DeleteCommand) invocation;
068: String path = dc.getPath();
069: try {
070: log.trace(path + " removed from cache");
071: cache.remove(path);
072: } catch (CMSCacheException e) {
073: e.printStackTrace();
074: }
075: return invocation.invokeNext();
076: } else if (invocation instanceof UpdateFileCommand) {
077: // delete from cache
078: UpdateFileCommand ufc = (UpdateFileCommand) invocation;
079: try {
080: String path = ufc.getPath();
081: Locale locale = ufc.getLocale();
082: log.trace(path + "/" + locale + " removed from cache");
083: cache.remove(path, locale);
084: } catch (CMSCacheException e) {
085: e.printStackTrace();
086: }
087: return invocation.invokeNext();
088: } else if ((invocation instanceof FileGetCommand)) {
089: FileGetCommand fgc = (FileGetCommand) invocation;
090: String path = fgc.getPath();
091: Locale locale = fgc.getLocale();
092:
093: File file = cache.get(path, locale);
094: if (file != null) {
095: log.trace(file.getBasePath() + "/" + locale
096: + " obtained from cache");
097: return file;
098: } else {
099: File result = (File) invocation.invokeNext();
100: if (result != null) {
101: log
102: .trace(path + "/" + locale
103: + " written to cache");
104: cache.put(path, locale, result);
105: }
106: return result;
107: }
108: } else {
109: log.trace("Command not trapped by the cache interceptor: "
110: + invocation);
111: return invocation.invokeNext();
112: }
113: }
114: }
|