Example usage for java.util ServiceLoader load

List of usage examples for java.util ServiceLoader load

Introduction

In this page you can find the example usage for java.util ServiceLoader load.

Prototype

@CallerSensitive
public static <S> ServiceLoader<S> load(Class<S> service) 

Source Link

Document

Creates a new service loader for the given service type, using the current thread's java.lang.Thread#getContextClassLoader context class loader .

Usage

From source file:com.googlecode.fascinator.api.PluginManager.java

/**
 * Gets a storage plugin//  w  ww .  j  av  a 2s. c  o m
 *
 * @param id plugin identifier
 * @return a storage plugin, or null if not found
 */
public static Storage getStorage(String id) {
    Class<Storage> clazz = (Class<Storage>) cache.getIfPresent("storage" + id);
    if (clazz != null) {
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            // Throwing RuntimeException to keep it unchecked
            // TODO: Remove later
            throw new RuntimeException(e);
        }
    }
    ServiceLoader<Storage> plugins = ServiceLoader.load(Storage.class);
    for (Storage plugin : plugins) {
        if (id.equals(plugin.getId())) {
            return plugin;
        }
    }

    File groovyFile = getPathFile("plugins/storage/" + id + ".groovy");
    if (groovyFile.exists()) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        try {
            clazz = gcl.parseClass(groovyFile);
            cache.put("storage" + id, clazz);
            return clazz.newInstance();

        } catch (Exception e) {
            // Throwing RuntimeException to keep it unchecked
            // TODO: Remove later
            throw new RuntimeException(e);
        }

    }
    return null;
}

From source file:com.fitbur.testify.system.SpringSystemTest.java

public ClientContext getClientContext(TestContext testContext, ServerContext serverContext) {
    return testContext.getAnnotation(App.class).map(app -> {
        SpringSystemClientDescriptor descriptor = new SpringSystemClientDescriptor(app, testContext,
                serverContext.getInstance().getURI());

        ServiceLoader<ClientProvider> clientProviderLoader = ServiceLoader.load(ClientProvider.class);
        ArrayList<ClientProvider> clientProviders = Lists.newArrayList(clientProviderLoader);

        checkState(!clientProviders.isEmpty(),
                "ClientProvider provider implementation not found in the classpath");

        checkState(clientProviders.size() == 1,
                "Multiple ClientProvider provider implementations found in the classpath. "
                        + "Please insure there is only one ClientProvider provider implementations "
                        + "in the classpath.");

        ClientProvider clientProvider = clientProviders.get(0);
        Object configuration = clientProvider.configuration(descriptor);

        testContext.getConfigMethod(configuration.getClass()).map(m -> m.getMethod()).ifPresent(m -> {
            AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                try {
                    m.setAccessible(true);
                    m.invoke(testContext.getTestInstance(), configuration);
                } catch (Exception e) {
                    checkState(false, "Call to config method '%s' in test class '%s' failed due to: ",
                            m.getName(), descriptor.getTestClassName(), e.getMessage());
                }/*from w  w  w  .  ja  v  a2  s  .  c o m*/

                return null;
            });
        });

        ClientInstance instance = clientProvider.init(descriptor, configuration);
        ServiceLocator serviceLocator = serverContext.getLocator();
        ClientContext context = new ClientContext(clientProvider, descriptor, instance, configuration);

        serviceLocator.addConstant(context.getClass().getSimpleName(), context);
        serviceLocator.addConstant(instance.getClass().getSimpleName(), instance);

        return context;

    }).get();
}

From source file:org.apache.taverna.scufl2.translator.t2flow.T2FlowParser.java

public synchronized Set<T2Parser> getT2Parsers() {
    Set<T2Parser> parsers = t2Parsers;
    if (parsers != null)
        return parsers;
    parsers = new HashSet<>();
    /*/*from www .  j a  va2  s .  com*/
     * TODO: Do we need to cache this, or is the cache in ServiceLoader fast
     * enough?
     */
    if (discoveredT2Parsers == null)
        discoveredT2Parsers = ServiceLoader.load(T2Parser.class);
    for (T2Parser parser : discoveredT2Parsers)
        parsers.add(parser);
    return parsers;
}

From source file:com.googlecode.fascinator.api.PluginManager.java

/**
 * Get a list of storage plugins//from   w w w.j a  v a2 s.c  o m
 *
 * @return map of storage plugins, or empty map if not found
 */
public static Map<String, Storage> getStoragePlugins() {
    Map<String, Storage> storageMap = new HashMap<String, Storage>();
    ServiceLoader<Storage> plugins = ServiceLoader.load(Storage.class);
    for (Storage plugin : plugins) {
        storageMap.put(plugin.getId(), plugin);
    }
    return storageMap;
}

From source file:edu.mayo.cts2.framework.core.plugin.FelixPluginManager.java

protected void initializeNonOsgiPlugins() {
    ServiceLoader<NonOsgiPluginInitializer> serviceLoader = ServiceLoader.load(NonOsgiPluginInitializer.class);

    Iterator<NonOsgiPluginInitializer> itr = serviceLoader.iterator();
    while (itr.hasNext()) {
        NonOsgiPluginInitializer pluginInitializer = itr.next();
        pluginInitializer.initialize(this);
        this.nonOsgiPlugins.add(pluginInitializer);
    }/*  w  w w . j a v a  2 s  . com*/
}

From source file:com.googlecode.fascinator.api.PluginManager.java

/**
 * Gets a TransactionManager plugin/*from w  w  w. ja va  2 s .co  m*/
 *
 * @param id Plugin identifier
 * @return TransactionManager Instantiated plugin, or null if not found
 */
