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.web.WebConstants;
030import org.ametys.web.repository.site.Site;
031
032/**
033 * Abstract {@link Observer} for invalidate site data.
034 */
035public abstract class AbstractSiteCacheObserver extends AbstractCacheObserver implements Serviceable
036{
037    /** JCR repository. */
038    protected Repository _repository;
039    /** Cache invalidation policy */
040    protected CacheInvalidationPolicy _cachePolicy;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _repository = (Repository) manager.lookup(Repository.class.getName());
046        _cachePolicy = (CacheInvalidationPolicy) manager.lookup(CacheInvalidationPolicy.class.getName());
047    }
048    
049    @Override
050    public void observe(Event event, Map<String, Object> transientVars) throws Exception
051    {
052        try
053        {
054            Site site = _getSite(event);
055            if (site != null)
056            {
057                Session liveSession = null; 
058                
059                try
060                {
061                    // Open a session to the workspace live
062                    liveSession = _repository.login(WebConstants.LIVE_WORKSPACE);
063                    _internalObserve(event, site, liveSession);
064                }
065                finally
066                {
067                    if (liveSession != null)
068                    {
069                        liveSession.logout();
070                    }
071                }
072            }
073        }
074        catch (Exception e)
075        {
076            getLogger().error("Unable to invalidate cache from event: " + event, e);
077        }
078    }
079    
080    /**
081     * Retrieves the site needed for accessing the front-office.
082     * @param event the event.
083     * @return the site or <code>null</code> if not found.
084     */
085    protected abstract Site _getSite(Event event);
086    
087    /**
088     * Observes the event with access to the target site and live workspace.
089     * @param event the event.
090     * @param site the site.
091     * @param liveSession the session to the workspace live.
092     * @throws Exception if an error occurs.
093     */
094    protected abstract void _internalObserve(Event event, Site site, Session liveSession) throws Exception;
095}