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.contentio.in;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.Comparator;
021import java.util.List;
022
023import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
024
025/**
026 * Extension point for {@link ContentImporter}s.
027 */
028public class ContentImporterExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<ContentImporter>
029{
030    /** Avalon Role */
031    public static final String ROLE = ContentImporterExtensionPoint.class.getName();
032    
033    /** Compares importers by ascending priority. */
034    protected static ContentImporterPriorityComparator _IMPORTER_COMPARATOR = new ContentImporterPriorityComparator();
035    
036    /** The importers, sorted by priority. */
037    protected List<ContentImporter> _importersByPriority;
038    
039    /**
040     * Get the importer IDs, sorted by ascending priority.
041     * @return the importer IDs, sorted by ascending priority.
042     */
043    public List<ContentImporter> getImportersByPriority()
044    {
045        // On the first call, create, fill and sort the importer list.
046        if (_importersByPriority == null)
047        {
048            _importersByPriority = new ArrayList<>();
049            
050            for (String id : getExtensionsIds())
051            {
052                ContentImporter importer = getExtension(id);
053                
054                _importersByPriority.add(importer);
055            }
056            
057            Collections.sort(_importersByPriority, _IMPORTER_COMPARATOR);
058        }
059        
060        // Return the 
061        return Collections.unmodifiableList(_importersByPriority);
062    }
063    
064    /**
065     * Compares importers by ascending priority
066     */
067    protected static class ContentImporterPriorityComparator implements Comparator<ContentImporter>
068    {
069        @Override
070        public int compare(ContentImporter i1, ContentImporter i2)
071        {
072            return i1.getPriority() - i2.getPriority();
073        }
074    }
075    
076}