use of java.util.ServiceLoader in project hibernate-orm by hibernate.
the class ClassLoaderServiceImpl method stop.
@Override
public void stop() {
for (ServiceLoader serviceLoader : serviceLoaders.values()) {
// clear service loader providers
serviceLoader.reload();
}
serviceLoaders.clear();
// Avoid ClassLoader leaks
this.aggregatedClassLoader = null;
}
use of java.util.ServiceLoader in project mule by mulesoft.
the class ApplicationModel method getConfigurationPropertiesProvidersFromComponents.
private List<ConfigurationPropertiesProvider> getConfigurationPropertiesProvidersFromComponents(ArtifactConfig artifactConfig, ConfigurationPropertiesResolver localResolver) {
Map<ComponentIdentifier, ConfigurationPropertiesProviderFactory> providerFactoriesMap = new HashMap<>();
ServiceLoader<ConfigurationPropertiesProviderFactory> providerFactories = java.util.ServiceLoader.load(ConfigurationPropertiesProviderFactory.class);
providerFactories.forEach(service -> {
ComponentIdentifier componentIdentifier = service.getSupportedComponentIdentifier();
if (providerFactoriesMap.containsKey(componentIdentifier)) {
throw new MuleRuntimeException(createStaticMessage("Multiple configuration providers for component: " + componentIdentifier));
}
providerFactoriesMap.put(componentIdentifier, service);
});
List<ConfigurationPropertiesProvider> configConfigurationPropertiesProviders = new ArrayList<>();
artifactConfig.getConfigFiles().stream().forEach(configFile -> configFile.getConfigLines().stream().forEach(configLine -> {
for (ConfigLine componentConfigLine : configLine.getChildren()) {
if (componentConfigLine.getNamespace() == null) {
continue;
}
ComponentIdentifier componentIdentifier = ComponentIdentifier.builder().namespace(componentConfigLine.getNamespace()).name(componentConfigLine.getIdentifier()).build();
if (!providerFactoriesMap.containsKey(componentIdentifier)) {
continue;
}
DefaultConfigurationParameters.Builder configurationParametersBuilder = DefaultConfigurationParameters.builder();
ConfigurationParameters configurationParameters = resolveConfigurationParameters(configurationParametersBuilder, componentConfigLine, localResolver);
ConfigurationPropertiesProvider provider = providerFactoriesMap.get(componentIdentifier).createProvider(configurationParameters, externalResourceProvider);
if (provider instanceof Component) {
Component providerComponent = (Component) provider;
TypedComponentIdentifier typedComponentIdentifier = TypedComponentIdentifier.builder().type(UNKNOWN).identifier(componentIdentifier).build();
DefaultComponentLocation.DefaultLocationPart locationPart = new DefaultComponentLocation.DefaultLocationPart(componentIdentifier.getName(), of(typedComponentIdentifier), of(configFile.getFilename()), of(configLine.getLineNumber()));
providerComponent.setAnnotations(ImmutableMap.<QName, Object>builder().put(AbstractComponent.LOCATION_KEY, new DefaultComponentLocation(of(componentIdentifier.getName()), singletonList(locationPart))).build());
}
configConfigurationPropertiesProviders.add(provider);
try {
initialiseIfNeeded(provider);
} catch (InitialisationException e) {
throw new MuleRuntimeException(e);
}
}
}));
return configConfigurationPropertiesProviders;
}
use of java.util.ServiceLoader in project shiro by apache.
the class EnvironmentLoaderServiceTest method multipleServiceTest.
@Test()
public void multipleServiceTest() throws Exception {
List<WebEnvironmentStub> environmentList = Arrays.asList(new WebEnvironmentStub(), new WebEnvironmentStub());
ServletContext servletContext = EasyMock.mock(ServletContext.class);
expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(null);
PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load");
final ServiceLoader serviceLoader = PowerMock.createMock(ServiceLoader.class);
EasyMock.expect(ServiceLoader.load(WebEnvironment.class)).andReturn(serviceLoader);
EasyMock.expect(serviceLoader.iterator()).andReturn(environmentList.iterator());
EasyMock.replay(servletContext);
PowerMock.replayAll();
try {
new EnvironmentLoader().createEnvironment(servletContext);
Assert.fail("Expected ConfigurationException to be thrown");
} catch (ConfigurationException e) {
assertThat(e.getMessage(), stringContainsInOrder("zero or exactly one", "shiroEnvironmentClass"));
}
PowerMock.verifyAll();
EasyMock.verify(servletContext);
}
use of java.util.ServiceLoader in project hbase by apache.
the class SaslServerAuthenticationProviders method createProviders.
/**
* Loads server authentication providers from the classpath and configuration, and then creates
* the SaslServerAuthenticationProviders instance.
*/
static SaslServerAuthenticationProviders createProviders(Configuration conf) {
ServiceLoader<SaslServerAuthenticationProvider> loader = ServiceLoader.load(SaslServerAuthenticationProvider.class);
HashMap<Byte, SaslServerAuthenticationProvider> providers = new HashMap<>();
for (SaslServerAuthenticationProvider provider : loader) {
addProviderIfNotExists(provider, providers);
}
addExtraProviders(conf, providers);
if (LOG.isTraceEnabled()) {
String loadedProviders = providers.values().stream().map((provider) -> provider.getClass().getName()).collect(Collectors.joining(", "));
if (loadedProviders.isEmpty()) {
loadedProviders = "None!";
}
LOG.trace("Found SaslServerAuthenticationProviders {}", loadedProviders);
}
// Initialize the providers once, before we get into the RPC path.
providers.forEach((b, provider) -> {
try {
// Give them a copy, just to make sure there is no funny-business going on.
provider.init(new Configuration(conf));
} catch (IOException e) {
LOG.error("Failed to initialize {}", provider.getClass(), e);
throw new RuntimeException("Failed to initialize " + provider.getClass().getName(), e);
}
});
return new SaslServerAuthenticationProviders(conf, providers);
}
use of java.util.ServiceLoader in project spring-framework by spring-projects.
the class ServiceLoaderTests method testServiceLoaderFactoryBean.
@Test
void testServiceLoaderFactoryBean() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(ServiceLoaderFactoryBean.class);
bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName());
bf.registerBeanDefinition("service", bd);
ServiceLoader<?> serviceLoader = (ServiceLoader<?>) bf.getBean("service");
boolean condition = serviceLoader.iterator().next() instanceof DocumentBuilderFactory;
assertThat(condition).isTrue();
}
Aggregations