public <T> T getService(final Class<T> serviceInterface) { return getServiceEntry(serviceInterface).implementation; }
public <T> void removeServiceListener(final Class<T> serviceInterface, final IServiceChangeListener<T> listener) { final ServiceRegistryEntry<T> entry = getServiceEntry(serviceInterface); entry.listeners.remove(listener); }
public <T> void addServiceListener(final Class<T> serviceInterface, final IServiceChangeListener<T> listener) { final ServiceRegistryEntry<T> entry = getServiceEntry(serviceInterface); if (null != entry.implementation) { listener.serviceChanged(null, entry.implementation); } entry.listeners.add(listener); }
public <T> void registerService(final Class<T> serviceInterface, final T implementation) { Preconditions.checkNotNull(implementation, "Registering service for " + serviceInterface.getSimpleName() + " is prohibited with unspecified implementation."); final ServiceRegistryEntry<T> entry = getServiceEntry(serviceInterface); final T oldImplementation = entry.implementation; entry.implementation = implementation; // notify listeners for (final Iterator<IServiceChangeListener<T>> ir = new CopyOnWriteArrayList<IServiceChangeListener<T>>(entry.listeners).iterator(); ir.hasNext(); /**/) { final IServiceChangeListener<T> listener = ir.next(); listener.serviceChanged(oldImplementation, implementation); } // dispose old registered implementation if (oldImplementation instanceof IDisposableService && !((IDisposableService) oldImplementation).isDisposed()) { ((IDisposableService)oldImplementation).dispose(); } LOGGER.debug(MessageFormat.format("Registered service {0} for interface {1}.", implementation.getClass().getName(), serviceInterface.getName())); }