001/*
002 *  Copyright 2024 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.web.live;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024import java.util.regex.Pattern;
025
026import org.apache.avalon.framework.component.Component;
027import org.apache.avalon.framework.configuration.Configuration;
028import org.apache.avalon.framework.configuration.ConfigurationException;
029
030import org.ametys.runtime.plugin.ExtensionPoint;
031
032/**
033 * This extension point is in charge of handling Patterns of jcr path to exclude from Live synchronization.
034 */
035public class LiveWorkspaceExcludedPathExtensionPoint implements ExtensionPoint<Collection<Pattern>>, Component
036{
037    /** Avalon role */
038    public static final String ROLE = LiveWorkspaceExcludedPathExtensionPoint.class.getName();
039    
040    private Map<String, Collection<Pattern>> _extensions = new HashMap<>();
041    
042    @Override
043    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
044    {
045        List<Pattern> patterns = new ArrayList<>();
046        for (Configuration patternConfig : configuration.getChildren("pattern"))
047        {
048            String patternValue = patternConfig.getValue();
049            
050            // Add the pattern definition 
051            if (patternValue != null)
052            {
053                Pattern pattern = Pattern.compile(patternValue);
054                patterns.add(pattern);
055            }
056        }
057        
058        _extensions.put(id, patterns);
059    }
060
061    public void initializeExtensions() throws Exception
062    {
063           // Empty
064    }
065
066    public boolean hasExtension(String id)
067    {
068        return _extensions.containsKey(id);
069    }
070
071    public Collection<Pattern> getExtension(String id)
072    {
073        return _extensions.get(id);
074    }
075
076    public Set<String> getExtensionsIds()
077    {
078        return _extensions.keySet();
079    }
080    
081    /**
082     * Get all excluded paths
083     * @return an array of the pattern to exclude
084     */
085    public Pattern[] getExcludedPaths()
086    {
087        return _extensions.values().stream().flatMap(Collection::stream).toArray(Pattern[]::new);
088    }
089}