/** * Obtains the instance to be used. */ public static InitStrategy get(ClassLoader cl) throws IOException { Iterator<InitStrategy> it = ServiceLoader.load(InitStrategy.class, cl).iterator(); if (!it.hasNext()) { return new InitStrategy(); // default } InitStrategy s = it.next(); LOGGER.log(Level.FINE, "Using {0} as InitStrategy", s); return s; }
private static List<MetricRegistries> getDefinedImplemantations() { ServiceLoader<MetricRegistries> loader = ServiceLoader.load(MetricRegistries.class); List<MetricRegistries> availableFactories = new ArrayList<>(); for (MetricRegistries impl : loader) { availableFactories.add(impl); } return availableFactories; } }
private static List<Externalizer<Object>> loadExternalizers(ClassLoader loader) { List<Externalizer<Object>> loadedExternalizers = new LinkedList<>(); for (Externalizer<Object> externalizer : ServiceLoader.load(Externalizer.class, loader)) { loadedExternalizers.add(externalizer); } Set<DefaultExternalizer> defaultExternalizers = EnumSet.allOf(DefaultExternalizer.class); List<Externalizer<Object>> result = new ArrayList<>(defaultExternalizers.size() + loadedExternalizers.size()); result.addAll(defaultExternalizers); result.addAll(loadedExternalizers); return result; }
public static Monitor loadMonitor() { List<Monitor> monitors = new ArrayList<>(); ServiceLoader.load(MonitorExtension.class) .iterator() .forEachRemaining( extension -> { extension.getMonitor(); extension.initialize(); monitors.add(extension.getMonitor()); }); return monitors.isEmpty() ? new ConsoleMonitor(DEBUG) : new MultiplexMonitor((Monitor[]) monitors.toArray()); }
public static <T> Collection<T> loadFactories(Class<T> clazz, ClassLoader classLoader) { List<T> list = new ArrayList<>(); ServiceLoader<T> factories; if (classLoader != null) { factories = ServiceLoader.load(clazz, classLoader); } else { // this is equivalent to: // ServiceLoader.load(clazz, TCCL); factories = ServiceLoader.load(clazz); } if (factories.iterator().hasNext()) { factories.iterator().forEachRemaining(list::add); return list; } else { // By default ServiceLoader.load uses the TCCL, this may not be enough in environment dealing with // classloaders differently such as OSGi. So we should try to use the classloader having loaded this // class. In OSGi it would be the bundle exposing vert.x and so have access to all its classes. factories = ServiceLoader.load(clazz, ServiceHelper.class.getClassLoader()); if (factories.iterator().hasNext()) { factories.iterator().forEachRemaining(list::add); return list; } else { return Collections.emptyList(); } } } }
private static List<KeyFormat<?>> load(ClassLoader loader) { List<KeyFormat<?>> keyFormats = new LinkedList<>(); for (KeyFormat<?> keyFormat : ServiceLoader.load(KeyFormat.class, loader)) { keyFormats.add(keyFormat); } List<KeyFormat<?>> result = new ArrayList<>(keyFormats.size() + 6); // Add key formats for common key types result.add(new SimpleKeyFormat<>(String.class, Function.identity())); result.add(new SimpleKeyFormat<>(Byte.class, Byte::valueOf)); result.add(new SimpleKeyFormat<>(Short.class, Short::valueOf)); result.add(new SimpleKeyFormat<>(Integer.class, Integer::valueOf)); result.add(new SimpleKeyFormat<>(Long.class, Long::valueOf)); result.add(new SimpleKeyFormat<>(UUID.class, UUID::fromString)); result.addAll(keyFormats); return result; } }
private static void findAllProviders() { ServiceLoader<CDIProvider> providerLoader; Set<CDIProvider> providers = new TreeSet<>(Comparator.comparingInt(CDIProvider::getPriority).reversed()); providerLoader = ServiceLoader.load(CDIProvider.class, CDI.class.getClassLoader()); if(! providerLoader.iterator().hasNext()) { throw new IllegalStateException("Unable to locate CDIProvider"); } try { providerLoader.forEach(providers::add); } catch (ServiceConfigurationError e) { throw new IllegalStateException(e); } CDI.discoveredProviders = Collections.unmodifiableSet(providers); }
private PolicyValidator(Builder builder) { //first find all services ServiceLoader<PolicyExecutorService> services = ServiceLoader.load(PolicyExecutorService.class); for (PolicyExecutorService service : services) { executors.add(service.instantiate(builder.config.get(service.configKey()))); } //then add explicit this.executors.addAll(builder.executors); }
@Test public void testServiceListFactoryBean() { assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceListFactoryBean.class); bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName()); bf.registerBeanDefinition("service", bd); List<?> serviceList = (List<?>) bf.getBean("service"); assertTrue(serviceList.get(0) instanceof DocumentBuilderFactory); }
public static <SERVICE> SERVICE get(Class<SERVICE> serviceType, SERVICE defaultValue) { Iterator<SERVICE> services = ServiceLoader.load(serviceType, Services.class.getClassLoader()).iterator(); SERVICE result = services.hasNext() ? services.next() : defaultValue; if (services.hasNext()) { result = defaultValue; System.err.println(format("Found multiple implementations for the service provider %s. Using the default: %s", serviceType, result.getClass())); } return result; } }
private List<DistributedSystemService> initializeServices() { ServiceLoader<DistributedSystemService> loader = ServiceLoader.load(DistributedSystemService.class); List<DistributedSystemService> services = new ArrayList<>(); for (DistributedSystemService service : loader) { services.add(service); } return services; }
private AbacProvider(Builder builder) { ServiceLoader<AbacValidatorService> services = ServiceLoader.load(AbacValidatorService.class); for (AbacValidatorService service : services) { validators.add(service.instantiate(builder.config.get(service.configKey()))); } this.validators.addAll(builder.validators); Set<Class<? extends Annotation>> annotations = new HashSet<>(); Set<String> configKeys = new HashSet<>(); Set<Class<? extends AbacValidatorConfig>> customObjects = new HashSet<>(); validators.forEach(v -> { annotations.addAll(v.supportedAnnotations()); configKeys.add(v.configKey()); customObjects.add(v.configClass()); }); this.supportedAnnotations = Collections.unmodifiableSet(annotations); this.supportedConfigKeys = Collections.unmodifiableSet(configKeys); this.supportedCustomObjects = Collections.unmodifiableSet(customObjects); this.failOnUnvalidated = builder.failOnUnvalidated; this.failIfNoneValidated = builder.failIfNoneValidated; }