
org.jboss.shrinkwrap.resolver.spi.loader.ServiceRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shrinkwrap-resolver-spi Show documentation
Show all versions of shrinkwrap-resolver-spi Show documentation
SPI to Resolve Dependencies from a Generic Backend
The newest version!
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.spi.loader;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* A simple singleton (application scoped) registry that fulfills {@link ServiceLoader} contract. This registry uses underlying
* {@link ServiceLoader} implementation to load the service while storing cached instances in a local map.
*
* @author Karel Piwko
*
*/
public class ServiceRegistry implements ServiceLoader {
private final ServiceLoader serviceLoader;
private final Map, Collection>> cachedServiceInstances;
private static ServiceRegistry instance;
/**
* Creates a service registry with a {@link ServiceLoader} to load service instances until they are cached
*
* @param serviceLoader The underlying {@link ServiceLoader} to use for loading services
*/
public ServiceRegistry(ServiceLoader serviceLoader) {
this.serviceLoader = serviceLoader;
this.cachedServiceInstances = Collections.synchronizedMap(new HashMap<>());
}
/**
* Returns {@link ServiceRegistry} instance. Instance must be registered first.
*
* @return The singleton instance of {@link ServiceRegistry}
* @throws IllegalStateException If no service registry was registered yet
*/
public static synchronized ServiceRegistry getInstance() throws IllegalStateException {
if (instance == null) {
throw new IllegalStateException("Unable to get instance of Service Registry, it was not initialized.");
}
return instance;
}
/**
* Registers an instance of {@link ServiceRegistry}.
*
* @param registry The {@link ServiceRegistry} instance to register
*/
public static synchronized void register(ServiceRegistry registry) {
instance = registry;
}
@SuppressWarnings("unchecked")
@Override
public Collection all(Class serviceClass) throws IllegalArgumentException {
if (serviceClass == null) {
throw new IllegalArgumentException("ServiceClass must be provided");
}
synchronized (cachedServiceInstances) {
if (cachedServiceInstances.containsKey(serviceClass)) {
return (Collection) cachedServiceInstances.get(serviceClass);
}
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy