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:org.key2gym.business.services.ReportsServiceBean.java

@Override
public List<ReportGeneratorDTO> getReportGenerators() throws SecurityViolationException {
    LinkedList<ReportGeneratorDTO> generators = new LinkedList<ReportGeneratorDTO>();

    Iterator<ReportGenerator> it = ServiceLoader.load(ReportGenerator.class).iterator();

    while (it.hasNext()) {
        generators.add(convertToDTO(it.next()));
    }/*from  ww w .  j ava  2  s . c  om*/

    return generators;
}

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

/**
 * Get a list of authentication plugins/*from w ww  . j ava 2 s .co m*/
 *
 * @return map of authentication plugins, or empty map if not found
 */
public static Map<String, Authentication> getAuthenticationPlugins() {
    Map<String, Authentication> authenticators = new HashMap<String, Authentication>();
    ServiceLoader<Authentication> plugins = ServiceLoader.load(Authentication.class);
    for (Authentication plugin : plugins) {
        authenticators.put(plugin.getId(), plugin);
    }
    return authenticators;
}

From source file:com.github.matthesrieke.realty.CrawlerServlet.java

private void initializeCrawlers() {
    ServiceLoader<Crawler> loader = ServiceLoader.load(Crawler.class);

    this.crawlers = new ArrayList<>();
    for (Crawler crawler : loader) {
        this.crawlers.add(crawler);
    }//from  w  w w . j  a v a  2  s.c o  m
}

From source file:org.kie.server.services.prometheus.PrometheusKieServerExtension.java

@Override
public List<Object> getAppComponents(SupportedTransports type) {
    ServiceLoader<KieServerApplicationComponentsService> appComponentsServices = ServiceLoader
            .load(KieServerApplicationComponentsService.class);
    List<Object> appComponentsList = new ArrayList<Object>();
    Object[] services = { context };
    for (KieServerApplicationComponentsService appComponentsService : appComponentsServices) {
        appComponentsList.addAll(appComponentsService.getAppComponents(EXTENSION_NAME, type, services));
    }/*w  ww .  j av a2  s  . c om*/

    return appComponentsList;
}

From source file:org.jboss.set.aphrodite.Aphrodite.java

private void init(AphroditeConfig config) throws AphroditeException {
    if (LOG.isInfoEnabled())
        LOG.info("Initiating Aphrodite ...");

    boolean failed = false;
    StringBuilder error = new StringBuilder();
    this.config = config;

    executorService = config.getExecutorService();
    // Create new config object, as the object passed to init() will have its state changed.
    AphroditeConfig mutableConfig = new AphroditeConfig(config);

    for (IssueTrackerService is : ServiceLoader.load(IssueTrackerService.class)) {
        boolean initialised = is.init(mutableConfig);
        if (initialised) {
            issueTrackers.put(is.getTrackerID(), is);
        } else {// w ww  . ja  v  a 2s  .  c om
            failed = true;
            error.append("Failed to initialize issue tracker: ").append(is.getTrackerID()).append("\n");
        }
    }

    for (RepositoryService rs : ServiceLoader.load(RepositoryService.class)) {
        boolean initialised = rs.init(mutableConfig);
        if (initialised) {
            repositories.add(rs);
        } else {
            failed = true;
            error.append("Failed to initialize repository: ").append(rs.getRepositoryType()).append("\n");
        }
    }

    if (failed)
        throw new AphroditeException("Unable to initiatilise Aphrodite.\n" + error.toString());

    initialiseStreams(mutableConfig);

    int period = config.getStreamServiceUpdateRate();
    int initialDelay = config.getInitialDelay();
    if (period > 0) {
        this.executorService.scheduleAtFixedRate(new UpdateStreamServices(), initialDelay,
                config.getStreamServiceUpdateRate(), TimeUnit.MINUTES);
    }
    if (LOG.isInfoEnabled())
        LOG.info("Aphrodite Initialisation Complete");
}

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

/**
 * Gets a harvester plugin/*www. j  a va2s  . co  m*/
 *
 * @param id plugin identifier
 * @param storage a storage instance
 * @return a harvester plugin, or null if not found
 */
