All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.knowledgelinks.cicd.semantic.versioning.config.ConfigDescriptor Maven / Gradle / Ivy
package io.knowledgelinks.cicd.semantic.versioning.config;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.knowledgelinks.cicd.semantic.versioning.enums.ArtifactTypes;
/**
* Private DTO for storing configuration information
*
* @author mstabile75
*
*/
public class ConfigDescriptor {
private static final String ENV_SEPARATOR = "_";
private static final String ENV_HEADER = "SEMANTIC_VERSIONING";
private static final String PROPERTY_HEADER = "semantic.versioning";
private static final String PROPERTY_SEPARATOR = ".";
private String fieldName;
private Class type;
private String defaultValue;
private String stringValue;
private int intValue;
private List artifactTypesValue;
private String description;
private boolean commentOut;
private boolean addHeader;
private static final String format = "%s=%s";
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigDescriptor.class);
public ConfigDescriptor(String envName, Class type, String defaultValue, String description,
boolean commentOut, boolean addHeader) {
this.fieldName = envName;
this.type = type;
this.defaultValue = defaultValue;
setStringValue(defaultValue);
this.description = description;
this.commentOut = commentOut;
this.addHeader = addHeader;
}
/**
* Clones the passed in {@link ConfigDescriptor} in a new instance of the object
*
* @param old the instance to clone
*/
public ConfigDescriptor(ConfigDescriptor old) {
for (Field field : ConfigDescriptor.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
try {
field.set(this, field.get(old));
} catch (IllegalArgumentException | IllegalAccessException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Error setting field, {}", field.getName(), e);
}
}
}
}
}
public String getEnvFileValue() {
String format =
(commentOut) ? String.format("#%s", ConfigDescriptor.format) : ConfigDescriptor.format;
return String.format(format, getEnvName(addHeader), (defaultValue == null) ? "" : defaultValue);
}
public String getPropertiesFileValue() {
String format =
(commentOut) ? String.format("#%s", ConfigDescriptor.format) : ConfigDescriptor.format;
return String.format(format, getPropertyName(), (defaultValue == null) ? "" : defaultValue);
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public Class getType() {
return type;
}
public void setType(Class type) {
this.type = type;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = (stringValue == null || stringValue.isBlank()) ? null : stringValue;
if (type == Integer.class) {
try {
intValue = Integer.parseInt(stringValue);
} catch (NumberFormatException e) {
intValue = 0;
}
} else if (type == List.class) {
artifactTypesValue = (stringValue == null || stringValue.isBlank()) ? null
: Arrays.stream(stringValue.split(","))
.map(item -> ArtifactTypes.valueOf(item.toUpperCase())).collect(Collectors.toList());
}
}
public Object getValue() {
if (type == Integer.class) {
return intValue;
} else if (type == String.class) {
return stringValue;
} else if (type == List.class) {
return artifactTypesValue;
}
return null;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public List getArtifactTypesValue() {
return artifactTypesValue;
}
public void setArtifactTypesValue(List artifactTypesValue) {
this.artifactTypesValue = artifactTypesValue;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isCommentOut() {
return commentOut;
}
public void setCommentOut(boolean commentOut) {
this.commentOut = commentOut;
}
/**
* Returns the environment variable name for the field. Fields are in lower camel case. The
* returned value is the upper case field name separated by underscores with an optional header
* value for the application. - For example: - Field name: thisIsMyField - Environment name:
* SEMANTIC_VERSIONING_THIS_IS_MY_FIELD or THIS_IS_MY_FIELD
*
* @param withHeader true returns the name with the header prepended
* @return the environment name
*/
public String getEnvName(boolean withHeader) {
return ConfigDescriptor.convertCamelCaseToEnvFormat(fieldName, withHeader);
}
/**
* Converts a camelCase string into an environment variable format
*
* @param camelCaseName the name to convert
* @param withHeader true returns the name with the header prepended
* @return the environment variable name
*/
public static String convertCamelCaseToEnvFormat(String camelCaseName, boolean withHeader) {
String[] underscoreParts = camelCaseName.split(ENV_SEPARATOR);
List convertedParts = Arrays.stream(underscoreParts)
.map(item -> (item.toUpperCase().equals(item)) ? item
: String.join(ENV_SEPARATOR, item.split("(?=\\p{Upper})")))
.collect(Collectors.toList());
String envNonHeaderName = String.join(ENV_SEPARATOR, convertedParts).toUpperCase();
if (withHeader) {
return String.format("%s%s%s", ENV_HEADER, ENV_SEPARATOR, envNonHeaderName);
}
return envNonHeaderName;
}
/**
* Returns the properties file/system properties name for the field. Fields are in lower camel
* case. The returned value is the lower case field name separated by periods with a header value
* for the application. - For example: - Field name: thisIsMyField - Property name:
* semantic.versioning.this.is.my.field
*
* @return the property name
*/
public String getPropertyName() {
String[] parts = fieldName.split("(?=\\p{Upper})");
return String.format("%s%s%s", PROPERTY_HEADER, PROPERTY_SEPARATOR,
String.join(PROPERTY_SEPARATOR, parts).toLowerCase());
}
public String getOptions() {
if (type == List.class) {
return String.format("[%s]", String.join(", ", Arrays.stream(ArtifactTypes.values())
.map(item -> item.name()).collect(Collectors.toList())));
}
return null;
}
}