Refine search
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(); } } } }
/** * 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 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); }
public static <S> S loadFirst(final Class<S> clazz) { final ServiceLoader<S> loader = loadAll(clazz); final Iterator<S> iterator = loader.iterator(); if (!iterator.hasNext()) { throw new IllegalStateException(String.format( "No implementation defined in /META-INF/services/%s, please check whether the file exists and has the right implementation class!", clazz.getName())); } return iterator.next(); }
@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); }
@Override public OpenAPIConfiguration load(String path) throws IOException { ServiceLoader<OpenAPIConfigBuilder> loader = ServiceLoader.load(OpenAPIConfigBuilder.class); if (loader.iterator().hasNext()) { return loader.iterator().next().build(); } throw new IOException("Error loading OpenAPIConfigBuilder service implementation."); }
private void initCmdbService() throws NacosException { Iterator<CmdbService> iterator = serviceLoader.iterator(); if (iterator.hasNext()) { cmdbService = iterator.next(); } if (cmdbService == null && switches.isLoadDataAtStart()) { throw new NacosException(NacosException.SERVER_ERROR, "Cannot initialize CmdbService!"); } }
@Test public void testServiceFactoryBean() { assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext()); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ServiceFactoryBean.class); bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName()); bf.registerBeanDefinition("service", bd); assertTrue(bf.getBean("service") 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; } }
/** * Load first s. * * @param <S> the type parameter * @param clazz the clazz * @return the s */ public static <S> S loadFirst(final Class<S> clazz) { final ServiceLoader<S> loader = loadAll(clazz); final Iterator<S> iterator = loader.iterator(); if (!iterator.hasNext()) { throw new IllegalStateException(String.format( "No implementation defined in /META-INF/services/%s, please check whether the file exists and has the right implementation class!", clazz.getName())); } return iterator.next(); }