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.plugins.externaldata.data;
017
018import java.util.Collection;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.ametys.core.datasource.DataSourceClientInteraction.DataSourceType;
023import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
024
025/**
026 * Extension point for DataSource factories.
027 */
028public class DataSourceFactoryExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<DataSourceFactory>
029{
030    
031    /** The Avalon role. */
032    public static final String ROLE = DataSourceFactoryExtensionPoint.class.getName();
033    
034    /**
035     * Get all types handled by the various declared factories.
036     * @return the types as a Collection.
037     */
038    public Collection<DataSourceType> getAllTypes()
039    {
040        Set<DataSourceType> types = new HashSet<>();
041        for (String extensionId : getExtensionsIds())
042        {
043            DataSourceFactory extensionFactory = getExtension(extensionId);
044            types.addAll(extensionFactory.getHandledTypes());
045        }
046        return types;
047    }
048    
049    /**
050     * Get the factory handling the specified type.
051     * @param type the data source type.
052     * @return the DataSourceFactory handling the specified type, null if no factory handles this type.
053     */
054    public DataSourceFactory getFactory(DataSourceType type)
055    {
056        for (String extensionId : getExtensionsIds())
057        {
058            DataSourceFactory extensionFactory = getExtension(extensionId);
059            if (extensionFactory.getHandledTypes().contains(type))
060            {
061                return extensionFactory;
062            }
063        }
064        return null;
065    }
066    
067}