All Downloads are FREE. Search and download functionalities are using the official Maven repository.

domino.java.ServiceProviderCapsule Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
package domino.java;

import static de.tototec.utils.functional.FList.map;
import static de.tototec.utils.functional.FList.mkString;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import de.tototec.utils.functional.Optional;
import domino.java.capsule.Capsule;
import domino.java.internal.Logger;
import domino.java.internal.LoggerFactory;

/**
 * A capsule which registers an object in the OSGi service registry while the
 * current capsule scope is active.
 *
 * @param types
 *            Types under which to register the given service in the OSGi
 *            registry
 * @param properties
 *            Service properties
 * @param bundleContext
 *            Bundle context
 * @param service
 *            The object to be registered
 * @tparam S Service type
 */
public class ServiceProviderCapsule implements Capsule {

	private final Logger log = LoggerFactory.getLogger(ServiceProviderCapsule.class);

	private final Iterable> interfaces;
	private final Map properties;
	private final BundleContext bundleContext;
	private final S service;

	public ServiceProviderCapsule(
			final Iterable> interfaces,
			final Map properties,
			final BundleContext bundleContext,
			final S service) {
		this.interfaces = interfaces;
		this.properties = properties;
		this.bundleContext = bundleContext;
		this.service = service;

	}

	private Optional> serviceRegistration = Optional.none();

	/**
	 * Returns the service registration.
	 */
	public Optional> serviceRegistration() {
		return serviceRegistration;
	}

	@Override
	public void start() {
		// Create array of class names under which the service shall be
		// registered
		final List types = map(interfaces, i -> i.getName());

		final Hashtable props = new Hashtable<>(properties);

		if (log.isDebugEnabled()) {
			log.debug("About to provide the service: {}\n  interfaces: {}\n  properties: {}",
					service, mkString(types, ", "), properties);
		}

		// Register service
		@SuppressWarnings("unchecked")
		final ServiceRegistration serviceRegistration = (ServiceRegistration) bundleContext
				.registerService(types.toArray(new String[types.size()]), service, props);
		this.serviceRegistration = Optional.lift(serviceRegistration);
	}

	@Override
	public void stop() {
		serviceRegistration.foreach(reg -> {

			try {
				if (log.isDebugEnabled()) {
					final List types = map(interfaces, i -> i.getName());
					log.debug("Removing provided service: {}\n  interfaces: {}\n  properties: {}",
							service, mkString(types, ", "), properties);
				}
				reg.unregister();
			} catch (final IllegalStateException e) {
				// Do nothing. Was already unregistered.
			}
			serviceRegistration = Optional.none();
		});

	}

}