Example usage for java.util ServiceLoader iterator

List of usage examples for java.util ServiceLoader iterator

Introduction

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

Prototype

public Iterator<S> iterator() 

Source Link

Document

Returns an iterator to lazily load and instantiate the available providers of this loader's service.

Usage

From source file:org.ejbca.ui.web.admin.cainterface.CAInterfaceBean.java

/** Returns true if any CVC CA implementation is available, false otherwise.
 * Used to hide/give warning when no CVC CA implementation is available.
 */// w  w w  .j  a  v  a  2  s .  c o m
public boolean isCVCAvailable() {
    boolean ret = false;
    ServiceLoader<? extends CvcPlugin> loader = CvcCA.getImplementationClasses();
    if (loader.iterator().hasNext()) {
        ret = true;
    }
    return ret;
}

From source file:org.kie.server.controller.websocket.management.KieServerMgmtCommandServiceImpl.java

private KieServerMgmtCommandServiceImpl() {
    ServiceLoader<PersistingServerTemplateStorageService> storageServices = ServiceLoader
            .load(PersistingServerTemplateStorageService.class);
    if (storageServices != null && storageServices.iterator().hasNext()) {
        PersistingServerTemplateStorageService storageService = storageServices.iterator().next();
        setTemplateStorage(storageService.getTemplateStorage());
    } else {/*from w ww .  j  a  v  a  2s  .c om*/
        LOGGER.debug(
                "No server template storage defined. Default storage: InMemoryKieServerTemplateStorage will be used");
    }

    ServiceLoader<NotificationServiceFactory> notificationServiceLoader = ServiceLoader
            .load(NotificationServiceFactory.class);
    if (notificationServiceLoader != null && notificationServiceLoader.iterator().hasNext()) {
        final NotificationService notificationService = notificationServiceLoader.iterator().next()
                .getNotificationService();
        setNotificationService(notificationService);
    } else {
        LOGGER.warn(
                "Notification service not defined. Default notification: LoggingNotificationService will be used");
    }
}

From source file:org.kitodo.serviceloader.KitodoServiceLoader.java

/**
 * Loads a module from the classpath which implements the constructed clazz.
 * Frontend files of all modules will be loaded into the core module.
 *
 * @return A module with type T.// ww  w. ja v a2  s.c  om
 */
@SuppressWarnings("unchecked")
public T loadModule() {

    loadModulesIntoClasspath();
    loadBeans();
    loadFrontendFilesIntoCore();

    ServiceLoader<T> loader = ServiceLoader.load(clazz);

    return loader.iterator().next();
}

From source file:org.lable.oss.dynamicconfig.di.DynamicConfigModule.java

@Provides
@Singleton//  w ww .j  a v  a  2 s. co m
public HierarchicalConfigurationDeserializer provideDeserializer() throws ConfigurationException {
    ServiceLoader<HierarchicalConfigurationDeserializer> serviceLoader = ServiceLoader
            .load(HierarchicalConfigurationDeserializer.class);

    Iterator<HierarchicalConfigurationDeserializer> iterator = serviceLoader.iterator();
    if (!iterator.hasNext()) {
        throw new ConfigurationException("No HierarchicalConfigurationDeserializer found on the classpath. "
                + "You may need to load a module containing a suitable deserializer.");
    }

    HierarchicalConfigurationDeserializer hierarchicalConfigurationDeserializer = iterator.next();
    logger.info("Found HierarchicalConfigurationDeserializer {}; providing it to the configuration system.",
            hierarchicalConfigurationDeserializer.getClass().getName());
    return hierarchicalConfigurationDeserializer;
}

From source file:org.openflexo.foundation.sg.implmodel.TechnologyModuleDefinition.java

/**
 * Retrieve all {@link TechnologyModuleDefinition} available from classpath. <br>
 * Map contains the TechnologyModuleDefinition name as key and the TechnologyModuleDefinition itself as value.
 * /*www  .  ja va  2s .c o m*/
 * @return the retrieved TechnologyModuleDefinition map.
 */
private static Map<String, TechnologyModuleDefinition> getTechnologyModuleDefinitionMap() {
    if (technologyModuleDefinitionMap == null) {
        technologyModuleDefinitionMap = new Hashtable<String, TechnologyModuleDefinition>();

        ServiceLoader<TechnologyModuleDefinition> loader = ServiceLoader.load(TechnologyModuleDefinition.class);
        Iterator<TechnologyModuleDefinition> iterator = loader.iterator();
        while (iterator.hasNext()) {
            TechnologyModuleDefinition technologyModuleDefinition = iterator.next();

            if (technologyModuleDefinitionMap.containsKey(technologyModuleDefinition.getName())) {
                logger.severe("Cannot include TechnologyModuleDefinition with name '"
                        + technologyModuleDefinition.getName()
                        + "' because it already exists !!!! A Technology module name MUST be unique !");
            } else {
                technologyModuleDefinitionMap.put(technologyModuleDefinition.getName(),
                        technologyModuleDefinition);
            }
        }
    }

    return technologyModuleDefinitionMap;
}

From source file:org.opoo.press.impl.FactoryImpl.java

