001/*
002 *  Copyright 2012 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.service;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.configuration.Configurable;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.logger.AbstractLogEnabled;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.core.observation.Event;
029import org.ametys.web.cache.pageelement.PageElementCachePolicy;
030import org.ametys.web.repository.site.Site;
031
032/**
033 * {@link PageElementCachePolicy} for isolated services (services which don't display
034 * modifiable data from other objects, such as Iframe service).<br>
035 * The shouldClearCache method always return PolicyResult.KEEP.
036 */
037public class IsolatedServiceCachePolicy extends AbstractLogEnabled  implements PageElementCachePolicy, Configurable
038{
039    
040    /** The handled types. */
041    protected Set<String> _types;
042    
043    @Override
044    public void configure(Configuration configuration) throws ConfigurationException
045    {
046        _types = new HashSet<>();
047        for (Configuration serviceConf : configuration.getChildren("service"))
048        {
049            String serviceId = serviceConf.getAttribute("id", "");
050            if (StringUtils.isNotBlank(serviceId))
051            {
052                _types.add("SERVICE:" + serviceId);
053            }
054        }
055    }
056    
057    @Override
058    public Set<String> getPageElementTypes()
059    {
060        return Collections.unmodifiableSet(_types);
061    }
062    
063    @Override
064    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event)
065    {
066        return PolicyResult.KEEP;
067    }
068    
069    @Override
070    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event)
071    {
072        // Never called because the first-level method never returns NEED_INFORMATION.
073        throw new UnsupportedOperationException("Should never be called.");
074    }
075    
076}