001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.util;
020
021import java.lang.reflect.Array;
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.ServiceLoader;
028import java.util.ServiceLoader.Provider;
029import java.util.function.Function;
030import java.util.function.IntFunction;
031import java.util.stream.Collectors;
032import org.jgrapes.core.Channel;
033import org.jgrapes.core.ComponentFactory;
034import org.jgrapes.util.events.ConfigurationUpdate;
035
036/**
037 * A component that collects all component factory services of 
038 * a given type and uses each to create one or more components
039 * that are then attached to the component collector instance.
040 * 
041 * Effectively, the component collector leverages the 
042 * mechanism provided by the service loader to configure
043 * the component subtree rooted at the collector at "link time".
044 * 
045 * This class uses {@link ComponentProvider#setFactories(ComponentFactory...)} 
046 * and {@link ComponentProvider#setPinned(List)} for its implementation.
047 * As it inherits from {@link ComponentProvider}, it automatically
048 * supports the provisioning of additional components through
049 * {@link ConfigurationUpdate} events. If this is not desired, invoke
050 * {@link ComponentProvider#setComponentsEntry(String)} with `null` as
051 * argument. 
052 * 
053 * @param <F> the component factory type
054 */
055public class ComponentCollector<F extends ComponentFactory>
056        extends ComponentProvider {
057
058    private static final List<Map<Object, Object>> SINGLE_DEFAULT
059        = List.of(Collections.emptyMap());
060
061    /**
062     * Creates a new collector that collects the factories of the given 
063     * type and uses each to create one or more instances with this 
064     * component's (the component collector's) channel. 
065     * 
066     * Before instances are created, the `matcher` function is 
067     * invoked with the name of the class of the component
068     * to be created as argument. The list of maps returned is
069     * used to create components, passing each element in the list
070     * as parameter to {@link ComponentFactory#create(Channel, Map)}.
071     * 
072     * @param factoryClass the factory class
073     * @param componentChannel this component's channel
074     * @param configurator the configurator function
075     */
076    public ComponentCollector(Class<F> factoryClass, Channel componentChannel,
077            Function<String, List<Map<Object, Object>>> configurator) {
078        super(componentChannel);
079        ServiceLoader<F> serviceLoader = ServiceLoader.load(factoryClass);
080
081        // Get all factories
082        IntFunction<F[]> createFactoryArray = size -> {
083            @SuppressWarnings("unchecked")
084            var res = (F[]) Array.newInstance(factoryClass, size);
085            return res;
086        };
087        var factories = serviceLoader.stream().map(Provider::get)
088            .toArray(createFactoryArray);
089        setFactories(factories);
090
091        // Obtain a configuration for each factory
092        List<Map<?, ?>> configs = Arrays.stream(factories)
093            .map(factory -> configurator
094                .apply(factory.componentType().getName()).stream().map(c -> {
095                    if (c.containsKey(COMPONENT_TYPE)) {
096                        return c;
097                    }
098                    // The map may be immutable, copy.
099                    @SuppressWarnings("PMD.UseConcurrentHashMap")
100                    Map<Object, Object> newMap = new HashMap<>(c);
101                    newMap.put(COMPONENT_TYPE,
102                        factory.componentType().getName());
103                    return newMap;
104                }))
105            .flatMap(Function.identity()).collect(Collectors.toList());
106        setPinned(configs);
107    }
108
109    /**
110     * Utility constructor that uses each factory to create a single
111     * instance, using an empty map as properties.
112     * 
113     * @param factoryClass the factory class
114     * @param componentChannel this component's channel
115     */
116    public ComponentCollector(Class<F> factoryClass, Channel componentChannel) {
117        this(factoryClass, componentChannel, type -> SINGLE_DEFAULT);
118    }
119}