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

com.marvelution.maven.components.migration.manager.configuration.BuildPlugin Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.maven.components.migration.manager.configuration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.marvelution.utils.ListUtils;
import com.marvelution.utils.StringUtils;

/**
 * Configuration item for plugins required for the migration
 * 
 * @author Mark Rekveld
 */
public class BuildPlugin {

	/**
	 * The groupId of the plugin
	 */
	private String groupId;

	/**
	 * The artifactId of the plugin
	 */
	private String artifactId;

	/**
	 * The version of the plugin
	 */
	private String version;

	/**
	 * The configuration of the plugin
	 */
	private Map configuration;

	/**
	 * Constructor. Create a {@link BuildPlugin} from a Management Key in format groupId:artifactId with :version
	 * added optionally
	 * 
	 * @param managementKey the Management key of a BuildPlugin groupId:artifactId[:version]
	 */
	public BuildPlugin(String managementKey) {
		final String[] items = managementKey.split(":");
		if (items.length < 2 || items.length > 3) {
			throw new IllegalArgumentException("Invalid Management Key [" + managementKey + "] provided.");
		}
		groupId = items[0];
		artifactId = items[1];
		if (items.length == 3) {
			version = items[2];
		}
	}

	/**
	 * Constructor
	 * 
	 * @param groupId the groupId of the plugin
	 * @param artifactId the artifactId of the plugin
	 */
	public BuildPlugin(String groupId, String artifactId) {
		this.groupId = groupId;
		this.artifactId = artifactId;
		validateSettings();
	}

	/**
	 * Constructor
	 * 
	 * @param groupId the groupId of the plugin
	 * @param artifactId the artifactId of the plugin
	 * @param version the version of the plugin
	 */
	public BuildPlugin(String groupId, String artifactId, String version) {
		this.groupId = groupId;
		this.artifactId = artifactId;
		this.version = version;
		validateSettings();
	}

	/**
	 * Constructor
	 * 
	 * @param groupId the groupId of the plugin
	 * @param artifactId the artifactId of the plugin
	 * @param configuration {@link Map} containing key-value paired configuration of the plugin
	 */
	public BuildPlugin(String groupId, String artifactId, Map configuration) {
		this.groupId = groupId;
		this.artifactId = artifactId;
		this.configuration = configuration;
		validateSettings();
	}

	/**
	 * Constructor
	 * 
	 * @param groupId the groupId of the plugin
	 * @param artifactId the artifactId of the plugin
	 * @param version the version of the plugin
	 * @param configuration {@link Map} containing key-value paired configuration of the plugin
	 */
	public BuildPlugin(String groupId, String artifactId, String version, Map configuration) {
		this.groupId = groupId;
		this.artifactId = artifactId;
		this.version = version;
		this.configuration = configuration;
		validateSettings();
	}

	/**
	 * Validate the Settings of the {@link BuildPlugin}
	 */
	private void validateSettings() {
		if (StringUtils.isEmpty(this.groupId) || StringUtils.isEmpty(this.artifactId)) {
			throw new IllegalArgumentException("BuildPlugin groupId and artifactId may not be null or empty");
		}
		if (configuration != null) {
			validateConfiguration(configuration);
		}
	}

	/**
	 * Validate a configuration {@link Map}
	 * 
	 * @param conf the configuration {@link Map} to validate
	 */
	private void validateConfiguration(Map conf) {
		for (final Iterator iter = conf.entrySet().iterator(); iter.hasNext();) {
			final Entry entry = (Entry) iter.next();
			if (!(entry.getKey() instanceof String) || entry.getKey().toString().contains("=")) {
				throw new IllegalArgumentException(
					"Configuration keys may only be of type String and may not contain the character '='");
			}
			if (entry.getValue() instanceof List) {
				final List list = (List) entry.getValue();
				for (final Iterator i = list.iterator(); i.hasNext();) {
					if (!(i.next() instanceof String)) {
						throw new IllegalArgumentException("List may only contain String values");
					}
				}
			} else if (!(entry.getValue() instanceof String)) {
				throw new IllegalArgumentException(
					"Configuration value may only be of type String or List containing Strings");
			}
		}
	}

