001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.cache; 017 018import java.util.Map; 019 020import javax.jcr.Repository; 021import javax.jcr.Session; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.core.observation.Event; 028import org.ametys.core.observation.Observer; 029import org.ametys.runtime.plugin.component.AbstractLogEnabled; 030import org.ametys.web.WebConstants; 031import org.ametys.web.repository.site.Site; 032 033/** 034 * Abstract {@link Observer} for invalidate site data. 035 */ 036public abstract class AbstractSiteCacheObserver extends AbstractLogEnabled implements Observer, Serviceable 037{ 038 /** JCR repository. */ 039 protected Repository _repository; 040 /** Cache invalidation policy */ 041 protected CacheInvalidationPolicy _cachePolicy; 042 /** The cache helper */ 043 protected FOCommHelper _foCommHelper; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 _repository = (Repository) manager.lookup(Repository.class.getName()); 049 _cachePolicy = (CacheInvalidationPolicy) manager.lookup(CacheInvalidationPolicy.class.getName()); 050 _foCommHelper = (FOCommHelper) manager.lookup(FOCommHelper.ROLE); 051 } 052 053 public int getPriority() 054 { 055 // Will be processed after Solr update 056 return Observer.MAX_PRIORITY + 4000; 057 } 058 059 public void observe(Event event, Map<String, Object> transientVars) throws Exception 060 { 061 try 062 { 063 Site site = _getSite(event); 064 if (site != null) 065 { 066 Session liveSession = null; 067 068 try 069 { 070 // Open a session to the workspace live 071 liveSession = _repository.login(WebConstants.LIVE_WORKSPACE); 072 _internalObserve(event, site, liveSession); 073 } 074 finally 075 { 076 if (liveSession != null) 077 { 078 liveSession.logout(); 079 } 080 } 081 } 082 } 083 catch (Exception e) 084 { 085 getLogger().error("Unable to invalidate cache from event: " + event, e); 086 } 087 } 088 089 /** 090 * Retrieves the site needed for accessing the front-office. 091 * @param event the event. 092 * @return the site or <code>null</code> if not found. 093 */ 094 protected abstract Site _getSite(Event event); 095 096 /** 097 * Observes the event with access to the target site and live workspace. 098 * @param event the event. 099 * @param site the site. 100 * @param liveSession the session to the workspace live. 101 * @throws Exception if an error occurs. 102 */ 103 protected abstract void _internalObserve(Event event, Site site, Session liveSession) throws Exception; 104}