use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.
the class HealthCheckImpl method upgradeAllConfigurations.
@Override
public HealthCheckReport upgradeAllConfigurations(Path contextConfigurationPath, BackupPolicy backupPolicy) {
try {
upgradeContextConfiguration(contextConfigurationPath, backupPolicy);
} catch (BackupFailedException e) {
upgradeContextConfiguration(contextConfigurationPath, BackupPolicy.NO_BACKUP);
}
ContextConfiguration contextConfiguration = new ContextConfiguration(contextConfigurationPath);
List<String> expectedTemplatesConfigurations = new ArrayList<>();
Set<String> hasConfiguration = Sets.newHashSet();
Map<String, Path> upgradeableConfigurations = this.healthCheckReport.getUpgradeableConfigurations();
for (Trigger t : contextConfiguration.getTriggers()) {
expectedTemplatesConfigurations.add(t.getTemplateFolder());
hasConfiguration.add(t.getTemplateFolder());
}
this.healthCheckReport.setHasConfiguration(hasConfiguration);
upgradeableConfigurations.put("TempOne", contextConfigurationPath.resolve("TempOne"));
this.healthCheckReport.setUpgradeableConfigurations(upgradeableConfigurations);
if (expectedTemplatesConfigurations.containsAll(this.healthCheckReport.getHasConfiguration())) {
for (final String key : expectedTemplatesConfigurations) {
if (this.healthCheckReport.getUpgradeableConfigurations().containsKey(key)) {
upgradeTemplatesConfiguration(this.healthCheckReport.getUpgradeableConfigurations().get(key), backupPolicy);
}
}
} else {
LOG.error("Expected template configuration does not equal the actual template configuration");
throw new CobiGenRuntimeException("Update of the templates configuration was not successful, please retry");
}
return this.healthCheckReport;
}
use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.
the class ModelBuilderImpl method enrichByContextVariables.
/**
* Enriches the model by the context variables of the trigger.
*
* @param model to be enriched
* @param triggerInterpreter {@link TriggerInterpreter} to resolve the variables
* @param template the internal {@link Template} representation
* @param targetRootPath root path template destinations should be resolved against
* @param report is getting filled as side-effect
* @return the adapted model reference.
*/
public Map<String, Object> enrichByContextVariables(Map<String, Object> model, TriggerInterpreter triggerInterpreter, Template template, Path targetRootPath, GenerationReportTo report) {
Map<String, String> variables = Maps.newHashMap();
Map<String, String> contextVariables = new ContextVariableResolver(this.generatorInput, this.trigger).resolveVariables(triggerInterpreter, report).asMap();
Map<String, String> templateProperties = template.getVariables().asMap();
Properties targetCobiGenProperties = CobiGenPropertiesReader.load(targetRootPath);
// if there are properties overriding each other, throw an exception for better usability.
// This is most probably a not intended mechanism such that we simply will not support it.
Set<String> intersection = new HashSet<>(contextVariables.keySet());
intersection.retainAll(templateProperties.keySet());
Set<String> intersection2 = new HashSet<>(contextVariables.keySet());
intersection2.retainAll(targetCobiGenProperties.keySet());
if (!intersection.isEmpty() || !intersection2.isEmpty()) {
throw new CobiGenRuntimeException("There are conflicting variables coming from the context configuration " + "as well as coming from the " + ConfigurationConstants.COBIGEN_PROPERTIES + " file. " + "This is most probably an unintended behavior and thus is not supported. The following variables are " + "declared twice (once in " + ConfigurationConstants.CONTEXT_CONFIG_FILENAME + " and once in " + ConfigurationConstants.COBIGEN_PROPERTIES + " file): " + Arrays.toString(intersection.toArray()));
}
variables.putAll(contextVariables);
variables.putAll(templateProperties);
variables.putAll(new Variables(targetCobiGenProperties).asMap());
model.put(NS_VARIABLES, variables);
return model;
}
use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.
the class UpdateCommand method extractVersionFragments.
/**
* Match and extract the version fragments separated by dot
*
* @param version string
* @return the version fragments split by dot and ignoring any additional value after dash
*/
private String[] extractVersionFragments(String version) {
Matcher lclMatcher = VERSION_PATTERN.matcher(version);
if (!lclMatcher.find()) {
throw new CobiGenRuntimeException("unable to match version " + version + " against version pattern " + VERSION_PATTERN.pattern());
}
String[] localVersion = lclMatcher.group(1).split("\\.");
return localVersion;
}
use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.
the class BeanFactory method initializeAndSetFields.
/**
* Sets the fields annotated with {@link Inject} of the new instance. On demand creates new beans.
*
* @param newInstance to be initialized
*/
private void initializeAndSetFields(Object newInstance) {
for (Field field : newInstance.getClass().getDeclaredFields()) {
if (field.getAnnotationsByType(Inject.class).length > 0) {
Class<?> fieldType = field.getType();
Object fieldInstance = createBean(fieldType);
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
try {
field.set(newInstance, fieldInstance);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new CobiGenRuntimeException("Failure on setting field " + field.getName() + " of class " + newInstance.getClass().getCanonicalName(), e);
}
// restore config
field.setAccessible(accessible);
}
}
}
use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.
the class MavenUtil method createCachedPomFromJar.
/**
* Generates a cached-pom.xml file from a jar archive, automatically deletes the cached-pom.xml on exit
*
* @param pomFile to cache
* @param outputPath output directory
* @return the cached-pom.xml file
*/
public static Path createCachedPomFromJar(Path pomFile, Path outputPath) {
Path cachedPomXml = outputPath.resolve("cached-pom.xml");
try {
Files.copy(pomFile, cachedPomXml);
} catch (IOException e) {
throw new CobiGenRuntimeException("Unable to extract " + pomFile.toUri() + " from JAR to " + cachedPomXml, e);
}
pomFile = cachedPomXml;
cachedPomXml.toFile().deleteOnExit();
return cachedPomXml;
}
Aggregations