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    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
077    public ComponentCollector(Class<F> factoryClass, Channel componentChannel,
078            Function<String, List<Map<Object, Object>>> configurator) {
079        super(componentChannel);
080        ServiceLoader<F> serviceLoader = ServiceLoader.load(factoryClass);
081
082        // Get all factories
083        IntFunction<F[]> createFactoryArray = size -> {
084            @SuppressWarnings("unchecked")
085            var res = (F[]) Array.newInstance(factoryClass, size);
086            return res;
087        };
088        var factories = serviceLoader.stream().map(Provider::get)
089            .toArray(createFactoryArray);
090        setFactories(factories);
091
092        // Obtain a configuration for each factory
093        List<Map<?, ?>> configs = Arrays.stream(factories)
094            .map(factory -> configurator
095                .apply(factory.componentType().getName()).stream().map(c -> {
096                    if (c.containsKey(COMPONENT_TYPE)) {
097                        return c;
098                    }
099                    // The map may be immutable, copy.
100                    @SuppressWarnings("PMD.UseConcurrentHashMap")
101                    Map<Object, Object> newMap = new HashMap<>(c);
102                    newMap.put(COMPONENT_TYPE,
103                        factory.componentType().getName());
104                    return newMap;
105                }))
106            .flatMap(Function.identity()).collect(Collectors.toList());
107        setPinned(configs);
108    }
109
110    /**
111     * Utility constructor that uses each factory to create a single
112     * instance, using an empty map as properties.
113     * 
114     * @param factoryClass the factory class
115     * @param componentChannel this component's channel
116     */
117    public ComponentCollector(Class<F> factoryClass, Channel componentChannel) {
118        this(factoryClass, componentChannel, type -> SINGLE_DEFAULT);
119    }
120}