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

com.github.bordertech.didums.hk2.DidumsHk2Provider Maven / Gradle / Ivy

package com.github.bordertech.didums.hk2;

import com.github.bordertech.config.Config;
import com.github.bordertech.didums.DidumsProvider;
import java.lang.annotation.Annotation;
import javax.inject.Singleton;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.hk2.utilities.binding.ServiceBindingBuilder;

/**
 * Didums Provider that uses HK2 for binding.
 *
 * @author Jonathan Austin
 * @since 1.0.0
 */
public class DidumsHk2Provider implements DidumsProvider {

	/**
	 * Default HK2 context name.
	 */
	public static final String CONTEXT_NAME = Config.getInstance().getString("bordertech.didums.hk2.context", "bt-hk2");

	private final ServiceLocator serviceLocator;

	/**
	 * Use the default HK2 service locator.
	 */
	public DidumsHk2Provider() {
		ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
		serviceLocator = factory.create(CONTEXT_NAME);
	}

	/**
	 * Use a predefined HK2 service locator.
	 *
	 * @param serviceLocator the HK2 service locator to use
	 */
	public DidumsHk2Provider(final ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	/**
	 * @return the HK2 service locator
	 */
	public final ServiceLocator getServiceLocator() {
		return serviceLocator;
	}

	@Override
	public  T getService(final Class service, final Annotation... qualifiers) {
		return serviceLocator.getService(service, qualifiers);
	}

	@Override
	public  T createAndInject(final Class createMe) {
		return serviceLocator.createAndInitialize(createMe);
	}

	@Override
	public  void bind(final Class contract, final Class contractImpl, final boolean singleton, final Annotation... qualifiers) {
		Binder binder = new AbstractBinder() {
			@Override
			protected void configure() {
				ServiceBindingBuilder builder = bind(contractImpl).to(contract);
				for (Annotation annotation : qualifiers) {
					builder.qualifiedBy(annotation);
				}
				if (singleton) {
					builder.in(Singleton.class);
				}
			}
		};
		ServiceLocatorUtilities.bind(serviceLocator, binder);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy