io.knowledgelinks.cicd.semantic.versioning.formatters.VersionFormatterFactory Maven / Gradle / Ivy
package io.knowledgelinks.cicd.semantic.versioning.formatters;
/*-
* #%L
* Semantic Versioning
* %%
* Copyright (C) 2022 - 2023 Knowledgelinks
* %%
* Licensed 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.
* #L%
*/
import java.lang.reflect.InvocationTargetException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.knowledgelinks.cicd.semantic.versioning.config.Configuration;
import io.knowledgelinks.cicd.semantic.versioning.enums.ArtifactTypes;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.SemanticVersioningException;
public class VersionFormatterFactory {
private static Logger logger = LoggerFactory.getLogger(VersionFormatterFactory.class);
private Configuration config;
public VersionFormatterFactory(Configuration config) throws SemanticVersioningException {
this.config = (config == null) ? Configuration.getInstance() : config;
}
public VersionFormatter get() throws SemanticVersioningException {
return get(config.getDefaultVersionFormatter());
}
public ArtifactFormatter get(ArtifactTypes artifactType) throws SemanticVersioningException {
switch (artifactType) {
case JAR:
return new JavaArtifactFormatter(get(), config, artifactType);
case WHEEL:
break;
case DOCKER:
break;
case TAR:
return new ArchiveArtifactFormatter(get(), config, artifactType);
case TGZ:
return new ArchiveArtifactFormatter(get(), config, artifactType);
case WAR:
return new JavaArtifactFormatter(get(), config, artifactType);
case ZIP:
return new ArchiveArtifactFormatter(get(), config, artifactType);
default:
break;
}
return null;
}
private VersionFormatter get(String versionFormatterClassName)
throws SemanticVersioningException {
Class versionFormatter = getVersionFormatterClass(versionFormatterClassName);
try {
return (VersionFormatter) versionFormatter.getDeclaredConstructor(Configuration.class)
.newInstance(config);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
logger.error("Unablet to get constructor for: {}", versionFormatterClassName, e);
return new StandardVersionFormatter(config);
}
}
private Class getVersionFormatterClass(String versionFormatterName) {
try {
return Class.forName(versionFormatterName);
} catch (ClassNotFoundException e) {
return StandardVersionFormatter.class;
}
}
}