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.repository;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.runtime.plugin.ExtensionPoint;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028
029/**
030 * {@link ExtensionPoint} hosting the JCR namespaces.
031 */
032public class NamespacesExtensionPoint extends AbstractLogEnabled implements ExtensionPoint<String>, Component
033{
034    /** Avalon Role */
035    public static final String ROLE = NamespacesExtensionPoint.class.getName();
036    
037    private Map<String, String> _namespaces = new HashMap<>();
038    
039    @Override
040    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
041    {
042        String namespace = configuration.getChild("namespace", true).getValue(null);
043        
044        // Add the nodetype definition 
045        if (namespace != null)
046        {
047            if (_namespaces.containsKey(id))
048            {
049                getLogger().error("The namespace for prefix " + id + " is already defined. It will be ignored");
050            }
051            else
052            {
053                _namespaces.put(id, namespace);
054            }
055        }
056    }
057
058    @Override
059    public void initializeExtensions() throws Exception
060    {
061        // Empty
062    }
063
064    @Override
065    public boolean hasExtension(String id)
066    {
067        return _namespaces.containsKey(id);
068    }
069
070    @Override
071    public String getExtension(String id)
072    {
073        if (hasExtension(id))
074        {
075            return _namespaces.get(id);
076        }
077        
078        return null;
079    }
080
081    @Override
082    public Set<String> getExtensionsIds()
083    {
084        return _namespaces.keySet();
085    }
086    
087    /**
088     * Returns the namespace for the given prefix or null if none.
089     * @param prefix the prefix.
090     * @return the namespace for the given prefix or null if none.
091     */
092    public String getNamespace(String prefix)
093    {
094        return _namespaces.get(prefix);
095    }
096}