001/*
002 *  Copyright 2023 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.component;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.slf4j.Logger;
021
022import org.ametys.runtime.plugin.ExtensionPoint;
023
024/**
025 * Component manager used to initialize extensions to extension points subclassing {@link AbstractThreadSafeComponentExtensionPoint}.
026 * @param <T> The type of handled extensions
027 */
028public class ExtensionPointComponentManager<T> extends ThreadSafeComponentManager<T>
029{
030    private ExtensionPoint<T> _point;
031    
032    /**
033     * Constructor.
034     * @param point the parent {@link ExtensionPoint}.
035     */
036    public ExtensionPointComponentManager(ExtensionPoint<T> point)
037    {
038        super(true);
039        _point = point;
040    }
041    
042    @Override
043    ComponentFactory getComponentFactory(String pluginName, String featureName, String role, Class<? extends T> componentClass, Configuration configuration)
044    {
045        return new ExtensionFactory(pluginName, featureName, role, componentClass, configuration, _manager, getLogger());
046    }
047    
048    class ExtensionFactory extends ComponentFactory
049    {
050        ExtensionFactory(String pluginName, String featureName, String role, Class<? extends T> componentClass, Configuration configuration, ServiceManager serviceManager, Logger logger)
051        {
052            super(pluginName, featureName, role, componentClass, configuration, serviceManager, logger);
053        }
054        
055        @Override
056        T instanciate() throws Exception
057        {
058            T object = super.instanciate();
059            
060            if (object instanceof ExtensionPointAware)
061            {
062                ((ExtensionPointAware) object).setExtensionPoint(_point);
063            }
064            
065            return object;
066        }
067    }
068}