Java Code Examples for java.util.ServiceLoader#load()

The following examples show how to use java.util.ServiceLoader#load() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceInputOutputFactory.java    From nordpos with GNU General Public License v3.0 6 votes vote down vote up
public static DeviceInputOutput createInstance(AppProperties props) {

        ServiceLoader<InputOutputInterface> pluLoader = ServiceLoader.load(InputOutputInterface.class);

        DeviceInputOutput m_plu = new DeviceInputOutputNull();

        for (InputOutputInterface machineInterface : pluLoader) {
            try {
                m_plu = machineInterface.getDeviceIO(props.getProperty("machine.pludevice"));
            } catch (Exception ex) {
                logger.log(Level.WARNING, ex.getMessage(), ex);
            }
        }

        return m_plu;
    }
 
Example 2
Source File: OASFactoryResolver.java    From microprofile-open-api with Apache License 2.0 6 votes vote down vote up
private static OASFactoryResolver loadSpi(ClassLoader cl) {
    if (cl == null) {
        return null;
    }

    OASFactoryResolver instance = loadSpi(cl.getParent());

    if (instance == null) {
        ServiceLoader<OASFactoryResolver> sl = ServiceLoader.load(OASFactoryResolver.class, cl);
        for (OASFactoryResolver spi : sl) {
            if (instance != null) {
                throw new IllegalStateException("Multiple OASFactoryResolver implementations found: " + spi.getClass().getName() + " and "
                        + instance.getClass().getName());
            }
            else {
                instance = spi;
            }
        }
    }
    return instance;
}
 
Example 3
Source File: AsynchronousChannelProvider.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
Example 4
Source File: KeyStoreUtil.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a security provider as a service.
 *
 * @param provName the name
 * @param arg optional arg
 * @throws IllegalArgumentException if no provider matches the name
 */
public static void loadProviderByName(String provName, String arg) {
    Provider loaded = Security.getProvider(provName);
    if (loaded != null) {
        if (arg != null) {
            loaded = loaded.configure(arg);
            Security.addProvider(loaded);
        }
        return;
    }
    for (Provider p : ServiceLoader.load(Provider.class,
            ClassLoader.getSystemClassLoader())) {
        if (p.getName().equals(provName)) {
            if (arg != null) {
                p = p.configure(arg);
            }
            Security.addProvider(p);
            return;
        }
    }
    throw new IllegalArgumentException("No provider found");
}
 
Example 5
Source File: RasterIngestRunner.java    From geowave with Apache License 2.0 5 votes vote down vote up
private synchronized Map<String, Landsat8BandConverterSpi> getRegisteredConverters() {
  if (registeredBandConverters == null) {
    registeredBandConverters = new HashMap<>();
    final ServiceLoader<Landsat8BandConverterSpi> converters =
        ServiceLoader.load(Landsat8BandConverterSpi.class);
    final Iterator<Landsat8BandConverterSpi> it = converters.iterator();
    while (it.hasNext()) {
      final Landsat8BandConverterSpi converter = it.next();
      registeredBandConverters.put(converter.getName(), converter);
    }
  }
  return registeredBandConverters;
}
 
Example 6
Source File: HadoopS3FileSystemsSchemesTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static void testFactory(String scheme) {
	ServiceLoader<FileSystemFactory> serviceLoader = ServiceLoader.load(FileSystemFactory.class);
	for (FileSystemFactory fs : serviceLoader) {
		if (scheme.equals(fs.getScheme())) {
			// found the matching scheme
			return;
		}
	}

	fail("No factory available for scheme " + scheme);
}
 
Example 7
Source File: AbstractChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the available chronologies.
 * <p>
 * Each returned {@code Chronology} is available for use in the system.
 * The set of chronologies includes the system chronologies and
 * any chronologies provided by the application via ServiceLoader
 * configuration.
 *
 * @return the independent, modifiable set of the available chronology IDs, not null
 */
static Set<Chronology> getAvailableChronologies() {
    initCache();       // force initialization
    HashSet<Chronology> chronos = new HashSet<>(CHRONOS_BY_ID.values());

    /// Add in Chronologies from the ServiceLoader configuration
    @SuppressWarnings("rawtypes")
    ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
    for (Chronology chrono : loader) {
        chronos.add(chrono);
    }
    return chronos;
}
 
Example 8
Source File: DefaultServiceDiscoveryFactory.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Override
public ServiceDiscovery getInstance() {
	ServiceLoader<ServiceDiscovery> loader = ServiceLoader.load(ServiceDiscovery.class);
	Iterator<ServiceDiscovery> iterator = loader.iterator();
	if (iterator.hasNext()) {
		ServiceDiscovery discovery = iterator.next();
		PreconditionUtil.assertFalse("expected unique ServiceDiscovery implementation, got: " + loader, iterator.hasNext());
		return discovery;
	}
	return null;
}
 
