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    void wireDeferredServiceableComponents() throws Exception
044    {
045        // Do nothing here: deferred extensions are handled by the extension point itself
046    }
047    
048    void wireDeferredServiceableExtensions() throws Exception
049    {
050        // Called after all components initializations by the main PluginComponentManager 
051        super.wireDeferredServiceableComponents();
052    }
053    
054    @Override
055    ComponentFactory getComponentFactory(String pluginName, String featureName, String role, Class<? extends T> componentClass, Configuration configuration)
056    {
057        return new ExtensionFactory(pluginName, featureName, role, componentClass, configuration, _manager, getLogger());
058    }
059    
060    class ExtensionFactory extends ComponentFactory
061    {
062        ExtensionFactory(String pluginName, String featureName, String role, Class<? extends T> componentClass, Configuration configuration, ServiceManager serviceManager, Logger logger)
063        {
064            super(pluginName, featureName, role, componentClass, configuration, serviceManager, logger);
065        }
066        
067        @Override
068        T instanciate() throws Exception
069        {
070            T object = super.instanciate();
071            
072            if (object instanceof ExtensionPointAware)
073            {
074                ((ExtensionPointAware) object).setExtensionPoint(_point);
075            }
076            
077            return object;
078        }
079    }
080}