= ServiceRegistry
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

Collects together methods for injecting or looking up domain services (either provided by the framework or application-specific) currently known to the runtime.

== API

[source,java]
.ServiceRegistry.java
----
interface ServiceRegistry {
  Can<T> select(Class<T> type, Annotation[] qualifiers)     // <.>
  Can<T> select(Class<T> type)     // <.>
  Stream<_SingletonBeanProvider> streamRegisteredBeansOfType(Class<?> requiredType)     // <.>
  Stream<_SingletonBeanProvider> streamRegisteredBeans()     // <.>
  Optional<_SingletonBeanProvider> lookupRegisteredBeanById(LogicalType id)     // <.>
  _SingletonBeanProvider lookupRegisteredBeanByIdElseFail(LogicalType id)     // <.>
  Optional<?> lookupBeanById(String id)
  Optional<T> lookupService(Class<T> serviceClass)     // <.>
  Optional<T> lookupService(Class<T> serviceClass, Comparator<Object> comparator)     // <.>
  T lookupServiceElseFail(Class<T> serviceClass)     // <.>
  void clearRegisteredBeans()     // <.>
}
----

<.> xref:#select_Class_Annotation[select(Class, Annotation)]
+
--
Obtains a xref:refguide:commons:index/collections/Can.adoc[Can] container containing any matching instances for the given required type and additional required qualifiers.
--
<.> xref:#select_Class[select(Class)]
+
--
Obtains a xref:refguide:commons:index/collections/Can.adoc[Can] container containing any matching instances for the given required type.
--
<.> xref:#streamRegisteredBeansOfType_Class[streamRegisteredBeansOfType(Class)]
+
--
Streams all registered bean adapters implementing the requested type.
--
<.> xref:#streamRegisteredBeans_[streamRegisteredBeans()]
+
--
Returns all bean adapters that have been registered.
--
<.> xref:#lookupRegisteredBeanById_LogicalType[lookupRegisteredBeanById(LogicalType)]
+
--
Returns a registered bean of given _name_ .
--
<.> xref:#lookupRegisteredBeanByIdElseFail_LogicalType[lookupRegisteredBeanByIdElseFail(LogicalType)]
+
--
Returns a registered bean of given _name_ , or throws when no such bean.
--
<.> xref:#lookupService_Class[lookupService(Class)]
+
--
Returns a domain service implementing the requested type.
--
<.> xref:#lookupService_Class_Comparator[lookupService(Class, Comparator)]
+
--
Returns a domain service implementing the requested type.
--
<.> xref:#lookupServiceElseFail_Class[lookupServiceElseFail(Class)]
+
--
Looks up a domain service of the requested type (same as _#lookupService(Class)_ ) but throws a _NoSuchElementException_ if there are no such instances.
--
<.> xref:#clearRegisteredBeans_[clearRegisteredBeans()]
+
--
Invalidates any cached service adapters that might hold a reference to the current _SpecificationLoader_ . Particularly useful when discarding a meta-model instance, that is, purging the _ObjectSpecification_ cache.
--

== Members

[#select_Class_Annotation]
=== select(Class, Annotation)

Obtains a xref:refguide:commons:index/collections/Can.adoc[Can] container containing any matching instances for the given required type and additional required qualifiers.

[#select_Class]
=== select(Class)

Obtains a xref:refguide:commons:index/collections/Can.adoc[Can] container containing any matching instances for the given required type.

[#streamRegisteredBeansOfType_Class]
=== streamRegisteredBeansOfType(Class)

Streams all registered bean adapters implementing the requested type.

[#streamRegisteredBeans_]
=== streamRegisteredBeans()

Returns all bean adapters that have been registered.

[#lookupRegisteredBeanById_LogicalType]
=== lookupRegisteredBeanById(LogicalType)

Returns a registered bean of given _name_ .

[#lookupRegisteredBeanByIdElseFail_LogicalType]
=== lookupRegisteredBeanByIdElseFail(LogicalType)

Returns a registered bean of given _name_ , or throws when no such bean.

[#lookupService_Class]
=== lookupService(Class)

Returns a domain service implementing the requested type.

If this lookup is ambiguous, the service annotated with highest priority is returned. see _Priority_

[#lookupService_Class_Comparator]
=== lookupService(Class, Comparator)

Returns a domain service implementing the requested type.

If this lookup is ambiguous, then the provided comparator is used.

[#lookupServiceElseFail_Class]
=== lookupServiceElseFail(Class)

Looks up a domain service of the requested type (same as _#lookupService(Class)_ ) but throws a _NoSuchElementException_ if there are no such instances.

[#clearRegisteredBeans_]
=== clearRegisteredBeans()

Invalidates any cached service adapters that might hold a reference to the current _SpecificationLoader_ . Particularly useful when discarding a meta-model instance, that is, purging the _ObjectSpecification_ cache.

include::hooks/ServiceRegistry_010-implementation.adoc[]

include::hooks/ServiceRegistry_020-examples-and-usage.adoc[]
