001/*
002 *  Copyright 2016 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.core.datasource;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
022
023/**
024 * This class is in charge to load and initialize the various {@link DataSourceConsumer} 
025 */
026public class DataSourceConsumerExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<DataSourceConsumer>
027{
028    /** Avalon Role */
029    public static final String ROLE = DataSourceConsumerExtensionPoint.class.getName();
030
031    /**
032     * Determines if a data source is in use by a {@link DataSourceConsumer}
033     * @param id The id of the data source to check
034     * @return true if the data source is used by at least one of the {@link DataSourceConsumer}
035     */
036    public boolean isInUse(String id)
037    {
038        for (String extensionId : getExtensionsIds())
039        {
040            DataSourceConsumer dataSourceClient = getExtension(extensionId);
041            if (dataSourceClient.isInUse(id))
042            {
043                return true;
044            }
045        }
046        
047        return false;
048    }
049    
050    /**
051     * Retrieve the ids of the used data sources
052     * @return the set of ids of data source used by all of the {@link DataSourceConsumer}
053     */
054    public Set<String> getUsedDataSourceIds()
055    {
056        Set<String> extensionsIds = getExtensionsIds();
057        Set<String> usedDataSourceIds = new HashSet<>();
058        
059        for (String extensionId : extensionsIds)
060        {
061            DataSourceConsumer dataSourceClient = getExtension(extensionId);
062            usedDataSourceIds.addAll(dataSourceClient.getUsedDataSourceIds());
063        }
064        
065        return usedDataSourceIds;
066    }
067}