Example 9
Source File: AuthorizationFactoryService.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public static AuthorizationFactory newAuthorizationFactory(){
    ServiceLoader<AuthorizationFactory> factoryService = ServiceLoader.load(AuthorizationFactory.class);
    Iterator<AuthorizationFactory> iter = factoryService.iterator();
    if(!iter.hasNext())
        throw new IllegalStateException("No AuthorizationFactory service found!");
    AuthorizationFactory af = null;
    AuthorizationFactory currentAF = null;
    while (iter.hasNext()) {
        currentAF = (AuthorizationFactory) iter.next();
        if (af == null || af.getPriority() < currentAF.getPriority())
            af = currentAF;
    }
    return af;
}
 
Example 10
Source File: AbstractAnnotationHandler.java    From minnal with Apache License 2.0 5 votes vote down vote up
public synchronized static AbstractAnnotationHandler handlerFor(Annotation annotation) {
	if (handlers == null) {
		 handlers = new HashMap<Class<?>, AbstractAnnotationHandler>();
		 ServiceLoader<AbstractAnnotationHandler> loader = ServiceLoader.load(AbstractAnnotationHandler.class);
		 for (AbstractAnnotationHandler handler : loader) {
			 handlers.put(handler.getAnnotationType(), handler);
		 }
	}
	return handlers.get(annotation.annotationType());
}
 
Example 11
Source File: BookService.java    From Java-9-Cookbook with MIT License 5 votes vote down vote up
public static BookService getInstance(){
	ServiceLoader<BookServiceProvider> sl
           = ServiceLoader.load(BookServiceProvider.class);
       Iterator<BookServiceProvider> iter = sl.iterator();
       if (!iter.hasNext())
           throw new RuntimeException("No service providers found!");
       BookServiceProvider provider = null;
       while(iter.hasNext()){
       	provider = iter.next();
       	System.out.println(provider.getClass());
       }
       return provider.getBookService();
}
 
Example 12
Source File: CounterServiceAsyncClient.java    From dapeng-soa with Apache License 2.0 5 votes vote down vote up
public CounterServiceAsyncClient() {
  this.serviceName = "com.github.dapeng.basic.api.counter.service.CounterService";
  this.version = "1.0.0";

  ServiceLoader<SoaConnectionPoolFactory> factories = ServiceLoader.load(SoaConnectionPoolFactory.class,getClass().getClassLoader());
  this.pool = factories.iterator().next().getPool();
  this.clientInfo = this.pool.registerClientInfo(serviceName,version);
}
 
Example 13
Source File: FlowHelper.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param flowName
 * @return <br>
 */
private static FlowConfig match(final String flowName) {
    if (serviceLoader == null) {
        serviceLoader = ServiceLoader.load(FlowLoader.class);
    }

    FlowConfig flowConfig = null;
    for (FlowLoader flowLoader : serviceLoader) {
        flowConfig = flowLoader.load(flowName);
        if (flowConfig != null) {
            break;
        }
    }
    return flowConfig;
}
 
Example 14
Source File: CloudObjects.java    From beam with Apache License 2.0 5 votes vote down vote up
private static Map<String, CloudObjectTranslator<? extends Coder>>
    populateCloudObjectTranslators() {
  ImmutableMap.Builder<String, CloudObjectTranslator<? extends Coder>> builder =
      ImmutableMap.builder();
  for (CoderCloudObjectTranslatorRegistrar coderRegistrar :
      ServiceLoader.load(CoderCloudObjectTranslatorRegistrar.class)) {
    builder.putAll(coderRegistrar.classNamesToTranslators());
  }
  return builder.build();
}
 
Example 15
Source File: ProcessBuildData.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private static List<ProcessDataEventListenerProvider> collectProviders() {
    ServiceLoader<ProcessDataEventListenerProvider> availableProviders = ServiceLoader.load(ProcessDataEventListenerProvider.class);
    List<ProcessDataEventListenerProvider> collected = new ArrayList<ProcessDataEventListenerProvider>();
    try {
        for (ProcessDataEventListenerProvider provider : availableProviders) {
            collected.add(provider);
        }
    } catch (Throwable e) {
        logger.debug("Unable to collect process data event listeners due to {}", e.getMessage());
    }
    return collected;
}
 
Example 16
Source File: JavaSPITest.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Test
public void sayHello() {
    ServiceLoader<Robot> serviceLoader = ServiceLoader.load(Robot.class);
    System.out.println("Java SPI");
    serviceLoader.forEach(Robot::sayHello);
}
 
Example 17
Source File: AdditionalPluginLoader.java    From ramus with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static Iterator loadProviders(Class clazz) {
    ServiceLoader sl = ServiceLoader.load(clazz);
    return sl.iterator();
}
 
Example 18
Source File: ResourceLoader.java    From Time4A with Apache License 2.0 2 votes vote down vote up
@Override
public <S> Iterable<S> services(Class<S> serviceInterface) {

    return ServiceLoader.load(serviceInterface, serviceInterface.getClassLoader());

}
 
Example 19
Source File: NPE.java    From jdk8u60 with GNU General Public License v2.0 votes vote down vote up
void run() { ServiceLoader.load(null); } 
Example 20
Source File: NPE.java    From openjdk-8 with GNU General Public License v2.0 votes vote down vote up
void run() { ServiceLoader.load(null, NPE.class.getClassLoader()); }