ShrinkWrap Resolver SPI 2.0.0-cr-1

org.jboss.shrinkwrap.resolver.spi.loader
Class ServiceRegistry

java.lang.Object
  extended by org.jboss.shrinkwrap.resolver.spi.loader.ServiceRegistry
All Implemented Interfaces:
ServiceLoader

public class ServiceRegistry
extends Object
implements ServiceLoader

A simple singleton (application scoped} registry that fulfills ServiceLoader contract. This registry uses underlying ServiceLoader implementation to load the service while storing cached instances in a local map.

Author:
Karel Piwko

Constructor Summary
ServiceRegistry(ServiceLoader serviceLoader)
          Creates a service registry with a ServiceLoader to load service instances until they are cached
 
Method Summary
<T> Collection<T>
all(Class<T> serviceClass)
          Loads all registered services for given serviceClass
static ServiceRegistry getInstance()
          Returns ServiceRegistry instance.
<T> T
onlyOne(Class<T> serviceClass)
          Loads a registered service for given serviceClass
<T> T
onlyOne(Class<T> serviceClass, Class<? extends T> defaultImplementationClass)
          Loads a registered service for given serviceClass.
static void register(ServiceRegistry registry)
          Registers an instance of ServiceRegistry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceRegistry

public ServiceRegistry(ServiceLoader serviceLoader)
Creates a service registry with a ServiceLoader to load service instances until they are cached

Parameters:
serviceLoader -
Method Detail

getInstance

public static ServiceRegistry getInstance()
                                   throws IllegalStateException
Returns ServiceRegistry instance. Instance must be registered first.

Returns:
Throws:
IllegalStateException - If no service registry was registered yet

register

public static void register(ServiceRegistry registry)
Registers an instance of ServiceRegistry.

Parameters:
registry -

all

public <T> Collection<T> all(Class<T> serviceClass)
                  throws IllegalArgumentException
Loads all registered services for given serviceClass

Specified by:
all in interface ServiceLoader
Parameters:
serviceClass -
Returns:
Throws:
IllegalArgumentException - If serviceClass is null

onlyOne

public <T> T onlyOne(Class<T> serviceClass)
          throws IllegalArgumentException,
                 IllegalStateException
Loads a registered service for given serviceClass

Specified by:
onlyOne in interface ServiceLoader
Parameters:
serviceClass -
Returns:
Throws:
IllegalArgumentException - If either classLoader or serviceClass is null
IllegalStateException - If more than a single service is registered

onlyOne

public <T> T onlyOne(Class<T> serviceClass,
                     Class<? extends T> defaultImplementationClass)
          throws IllegalArgumentException,
                 IllegalStateException
Loads a registered service for given serviceClass. Reverts to the defaultImplementationClass if no other service is registered. If defaultImplemenationClass is registered as well, it simply ignores it during resolution.

Specified by:
onlyOne in interface ServiceLoader
Parameters:
serviceClass -
defaultImplementationClass -
Returns:
Throws:
IllegalArgumentException - If either serviceClass or defaultImplementationClass is null
IllegalStateException - If more than a single service is registered

ShrinkWrap Resolver SPI 2.0.0-cr-1

Copyright © 2013 JBoss by Red Hat. All Rights Reserved.