	/**
	 * Gets the groupIt
	 * 
	 * @return the groupId
	 */
	public String getGroupId() {
		return groupId;
	}

	/**
	 * Gets the artifactId
	 * 
	 * @return the artifactId
	 */
	public String getArtifactId() {
		return artifactId;
	}

	/**
	 * Gets the version
	 * 
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * Gets the configuration
	 * 
	 * @return the configuration
	 */
	public Map getConfiguration() {
		if (configuration == null) {
			configuration = new HashMap();
		}
		return configuration;
	}

	/**
	 * Sets the configuration
	 * 
	 * @param configuration the configuration to set
	 */
	public void setConfiguration(Map configuration) {
		validateConfiguration(configuration);
		this.configuration = configuration;
	}

	/**
	 * Sets the configuration from a {@link String} formatted by {@link #getConfigurationAsString()}
	 * 
	 * @param configuration the configuration {@link String} to parse
	 */
	public void setConfiguration(String configuration) {
		if (configuration.startsWith("[") && configuration.endsWith("]")) {
			final Map config = new HashMap();
			int startPos = 0, endPos = 0;
			startPos = configuration.indexOf('[', startPos) + 1;
			endPos = configuration.indexOf(']', endPos);
			while (startPos > -1 && endPos > -1) {
				final String item = configuration.substring(startPos, endPos);
				final String key = item.substring(0, item.indexOf('='));
				final String value = item.substring(item.indexOf('=') + 1);
				if (value.contains(ListUtils.LIST_ELEMENT_SEPARATOR)) {
					config.put(key, ListUtils.toList(value));
				} else {
					config.put(key, value);
				}
				startPos = configuration.indexOf('[', startPos) + 1;
				endPos = configuration.indexOf(']', endPos + 1);
			}
			setConfiguration(config);
		} else {
			throw new IllegalArgumentException(
				"The configuration stirng must be formetted like: [some.key=some.value][some.list.key=item1;item2]");
		}
	}

	/**
	 * Adds a configuration item to the configuration Map
	 * 
	 * @param key the name of the configuration item
	 * @param value the value of the item, can be a {@link String} or a {@link List} containing key-value paired
	 *            configuration items
	 */
	public void addConfigurationItem(String key, Object value) {
		if (value instanceof String) {
			getConfiguration().put(key, String.valueOf(value));
		} else if (value instanceof List) {
			getConfiguration().put(key, (List) value);
		} else {
			throw new IllegalArgumentException("Only Strings and Lists are supported as Configuration item values");
		}
	}

	/**
	 * Get the Management key of the Build Plugin
	 * 
	 * @return the management key format: groupId:artifactId[:version]
	 */
	public final String getManagementKey() {
		String key = getKey();
		if (StringUtils.isNotEmpty(getVersion())) {
			key += ":" + getVersion();
		}
		return key;
	}

	/**
	 * Gets the Key of the {@link BuildPlugin}
	 * 
	 * @return the key in format groupId:artifactId
	 */
	public final String getKey() {
		return groupId + ":" + artifactId;
	}

	/**
	 * Gets the Configuration as String
	 * 
	 * @return the configuration converted to a {@link String}
	 */
	public final String getConfigurationAsString() {
		String config = "";
		for (final Iterator iter = getConfiguration().entrySet().iterator(); iter.hasNext();) {
			final Entry entry = (Entry) iter.next();
			config += "[" + entry.getKey().toString() + "=";
			if (entry.getValue() instanceof List) {
				config += ListUtils.toString((List) entry.getValue());
			} else {
				config += entry.getValue().toString();
			}
			config += "]";
		}
		return config;
	}

	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		return "BuildPlugin {Artifact:" + getManagementKey() + ", Configuration:" + getConfigurationAsString() + "}";
	}

	/**
	 * {@inheritDoc}
	 */
	public int hashCode() {
		return getKey().hashCode();
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object other) {
		if (other instanceof BuildPlugin) {
			final BuildPlugin otherPlugin = (BuildPlugin) other;
			return (getKey().equals(otherPlugin.getKey()));
		}
		return false;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy