CServiceRegistry / IServiceRegistry

The service registry manages 3 different object types:

  • Service starter
  • Services
  • Service listeners

The Service Starters

Service starters implement the IServiceStarter interface. They provide a list of dependencies for a service or the classes of a Java package. When the dependencies are fulfilled and all required interfaces have been registered in the service registry, the starter is started. On the other hand, when a dependency is dropped from the service registry, the starter is stopped.

Starters are registered at the service registry. This usually happens at the moment when a plug-in is started.

void addStarter(@NotNull IServiceStarter aStarter);

Starters can also be removed again.

void removeStarter(@NotNull IServiceStarter aStarter);

It is possible to check whether a starter has been registered.

boolean isStarted(IServiceStarter aStarter);

Services registration

Services can be registered in the service registry at any time. If a service is registered and there is a starter that needs this service as the last open dependency, this starter is then marked for start. It is not started immediately, but only pre-marked, since all starts of service starters are strictly serialized. This means that the next starter will not be started until the previous start has been completed.

The following method registers a singleton.

void registerService(@NotNull Class aClass,
                     @NotNull Object aInstance);

In practice, only public interfaces are registered. The implementing class is almost always package private.

class CDoSomething implements IDoSomeThing, IService
{
    ...
}

final CDoSomething service = new CDoSomething();
CServiceRegistry.getInstance()
                .registerService(IDoSomething.class,
                                 service);

Services that are not singletons are registered with properties to distinguish them.

void registerService(@NotNull Class aClass,
                     @Nullable CStringProperties aProperties,
                     @NotNull Object aInstance);

Example:

final CStringProperties sp = new CStringProperties();
sp.put("key1",
       "value1");
sp.put("key2",
       "value2");
CServiceRegister.getInstance().
                 registerService(IDoSomething.class,
                                 sp,
                                 new CDoSomething());

A simpler variant is the one with a property list using string form.

void registerService(@NotNull Class aClass,
                     @NotNull String aProperties,
                     @NotNull Object aInstance);

Example:

CServiceRegister.getInstance().
                 registerService(IDoSomething.class,
                                 "key1=value1,key2=value2",
                                 new CDoSomething());

Request services

Required services can be fetched from the service registry at any time.

@Nullable <T> T getService(@NotNull Class<T> aClass);
@NotNull <T> T getServiceOrThrow(@NotNull Class<T> aClass) throws CException;

While the first method can return null if the service is not (yet) available, the second method throws an exception in this case.

Example:

final IDoSomething service = CServiceRegistry.getInstance()
                                             .getService(IDoSomething.class);

Non-singletons are fetched with additional properties:

@Nullable <T> T getService(@NotNull Class<T> aClass,
                           @NotNull String aProperties);
@NotNull <T> T getServiceOrThrow(@NotNull Class<T> aClass,
                                 @NotNull String aProperties) throws CException;

Remove services

Services can of course be removed again. This is done mainly in the stop() method of the service starter. The first method expects the service instance as a parameter.

void deregisterService(@NotNull Object aInstance);

It is also possible to remove a service type. This method expects the service class (the interface) as a parameter.

void deregisterServiceType(@NotNull Class aClass);

Debugging

This method outputs the status of services and service starters via the logging framework. For example, it informs about the pending dependencies of waiting starters. The output is at the DEBUG level of the logger for the package "de.sillysky.nyssr.impl.service".

public void printStatus();

Service listeners

Service listeners can be registered to monitor the registration of services. You can be interested in certain services or in all of them.

void registerServiceListener(@NotNull IServiceListener aListener);
void registerServiceListener(@NotNull Class aClass,
                             @NotNull IServiceListener aListener);

Accordingly, there are also methods for removing the listeners.

void deregisterServiceListener(@NotNull IServiceListener aListener);

void deregisterServiceListener(@NotNull Class aClass,
                               @NotNull IServiceListener aListener);