Class ServiceRegistrationsBinding<S>

java.lang.Object
eu.cloudnetservice.driver.impl.registry.ServiceRegistrationsBinding<S>
Type Parameters:
S - the model of the service type.

final class ServiceRegistrationsBinding<S> extends Object
A binding for a single type of service that holds the information about all registrations for the service.
Since:
4.0
  • Field Details

    • readLock

      private final Lock readLock
    • writeLock

      private final Lock writeLock
    • serviceType

      private final Class<S> serviceType
    • registry

      private final DefaultServiceRegistry registry
    • defaultRegistrationProxy

      private final eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> defaultRegistrationProxy
    • registrationsByName

      private final SequencedMap<String, eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S>> registrationsByName
    • obsolete

      private volatile boolean obsolete
    • defaultRegistrationRef

      private volatile eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> defaultRegistrationRef
  • Constructor Details

    • ServiceRegistrationsBinding

      public ServiceRegistrationsBinding(@NonNull @NonNull Class<S> serviceType, @NonNull @NonNull DefaultServiceRegistry serviceRegistry)
      Constructs a new service registration binding.
      Parameters:
      serviceType - the type of the service that is managed by this binding.
      serviceRegistry - the service registry in which this binding is registered.
      Throws:
      NullPointerException - if the given service type or service registry is null.
  • Method Details

    • defaultRegistrationProxy

      @NonNull public @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> defaultRegistrationProxy()
      Get the proxy service registration which always delegates to the current default service registration.
      Returns:
      the proxy service registration for the default service.
    • valid

      public boolean valid()
      Get if this registration binding is still valid.
      Returns:
      true if this registration is still valid, false otherwise.
    • registrationValid

      public boolean registrationValid(@NonNull @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> registration)
      Checks if the given registration is still valid in this binding.
      Parameters:
      registration - the registration to check.
      Returns:
      true if the given registration is still valid, false otherwise.
      Throws:
      NullPointerException - if the given registration is null.
    • registrationIsDefault

      public boolean registrationIsDefault(@NonNull @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> registration)
      Checks if the given registration is the default registration for the service.
      Parameters:
      registration - the registration to check.
      Returns:
      true if the given registration is the default service registration, false otherwise.
      Throws:
      NullPointerException - if the given registration is null.
    • markAsDefaultRegistration

      public void markAsDefaultRegistration(@NonNull @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> registration)
      Marks the given registration as the default registration.
      Parameters:
      registration - the registration to mark as the default registration.
      Throws:
      NullPointerException - if the given registration is null.
      IllegalStateException - if the given registration is not part of this binding.
    • findRegistrationByName

      @Nullable public @Nullable eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> findRegistrationByName(@NonNull @NonNull String serviceName)
      Get the service registration that is registered for the given name.
      Parameters:
      serviceName - the name of the service registration to get.
      Returns:
      the service registration associated with the given name, null if no such registration exists.
      Throws:
      NullPointerException - if the given name is null.
    • register

      @NonNull public @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> register(@NonNull @NonNull String serviceName, @NonNull S serviceImplementation)
      Registers a singleton service into this binding. If a binding with the same name already exists, the old binding is returned instead.
      Parameters:
      serviceName - the name to associate the service registration with.
      serviceImplementation - the implementation of the service to register.
      Returns:
      a registration representing the service mapping.
      Throws:
      NullPointerException - if the given service name or service implementation is null.
    • register

      @NonNull public @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> register(@NonNull @NonNull String serviceName, @NonNull @NonNull Class<? extends S> implementationType)
      Registers a constructing service into this binding, which is a service that returns a new instance of the service type on each invocation. If a binding with the same name already exists, the old binding is returned instead.
      Parameters:
      serviceName - the name to associate the service registration with.
      implementationType - the type that implements the managed service type.
      Returns:
      a registration representing the service mapping.
      Throws:
      NullPointerException - if the given service name or service implementation type is null.
      IllegalArgumentException - if the implementation type has no or an inaccessible no-args constructor.
    • register

      @Nullable private @Nullable eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> register(@NonNull @NonNull String serviceName, @NonNull @NonNull Supplier<eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S>> registrationFactory)
      Registers a new service registration into this binding. If the binding is the first registration, the default service will be set to that registration as well.
      Parameters:
      serviceName - the name to associate the new service registration with.
      registrationFactory - the factory to use to construct the registration if needed.
      Returns:
      a new registration if the service was registered successfully, null if this binding became obsolete.
      Throws:
      NullPointerException - if the given service name or registration factory is null.
    • unregisterRegistration

      public boolean unregisterRegistration(@NonNull @NonNull eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S> registration)
      Unregisters the given registration from this binding, marking this binding as obsolete if no more registrations remain after this call. If the given registration was the default registration, the first of the remaining registrations will be promoted to the default registration.
      Parameters:
      registration - the registration to unregister from this binding.
      Returns:
      true if the registration was unregistered from this binding, false otherwise.
      Throws:
      NullPointerException - if the given registration is null.
    • unregisterAllByClassLoader

      public void unregisterAllByClassLoader(@NonNull @NonNull ClassLoader classLoader)
      Unregisters all service registrations which uses an implementation type that was loaded by the given class loader.
      Parameters:
      classLoader - the class loader of which all associated registrations should be removed.
      Throws:
      NullPointerException - if the given class loader is null.
    • cleanupAndMarkObsolete

      public void cleanupAndMarkObsolete()
      Removes all service registration that are stored in this binding and marks this binding as obsolete.
    • registrations

      @NonNull public @UnmodifiableView @NonNull Collection<eu.cloudnetservice.driver.registry.ServiceRegistryRegistration<S>> registrations()
      Get an unmodifiable view of all registrations that are registered in this binding.
      Returns:
      an unmodifiable view of all registrations that are registered in this binding.
    • executeInReadLock

      private <T> @UnknownNullability T executeInReadLock(@NonNull @NonNull Supplier<T> action)
      Executes the given action in the read lock of this binding. The read lock allows for multiple threads to read at the same time, while no write operations in a write lock can happen. Write operations should not be executed within this lock.
      Type Parameters:
      T - the type that is returned by the action supplier.
      Parameters:
      action - the action to execute once the read lock of this binding is acquired.
      Returns:
      the result of the given action.
      Throws:
      NullPointerException - if the given action is null.
    • executeInWriteLock

      private <T> @UnknownNullability T executeInWriteLock(@NonNull @NonNull Supplier<T> action)
      Executes the given action in the write lock of this binding. Once the write lock is obtained for the action, no other thread can obtain a read or write lock. Write and read operations can be performed safely within the lock.
      Type Parameters:
      T - the type that is returned by the action supplier.
      Parameters:
      action - the action to execute once the write lock of this binding is acquired.
      Returns:
      the result of the given action.
      Throws:
      NullPointerException - if the given action is null.