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.pageelement;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Set;
023
024import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
025
026/**
027 * Extension point for {@link PageElementCachePolicy}.
028 */
029public class PageElementCachePolicyExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<PageElementCachePolicy>
030{
031    private Map<String, Set<PageElementCachePolicy>> _policies;
032    
033    @Override
034    public void initializeExtensions() throws Exception
035    {
036        super.initializeExtensions();
037        
038        _policies = new HashMap<>();
039        
040        for (String id : getExtensionsIds())
041        {
042            PageElementCachePolicy policy = getExtension(id);
043            
044            for (String pageElementType : policy.getPageElementTypes())
045            {
046                Set<PageElementCachePolicy> policies = _policies.get(pageElementType);
047                
048                if (policies == null)
049                {
050                    policies = new HashSet<>();
051                    _policies.put(pageElementType, policies);
052                }
053                
054                policies.add(policy);
055            }
056        }
057    }
058    
059    /**
060     * Returns all {@link PageElementCachePolicy} associated with the given page element type.
061     * @param pageElementType the element type.
062     * @return a Set of {@link PageElementCachePolicy}.
063     */
064    public Set<PageElementCachePolicy> getPolicies(String pageElementType)
065    {
066        if (_policies.containsKey(pageElementType))
067        {
068            return _policies.get(pageElementType);
069        }
070        else
071        {
072            return Collections.emptySet();
073        }
074    }
075}