public static TransactionManager getTransactionManager(String id) throws TransactionException {
    Class<TransactionManager> clazz = (Class<TransactionManager>) cache.getIfPresent("transactionManager" + id);
    if (clazz != null) {
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            // Throwing RuntimeException to keep it unchecked
            // TODO: Remove later
            throw new RuntimeException(e);
        }
    }
    ServiceLoader<TransactionManager> plugins = ServiceLoader.load(TransactionManager.class);
    for (TransactionManager plugin : plugins) {
        if (id.equals(plugin.getId())) {
            return plugin;
        }
    }

    File groovyFile = getPathFile("plugins/transactionManager/" + id + ".groovy");
    if (groovyFile.exists()) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        try {
            clazz = gcl.parseClass(groovyFile);
            cache.put("transactionManager" + id, clazz);
            return clazz.newInstance();

        } catch (Exception e) {
            // Throwing RuntimeException to keep it unchecked
            // TODO: Remove later
            throw new RuntimeException(e);
        }

    }

    return null;
}

From source file:com.jaxio.celerio.factory.ProjectFactory.java

private void loadAndApplySpis() {
    // 1- project SPI
    loadProjectSpis(defaultProjectSpis.iterator());
    loadProjectSpis(ServiceLoader.load(ProjectSpi.class).iterator());

    // 2- entity SPI
    loadEntitySpis(defaultEntitySpis.iterator());
    loadEntitySpis(ServiceLoader.load(EntitySpi.class).iterator());

    // 3- attribute SPI
    loadAttributeSpis(defaultAttributeSpis.iterator());
    loadAttributeSpis(ServiceLoader.load(AttributeSpi.class).iterator());

    // 4- relation SPI
    loadRelationSpis(defaultRelationSpis.iterator());
    loadRelationSpis(ServiceLoader.load(RelationSpi.class).iterator());

    // 5- real binding on project, entities, attributes, relations.
    bindAllSpis();/*from  www .j  a v a  2 s.c  o  m*/

    // 6- now bind all namers found in various pack config
    // Note: namers found in main config are loaded/binded by the entityFactory.
    for (TemplatePack templatePack : packLoader.getTemplatePacks()) {

        List<EntityContextProperty> entityContextPropertyList = templatePack.getTemplatePackInfo()
                .getEntityContextPropertyList();
        if (entityContextPropertyList == null || entityContextPropertyList.size() == 0) {
            continue;
        }

        for (Entity entity : config.getProject().getCurrentEntities()) {
            for (EntityContextProperty ecp : entityContextPropertyList) {
                entity.put(ecp.getProperty(), new ClassNamer2(entity, ecp.getRootPackage(), ecp.getSubPackage(),
                        ecp.getPrefix(), ecp.getSuffix()));
            }
        }
    }
}

From source file:com.googlecode.fascinator.api.PluginManager.java

/**
 * Get a list of TransactionManager plugins
 *
 * @return Map of TransactionManager plugins and their IDs, or empty map if
 *         not found//from w w w.j av  a  2s. c o  m
 */
public static Map<String, TransactionManager> getTransactionManagerPlugins() {

    Map<String, TransactionManager> managers = new HashMap<String, TransactionManager>();
    ServiceLoader<TransactionManager> plugins = ServiceLoader.load(TransactionManager.class);
    for (TransactionManager plugin : plugins) {
        managers.put(plugin.getId(), plugin);
    }
    return managers;
}

From source file:com.googlecode.fascinator.api.PluginManager.java

/**
 * Gets a transformer plugin//from  ww w .j a va2  s .  c  o m
 *
 * @param id plugin identifier
 * @return a transformer plugin, or null if not found
 */
public static Transformer getTransformer(String id) throws TransformerException {
    Class<Transformer> clazz = (Class<Transformer>) cache.getIfPresent("transformer" + id);
    if (clazz != null) {
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            // Throwing RuntimeException to keep it unchecked
            // TODO: Remove later
            throw new RuntimeException(e);
        }
    }

    ServiceLoader<Transformer> plugins = ServiceLoader.load(Transformer.class);
    for (Transformer plugin : plugins) {
        if (id.equals(plugin.getId())) {
            return plugin;
        }
    }

    File groovyFile = getPathFile("plugins/transformer/" + id + ".groovy");
    if (groovyFile.exists()) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        try {
            clazz = gcl.parseClass(groovyFile);
            cache.put("transformer" + id, clazz);
            return clazz.newInstance();

        } catch (Exception e) {
            // Throwing RuntimeException to keep it unchecked
            // TODO: Remove later
            throw new RuntimeException(e);
        }

    }

    return null;
}

From source file:org.opencms.ui.apps.CmsWorkplaceAppManager.java

/**
 * Returns the configured apps using the service loader.<p>
 *
 * @return tthe configured apps//from  w  ww .  j a v a 2 s .c  o  m
 */
private List<I_CmsWorkplaceAppConfiguration> loadAppsUsingServiceLoader() {

    List<I_CmsWorkplaceAppConfiguration> appConfigurations = new ArrayList<I_CmsWorkplaceAppConfiguration>();
    Iterator<I_CmsWorkplaceAppConfiguration> configs = ServiceLoader.load(I_CmsWorkplaceAppConfiguration.class)
            .iterator();
    while (configs.hasNext()) {
        try {
            I_CmsWorkplaceAppConfiguration config = configs.next();
            appConfigurations.add(config);
        } catch (Throwable t) {
            LOG.error("Error loading workplace app configuration from classpath.", t);
        }
    }
    return appConfigurations;
}