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

com.justdavis.karl.misc.datasources.provisioners.DataSourceProvisionersManager Maven / Gradle / Ivy

The newest version!
package com.justdavis.karl.misc.datasources.provisioners;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.justdavis.karl.misc.datasources.IDataSourceCoordinates;

/**
 * This class is a user friendly front-end to the various
 * {@link IDataSourceProvisioner} implementations. The
 * {@link #provision(IProvisioningTargetsProvider, IProvisioningRequest)} method
 * will select the correct {@link IDataSourceProvisioner} implementation to meet
 * the specified request and will find a usable {@link IProvisioningTarget} from
 * those provided by the specified {@link IProvisioningTargetsProvider}.
 * 
 * @see IProvisioningTargetsProvider
 * @see XmlProvisioningTargetsProvider
 */
@Component
public final class DataSourceProvisionersManager {
	private final Set> provisioners;

	/**
	 * Constructs a new {@link DataSourceProvisionersManager} instance.
	 * 
	 * @param provisioners
	 *            the available {@link IDataSourceProvisioner}s
	 */
	@Inject
	public DataSourceProvisionersManager(
			Set> provisioners) {
		if (provisioners == null)
			throw new IllegalArgumentException();
		if (provisioners.isEmpty())
			throw new IllegalArgumentException();

		this.provisioners = provisioners;
	}

	/**
	 * Constructs a new {@link DataSourceProvisionersManager} instance.
	 * 
	 * @param provisioners
	 *            the available {@link IDataSourceProvisioner}s
	 */
	public DataSourceProvisionersManager(
			IDataSourceProvisioner... provisioners) {
		this(new HashSet>(
				Arrays.asList(provisioners)));
	}

	/**
	 * @return the available {@link IDataSourceProvisioner}s
	 */
	public Set> getProvisioners() {
		return provisioners;
	}

	/**
	 * A user-friendly alternative to
	 * {@link IDataSourceProvisioner#provision(IProvisioningTarget, IProvisioningRequest)}
	 * .
	 * 
	 * @param targetsProvider
	 *            the {@link IProvisioningTargetsProvider} with the
	 *            {@link IProvisioningTarget}s that are available for use
	 * @param request
	 *            the {@link IProvisioningRequest} to fulfill
	 * @return the {@link IDataSourceCoordinates} generated by fulfilling the
	 *         specified {@link IProvisioningRequest}
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ProvisioningResult provision(IProvisioningTargetsProvider targetsProvider, IProvisioningRequest request) {
		// Find the provisioner that matches the request.
		IDataSourceProvisioner matchingProvisioner = null;
		for (IDataSourceProvisioner provisioner : provisioners) {
			if (provisioner.getRequestType().equals(request.getClass()))
				matchingProvisioner = provisioner;
		}

		// Find the available target that matches the provisioner.
		IProvisioningTarget target = targetsProvider.findTarget(matchingProvisioner.getTargetType());

		// Run the provisioning request and return the result.
		IDataSourceCoordinates provisionedCoords = matchingProvisioner.provision(target, request);
		return new ProvisioningResult(target, request, provisionedCoords);
	}

	/**
	 * A user-friendly alternative to
	 * {@link IDataSourceProvisioner#provision(IProvisioningTarget, IProvisioningRequest)}
	 * .
	 * 
	 * @param provisioningResult
	 *            the {@link ProvisioningResult} instance whose provisioned data
	 *            source repository is to be deleted
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void delete(ProvisioningResult provisioningResult) {
		// Find the provisioner that matches the request.
		IDataSourceProvisioner matchingProvisioner = null;
		for (IDataSourceProvisioner provisioner : provisioners) {
			if (provisioner.getTargetType().equals(provisioningResult.getTarget().getClass()))
				matchingProvisioner = provisioner;
		}

		matchingProvisioner.delete(provisioningResult.getTarget(), provisioningResult.getRequest());
	}

	/**
	 * Models the results of a
	 * {@link DataSourceProvisionersManager#provision(IProvisioningTargetsProvider, IProvisioningRequest)}
	 * operation.
	 */
	public static final class ProvisioningResult {
		private final IProvisioningTarget target;
		private final IProvisioningRequest request;
		private final IDataSourceCoordinates coords;

		/**
		 * Constructs a new {@link ProvisioningResult} instance.
		 * 
		 * @param target
		 *            the value to use for {@link #getTarget()}
		 * @param request
		 *            the value to use for {@link #getRequest()}
		 * @param coords
		 *            the value to use for {@link #getCoords()}
		 */
		public ProvisioningResult(IProvisioningTarget target, IProvisioningRequest request,
				IDataSourceCoordinates coords) {
			if (target == null)
				throw new IllegalArgumentException();
			if (request == null)
				throw new IllegalArgumentException();
			if (coords == null)
				throw new IllegalArgumentException();

			this.target = target;
			this.request = request;
			this.coords = coords;
		}

		/**
		 * @return the {@link IProvisioningTarget} associated with
		 *         {@link #getCoords()} (generally, represents the server that
		 *         the data source repository was provisioned to)
		 */
		public IProvisioningTarget getTarget() {
			return target;
		}

		/**
		 * @return the {@link IProvisioningRequest} that was passed to
		 *         {@link DataSourceProvisionersManager#provision(IProvisioningTargetsProvider, IProvisioningRequest)}
		 *         and led to the creation of this {@link ProvisioningResult}
		 */
		public IProvisioningRequest getRequest() {
			return request;
		}

		/**
		 * @return the {@link IDataSourceCoordinates} to the data source
		 *         repository that was provisioned
		 */
		public IDataSourceCoordinates getCoords() {
			return coords;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy