001/*
002 *  Copyright 2019 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.runtime.plugin;
017
018import java.util.Collection;
019import java.util.List;
020import java.util.Map;
021import java.util.Objects;
022import java.util.stream.Collectors;
023
024import org.ametys.runtime.plugin.IncludePolicyFeatureActivator.IncludedFeature;
025
026class IncludedFeatureConsistencyChecker
027{
028    private Collection<IncludedFeature> _includedFeatures;
029    private Map<String, Feature> _initialFeatures;
030    private Map<String, Collection<IncomingDeactivation>> _incomingDeactivations;
031
032    IncludedFeatureConsistencyChecker(Collection<IncludedFeature> includedFeatures, Map<String, Feature> initialFeatures, Map<String, Collection<IncomingDeactivation>> incomingDeactivations)
033    {
034        _includedFeatures = includedFeatures;
035        _initialFeatures = initialFeatures;
036        _incomingDeactivations = incomingDeactivations;
037    }
038    
039    void checkConsistency()
040    {
041        List<String> includedFeatures = _includedFeatures.stream()
042                .map(IncludedFeature::featureId)
043                .collect(Collectors.toList());
044        for (String includedFeatureId : includedFeatures)
045        {
046            _checkFeature(includedFeatureId);
047        }
048    }
049    
050    private void _checkFeature(String featureId)
051    {
052        Feature feature = Objects.requireNonNull(_initialFeatures.get(featureId), String.format("Feature '%s' cannot be found.", featureId));
053        Collection<String> dependencies = feature.getDependencies();
054        for (String dependency : dependencies)
055        {
056            if (_incomingDeactivations.containsKey(dependency) && _allDeactivationsAreDeactivatedType(dependency))
057            {
058                throw new IllegalStateException(String.format(
059                        "Problem of consistency with included features %s. At least one of them is deactivated by another. The application will not be able to boot.", 
060                        _includedFeatures));
061            }
062            _checkFeature(dependency);
063        }
064    }
065    
066    private boolean _allDeactivationsAreDeactivatedType(String dependency)
067    {
068        Collection<IncomingDeactivation> incomingDeactivations = _incomingDeactivations.get(dependency);
069        return incomingDeactivations.stream()
070                .map(IncomingDeactivation::getType)
071                .allMatch(IncomingDeactivation.Type.DEACTIVATED::equals);
072    }
073}