private <T> void loadFilterConverterFactories(Class<T> c, BiConsumer<String, T> biConsumer) { ServiceFinder.load(c).forEach(factory -> { NamedFactory annotation = factory.getClass().getAnnotation(NamedFactory.class); if (annotation != null) { String name = annotation.name(); biConsumer.accept(name, factory); } }); }
public static <T> Collection<T> load(Class<T> contract, ClassLoader... loaders) { Map<String, T> services = new LinkedHashMap<>(); if (loaders.length == 0) { try { ServiceLoader<T> loadedServices = ServiceLoader.load(contract); addServices(loadedServices, services); } catch (Exception e) { // Ignore } } else { for (ClassLoader loader : loaders) { if (loader == null) throw new NullPointerException(); try { ServiceLoader<T> loadedServices = ServiceLoader.load(contract, loader); addServices(loadedServices, services); } catch (Exception e) { // Ignore } } } addOsgiServices( contract, services ); if (services.isEmpty()) { LOG.debugf("No service impls found: %s", contract.getSimpleName()); } return services.values(); }
private Properties addProgrammaticMappings(Properties indexingProperties, ComponentRegistry cr) { Iterator<ProgrammaticSearchMappingProvider> providers = ServiceFinder.load(ProgrammaticSearchMappingProvider.class).iterator(); if (providers.hasNext()) { SearchMapping mapping = (SearchMapping) indexingProperties.get(Environment.MODEL_MAPPING); if (mapping == null) { mapping = new SearchMapping(); Properties amendedProperties = new Properties(); amendedProperties.putAll(indexingProperties); amendedProperties.put(Environment.MODEL_MAPPING, mapping); indexingProperties = amendedProperties; } Cache cache = cr.getComponent(Cache.class); while (providers.hasNext()) { ProgrammaticSearchMappingProvider provider = providers.next(); if (log.isDebugEnabled()) { log.debugf("Loading programmatic search mappings for cache %s from provider : %s", cache.getName(), provider.getClass().getName()); } provider.defineMappings(cache, mapping); } } return indexingProperties; }
public CodecRegistry(EmbeddedCacheManager cacheManager) { GlobalConfiguration globalConfiguration = cacheManager.getCacheManagerConfiguration(); ClassLoader cl = globalConfiguration.classLoader(); ClassWhiteList classWhiteList = cacheManager.getClassWhiteList(); codecs = new HashMap<>(); Iterator<Codec> it = ServiceFinder.load(Codec.class, cl).iterator(); for (; ; ) { try { Codec codec = it.next(); codec.setWhiteList(classWhiteList); String name = codec.getName(); if (codecs.containsKey(name)) { log.duplicateCodec(codec.getClass().getName(), codecs.get(name).getClass().getName()); } else { codecs.put(name, codec); } } catch (ServiceConfigurationError e) { log.loadingCodecFailed(e); } catch (NoSuchElementException e) { break; } } }
private SearchIntegrator createSearchIntegrator(IndexingConfiguration indexingConfiguration, ComponentRegistry cr, ClassLoader aggregatedClassLoader) { SearchIntegrator searchIntegrator = cr.getComponent(SearchIntegrator.class); if (searchIntegrator != null && !searchIntegrator.isStopped()) { // a paranoid check against an unlikely failure throw new IllegalStateException("SearchIntegrator already initialized!"); } // load ProgrammaticSearchMappingProviders from classpath Collection<ProgrammaticSearchMappingProvider> programmaticSearchMappingProviders = new LinkedHashSet<>(); programmaticSearchMappingProviders.add(new DefaultSearchMappingProvider()); // make sure our DefaultSearchMappingProvider is first programmaticSearchMappingProviders.addAll(ServiceFinder.load(ProgrammaticSearchMappingProvider.class, aggregatedClassLoader)); // load LuceneAnalysisDefinitionProvider from classpath Collection<LuceneAnalysisDefinitionProvider> analyzerDefProviders = ServiceFinder.load(LuceneAnalysisDefinitionProvider.class, aggregatedClassLoader); // Set up the search factory for Hibernate Search first. SearchConfiguration config = new SearchableCacheConfiguration(indexingConfiguration.indexedEntities(), indexingConfiguration.properties(), programmaticSearchMappingProviders, analyzerDefProviders, cr, aggregatedClassLoader); searchIntegrator = new SearchIntegratorBuilder().configuration(config).buildSearchIntegrator(); cr.registerComponent(searchIntegrator, SearchIntegrator.class); return searchIntegrator; }
@Test public void testDuplicateServiceFinder() { ClassLoader mainClassLoader = this.getClass().getClassLoader(); ClassLoader otherClassLoader = new ClonedClassLoader(mainClassLoader); Collection<SampleSPI> spis = ServiceFinder.load(SampleSPI.class, mainClassLoader, otherClassLoader); assertEquals(1, spis.size()); }