001/*
002 *  Copyright 2018 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.core.ui.resources;
017
018import java.util.Comparator;
019import java.util.Map;
020import java.util.Set;
021import java.util.function.Function;
022import java.util.function.Predicate;
023
024import org.ametys.plugins.core.ui.minimize.HashCache.UriData;
025import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
026
027/**
028 * Extension point for calculating a resource's dependencies
029 */
030public class ResourceDependenciesListExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<ResourceDependenciesList>
031{
032    /** The avalon ROLE */
033    public static final String ROLE = ResourceDependenciesListExtensionPoint.class.getName();
034    
035    /**
036     * Get the list of dependencies of a resource
037     * @param uri The resource uri
038     * @param data Additional data, specific per ResourceDependenciesList implementations
039     * @return The list of dependencies' URIs, or null if no extension supports this URI
040     */
041    public Set<UriData> getDependencies(String uri, Map<String, String> data)
042    {
043        Predicate<ResourceDependenciesList> isUriSupported = ext -> ext.isSupported(uri);
044        Function<ResourceDependenciesList, Set<UriData>> getUriDependenciesList = ext -> ext.getDependenciesList(uri, data);
045        
046        return getExtensionsIds().stream()
047                .map(this::getExtension)
048                .filter(isUriSupported)
049                .sorted(Comparator.comparing(ResourceDependenciesList::getPriority).reversed())
050                .findFirst()
051                .map(getUriDependenciesList)
052                .orElse(null);
053    }
054}