public <T> T instantiate(Class<T> clazz) {
    ServiceLoader<T> loader = ServiceLoader.load(clazz, site.getClassLoader());
    Iterator<T> iterator = loader.iterator();
    if (iterator.hasNext()) {
        return apply(iterator.next());
    }//from ww  w.j a  v  a  2s.  c o m

    String className = (String) configuration.get(clazz.getName());
    if (StringUtils.isBlank(className) || "none".equalsIgnoreCase(className)) {
        return null;
    }
    return newInstance(className);
}

From source file:org.ops4j.pax.exam.forked.ForkedFrameworkFactoryTest.java

@Test
public void forkEquinox()
        throws BundleException, IOException, InterruptedException, NotBoundException, URISyntaxException {
    ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class);
    FrameworkFactory frameworkFactory = loader.iterator().next();

    ForkedFrameworkFactory forkedFactory = new ForkedFrameworkFactory(frameworkFactory);

    Map<String, Object> frameworkProperties = new HashMap<String, Object>();
    frameworkProperties.put(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());
    RemoteFramework framework = forkedFactory.fork(Collections.<String>emptyList(),
            Collections.<String, String>emptyMap(), frameworkProperties);
    framework.start();/*from  w  w w .j  a v a  2 s .c  o  m*/

    long bundleId = framework.installBundle("file:target/bundles/regression-pde-bundle-2.3.0.jar");
    framework.startBundle(bundleId);

    framework.callService("(objectClass=org.ops4j.pax.exam.regression.pde.HelloService)", "getMessage");

    Thread.sleep(3000);
    framework.stop();

    forkedFactory.join();
}

From source file:org.ops4j.pax.exam.forked.ForkedFrameworkFactoryTest.java

@Test
public void forkWithBootClasspath()
        throws BundleException, IOException, InterruptedException, NotBoundException, URISyntaxException {
    ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class);
    FrameworkFactory frameworkFactory = loader.iterator().next();

    ForkedFrameworkFactory forkedFactory = new ForkedFrameworkFactory(frameworkFactory);

    List<String> bootClasspath = Arrays
            .asList(new File("target/bundles/metainf-services.jar").getCanonicalPath());

    Map<String, Object> frameworkProperties = new HashMap<String, Object>();
    frameworkProperties.put(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());
    frameworkProperties.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.kohsuke.metainf_services");
    RemoteFramework framework = forkedFactory.fork(Collections.<String>emptyList(),
            Collections.<String, String>emptyMap(), frameworkProperties, null, bootClasspath);
    framework.start();//from w ww  .j  a va  2s. c o m

    File testBundle = generateBundle();
    long bundleId = framework.installBundle("file:" + testBundle.getAbsolutePath());
    framework.startBundle(bundleId);

    // START>>> not yet implemented
    // framework.waitForState(bundleId, Bundle.ACTIVE, 1500);
    Thread.sleep(3000);
    // <<<END not yet implemented

    framework.stop();

    forkedFactory.join();
}

From source file:org.ops4j.pax.exam.forked.ForkedFrameworkFactoryTest.java

@Test(expected = TestContainerException.class)
public void forkWithInvalidBootClasspath()
        throws BundleException, IOException, InterruptedException, NotBoundException, URISyntaxException {
    ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class);
    FrameworkFactory frameworkFactory = loader.iterator().next();

    ForkedFrameworkFactory forkedFactory = new ForkedFrameworkFactory(frameworkFactory);

    List<String> bootClasspath = Arrays
            .asList(CoreOptions.maven("org.kohsuke.metainf-services", "metainf-services", "1.2").getURL());

    Map<String, Object> frameworkProperties = new HashMap<String, Object>();
    frameworkProperties.put(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());
    frameworkProperties.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.kohsuke.metainf_services");
    forkedFactory.fork(Collections.<String>emptyList(), Collections.<String, String>emptyMap(),
            frameworkProperties, null, bootClasspath);
}

From source file:org.parallelj.launching.transport.tcp.command.AbstractLaunchTcpCommand.java

public List<IOption> getOptions() {
    final Class<? extends IOption> iOptionClass = this.getOptionClass();
    if (iOptionClass != null) {
        if (this.options == null) {
            this.options = new ArrayList<IOption>();
            ServiceLoader<? extends IOption> loader = CacheableServiceLoader.INSTANCE.load(iOptionClass,
                    AbstractLaunchTcpCommand.class.getClassLoader());
            if (loader == null || loader.iterator() == null || !loader.iterator().hasNext()) {
                loader = CacheableServiceLoader.INSTANCE.load(iOptionClass,
                        Thread.currentThread().getContextClassLoader());
            }//from  w  w  w . j a  v  a 2s. c  om
            for (IOption option : loader) {
                this.options.add(option);
            }
        }
    }

    // Sort the list of IOption.
    // The first IOption should be "id" as it is the only once mandatory
    final Comparator<IOption> comparator = new Comparator<IOption>() {
        public int compare(final IOption option1, final IOption option2) {
            if (option1.getOption() != null && option1.getOption().isRequired()) {
                return -1;
            }
            if (option2.getOption() != null && option2.getOption().isRequired()) {
                return 1;
            }
            return 0;
        }
    };
    Collections.sort(this.options, comparator);

    return this.options;
}