Search in sources :

Example 16 with CobiGenRuntimeException

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;
}
Also used : Path(java.nio.file.Path) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) BackupFailedException(com.devonfw.cobigen.impl.exceptions.BackupFailedException) ArrayList(java.util.ArrayList) ContextConfiguration(com.devonfw.cobigen.impl.config.ContextConfiguration)

Example 17 with CobiGenRuntimeException

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;
}
Also used : Variables(com.devonfw.cobigen.impl.config.entity.Variables) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) Properties(java.util.Properties) HashSet(java.util.HashSet)

Example 18 with CobiGenRuntimeException

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;
}
Also used : CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) Matcher(java.util.regex.Matcher)

Example 19 with CobiGenRuntimeException

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);
        }
    }
}
Also used : Field(java.lang.reflect.Field) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException)

Example 20 with CobiGenRuntimeException

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;
}
Also used : Path(java.nio.file.Path) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) IOException(java.io.IOException)

Aggregations

CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)43 IOException (java.io.IOException)22 Path (java.nio.file.Path)15 File (java.io.File)8 LoggerFactory (org.slf4j.LoggerFactory)5 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)4 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)4 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)4 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)4 InputStream (java.io.InputStream)4 URL (java.net.URL)4 URLClassLoader (java.net.URLClassLoader)4 CobiGen (com.devonfw.cobigen.api.CobiGen)3 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)3 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)3 BackupFailedException (com.devonfw.cobigen.impl.exceptions.BackupFailedException)3 Paths (java.nio.file.Paths)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Logger (org.slf4j.Logger)3