public static Harvester getHarvester(String id, Storage storage) {
    Class<Harvester> clazz = (Class<Harvester>) cache.getIfPresent("harvester" + 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<Harvester> plugins = ServiceLoader.load(Harvester.class);
    for (Harvester plugin : plugins) {
        if (id.equals(plugin.getId())) {
            plugin.setStorage(storage);
            return plugin;
        }
    }

    File groovyFile = getPathFile("plugins/harvester/" + id + ".groovy");
    if (groovyFile.exists()) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        try {
            clazz = gcl.parseClass(groovyFile);
            cache.put("harvester" + 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.googlecode.fascinator.portal.services.impl.PortalSecurityManagerImpl.java

/**
 * Get a SSO Provider from the ServiceLoader
 * //w w w  . ja  va 2  s.  c o m
 * @param id SSO Implementation ID
 * @return SSOInterface implementation matching the ID, if found
 */
private SSOInterface getSSOProvider(String id) {
    ServiceLoader<SSOInterface> providers = ServiceLoader.load(SSOInterface.class);
    for (SSOInterface provider : providers) {
        if (id.equals(provider.getId())) {
            return provider;
        }
    }
    return null;
}

From source file:org.apache.ace.agent.launcher.Launcher.java

/**
 * Load {@link BundleProvider}s through the {@link ServiceLoader}.
 * /* www  .j  a va 2  s.com*/
 * @return list of providers
 * @throws Exception
 *             on failure
 */
private BundleProvider[] loadBundleProviders() throws Exception {
    ServiceLoader<BundleProvider> bundleFactoryLoader = ServiceLoader.load(BundleProvider.class);
    Iterator<BundleProvider> bundleFactoryIterator = bundleFactoryLoader.iterator();
    List<BundleProvider> bundleFactoryList = new ArrayList<>();
    while (bundleFactoryIterator.hasNext()) {
        bundleFactoryList.add(bundleFactoryIterator.next());
    }
    return bundleFactoryList.toArray(new BundleProvider[bundleFactoryList.size()]);
}

From source file:org.wildfly.security.tool.Command.java

protected Supplier<Provider[]> getProvidersSupplier(final String providersList) {
    return () -> {
        if (providersList != null && !providersList.isEmpty()) {
            final String[] providerNames = providersList.split(",");
            List<Provider> providers = new ArrayList<>(providerNames.length);
            for (String p : providerNames) {
                Provider provider = Security.getProvider(p.trim());
                if (provider != null) {
                    providers.add(provider);
                }/*  ww w .ja  v a2  s.  c  o m*/
            }
            ServiceLoader<Provider> providerLoader = ServiceLoader.load(Provider.class);
            for (Provider provider : providerLoader) {
                for (String p : providerNames) {
                    if (provider.getName().equals(p)) {
                        providers.add(provider);
                        break;
                    }
                }
            }
            if (providers.isEmpty()) {
                throw ElytronToolMessages.msg.unknownProvider(providersList);
            }
            return providers.toArray(new Provider[providers.size()]);
        } else {
            // when no provider list is specified, load all Providers from service loader except WildFlyElytron Provider
            ServiceLoader<Provider> providerLoader = ServiceLoader.load(Provider.class);
            Iterator<Provider> providerIterator = providerLoader.iterator();
            List<Provider> providers = new ArrayList<>();
            while (providerIterator.hasNext()) {
                Provider provider = providerIterator.next();
                if (provider.getName().equals("WildFlyElytron"))
                    continue;
                providers.add(provider);
            }
            return providers.toArray(new Provider[providers.size()]);
        }
    };
}

From source file:datascript.tools.DataScriptTool.java

private void printExtensions() {
    ServiceLoader<Extension> loader = ServiceLoader.load(Extension.class);
    Iterator<Extension> it = loader.iterator();
    while (it.hasNext()) {
        Extension ext = it.next();
        System.out.println("Extension: " + ext.getClass().getName());
    }/* w ww  . j a v  a 2s .com*/
}