001/*
002 *  Copyright 2010 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;
020
021import org.ametys.plugins.repository.jcr.JCRAmetysObjectFactory;
022import org.ametys.runtime.plugin.ExtensionPoint;
023import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
024
025/**
026 * {@link ExtensionPoint} hosting all {@link AmetysObjectFactory}.
027 */
028public class AmetysObjectFactoryExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<AmetysObjectFactory>
029{
030    /** Avalon Role */
031    public static final String ROLE = AmetysObjectFactoryExtensionPoint.class.getName();
032    
033    private Map<String, AmetysObjectFactory> _schemes = new HashMap<>();
034    private Map<String, AmetysObjectFactory> _nodetypes = new HashMap<>();
035    
036    @Override
037    public void initializeExtensions() throws Exception
038    {
039        super.initializeExtensions();
040        
041        for (String id : getExtensionsIds())
042        {
043            AmetysObjectFactory factory = getExtension(id);
044            _schemes.put(factory.getScheme(), factory);
045            
046            if (factory instanceof JCRAmetysObjectFactory)
047            {
048                JCRAmetysObjectFactory<?> jcrFactory = (JCRAmetysObjectFactory) factory;
049                
050                for (String nodetype : jcrFactory.getNodetypes())
051                {
052                    if (_nodetypes.containsKey(nodetype))
053                    {
054                        throw new IllegalArgumentException("Nodetype '" + nodetype + "' cannot be managed by more than one factory.");
055                    }
056                    
057                    _nodetypes.put(nodetype, factory);
058                }
059            }
060        }
061    }
062    
063    /**
064     * Returns the {@link AmetysObjectFactory} corresponding to the given scheme.
065     * @param scheme the scheme.
066     * @return the {@link AmetysObjectFactory} or <code>null</code> if not found.
067     */
068    public AmetysObjectFactory getFactoryForScheme(String scheme)
069    {
070        return _schemes.get(scheme);
071    }
072    
073    /**
074     * Returns the {@link AmetysObjectFactory} corresponding to the given nodetype.
075     * @param nodetype the nodetype.
076     * @return the {@link AmetysObjectFactory} or <code>null</code> if not found.
077     */
078    public AmetysObjectFactory getFactoryForNodetype(String nodetype)
079    {
080        return _nodetypes.get(nodetype);
081    }
082}