001/*
002 *  Copyright 2014 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.plugins.explorer.resources.metadata.populate;
017
018import java.util.Collection;
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 ResourceMetadataPopulator}s.
028 */
029public class ResourceMetadataPopulatorExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<ResourceMetadataPopulator>
030{
031    /** Avalon Role */
032    public static final String ROLE = ResourceMetadataPopulatorExtensionPoint.class.getName();
033
034    private Map<String, Set<ResourceMetadataPopulator>> _types;
035    
036    @Override
037    public void initializeExtensions() throws Exception
038    {
039        super.initializeExtensions();
040        
041        initializePopulatorMap();
042    }
043    
044    /**
045     * Initialize the populator map by type.
046     */
047    protected void initializePopulatorMap()
048    {
049        _types = new HashMap<>();
050        
051        for (String id : getExtensionsIds())
052        {
053            ResourceMetadataPopulator populator = getExtension(id);
054            
055            if (populator.getTypes() == null || populator.getTypes().isEmpty())
056            {
057                // Default: use this populator for all the file types.
058                addPopulator("", populator);
059            }
060            else
061            {
062                for (String type : populator.getTypes())
063                {
064                    addPopulator(type, populator);
065                }
066            }
067        }
068    }
069
070    /**
071     * Add the populator to the map.
072     * @param type the mime type or the empty string.
073     * @param populator the populator.
074     */
075    protected void addPopulator(String type, ResourceMetadataPopulator populator)
076    {
077        Set<ResourceMetadataPopulator> types;
078        if (_types.containsKey(type))
079        {
080            types = _types.get(type);
081        }
082        else
083        {
084            types = new HashSet<>();
085            _types.put(type, types);
086        }
087        
088        types.add(populator);
089    }
090    
091    /**
092     * Returns the {@link ResourceMetadataPopulator}s corresponding to the given type.
093     * @param type the MIME type.
094     * @return a Collection of {@link ResourceMetadataPopulator}s, empty if there is no populator for the given type.
095     */
096    public Collection<ResourceMetadataPopulator> getPopulators(String type)
097    {
098        Set<ResourceMetadataPopulator> populators = new HashSet<>();
099        
100        // Add default populators.
101        if (_types.containsKey(""))
102        {
103            populators.addAll(_types.get(""));
104        }
105        
106        if (_types.containsKey(type))
107        {
108            populators.addAll(_types.get(type));
109        }
110        
111        return populators;
112    }
113    
114}