All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.qatools.allure.data.plugins.DefaultPluginLoader.groovy Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package ru.yandex.qatools.allure.data.plugins

import com.google.inject.Inject
import com.google.inject.Injector
import com.google.inject.Singleton
import ru.yandex.qatools.allure.data.utils.ServiceLoaderUtils

/**
 * @author Dmitry Baev [email protected]
 *         Date: 06.10.15
 */
@Singleton
class DefaultPluginLoader implements PluginLoader {

    private final ClassLoader pluginClassLoader

    private final Injector injector

    /**
     * Creates an instance of plugin loader.
     */
    @Inject
    DefaultPluginLoader(@PluginClassLoader ClassLoader pluginClassLoader, Injector injector) {
        this.pluginClassLoader = pluginClassLoader
        this.injector = injector
    }

    /**
     * Load all plugins using via Java SPI then remove all null plugins
     * and finally inject members to each plugin in case not null injector.
     */
    @Override
    List loadPlugins() {
        def result = [] as List
        def loaded = ServiceLoaderUtils.load(pluginClassLoader, Plugin.class) ?: [] as List

        loaded.each {
            if (isValidPlugin(it)) {
                injector?.injectMembers(it)
                result.add(it)
            }
        }
        result
    }

    /**
     * Some checks for plugins.
     * @see AbstractPlugin#isValid(java.lang.Class)
     */
    protected static boolean isValidPlugin(Plugin plugin) {
        return plugin && (plugin instanceof AbstractPlugin ?
                AbstractPlugin.isValid(plugin.class) : true)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy