use of com.devonfw.cobigen.api.exception.NotYetSupportedException in project cobigen by devonfw.
the class AbstractConfigurationUpgrader method upgradeConfigurationToLatestVersion.
/**
* Upgrades the configuration to the latest supported version.
*
* @param configurationRoot the root folder containing the configuration with the specified
* {@link #configurationFilename}. See {@link #AbstractConfigurationUpgrader(Enum, Class, String)} for more
* information.
* @param backupPolicy the {@link BackupPolicy} to choose if a backup is necessary or not.
* @return if manual adoptions has to be performed after upgrading
* @throws BackupFailedException if the backup could not be created
*/
public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, BackupPolicy backupPolicy) {
boolean manualAdoptionsNecessary = false;
VERSIONS_TYPE currentVersion = resolveLatestCompatibleSchemaVersion(configurationRoot);
Path configurationFile = configurationRoot.resolve(this.configurationFilename);
if (currentVersion == null) {
throw new InvalidConfigurationException(configurationFile.toUri().toString(), StringUtils.capitalize(this.configurationName) + " does not match any current or legacy schema definitions.");
} else {
VERSIONS_TYPE latestVersion = this.versions[this.versions.length - 1];
// increasing iteration of versions
for (int i = 0; i < this.versions.length; i++) {
if (currentVersion == latestVersion) {
// already up to date
break;
}
if (currentVersion == this.versions[i]) {
LOG.info("Upgrading {} '{}' from version {} to {}...", this.configurationName, configurationFile.toUri(), this.versions[i], this.versions[i + 1]);
Object rootNode;
try {
Class<?> jaxbConfigurationClass = getJaxbConfigurationClass(this.versions[i]);
rootNode = unmarshallConfiguration(configurationFile, this.versions[i], jaxbConfigurationClass);
createBackup(configurationFile, backupPolicy);
// NotYetSupportedException
ConfigurationUpgradeResult result = performNextUpgradeStep(this.versions[i], rootNode);
manualAdoptionsNecessary |= result.areManualAdoptionsNecessary();
try (OutputStream out = Files.newOutputStream(configurationFile)) {
JAXB.marshal(result.getResultConfigurationJaxbRootNode(), out);
}
// implicitly check upgrade step
currentVersion = resolveLatestCompatibleSchemaVersion(configurationRoot);
} catch (NotYetSupportedException | BackupFailedException e) {
throw e;
} catch (Throwable e) {
throw new CobiGenRuntimeException("An unexpected exception occurred while upgrading the " + this.configurationName + " from version '" + this.versions[i] + "' to '" + this.versions[i + 1] + "'.", e);
}
// if CobiGen does not understand the upgraded file... throw exception
if (currentVersion == null) {
throw new CobiGenRuntimeException("An error occurred while upgrading " + this.configurationName + " from version " + this.versions[i] + " to " + this.versions[i + 1] + ".");
}
}
}
}
return manualAdoptionsNecessary;
}
use of com.devonfw.cobigen.api.exception.NotYetSupportedException in project cobigen by devonfw.
the class ContextConfigurationUpgrader method performNextUpgradeStep.
@Override
protected ConfigurationUpgradeResult performNextUpgradeStep(ContextConfigurationVersion source, Object previousConfigurationRootNode) throws Exception {
ConfigurationUpgradeResult result = new ConfigurationUpgradeResult();
MapperFactory mapperFactory;
MapperFacade mapper;
com.devonfw.cobigen.impl.config.entity.io.v2_2.ContextConfiguration upgradedConfig;
switch(source) {
case v2_0:
case v2_1:
mapperFactory = new DefaultMapperFactory.Builder().useAutoMapping(true).mapNulls(true).build();
mapperFactory.classMap(com.devonfw.cobigen.impl.config.entity.io.v2_0.ContextConfiguration.class, com.devonfw.cobigen.impl.config.entity.io.v2_2.ContextConfiguration.class).field("triggers.trigger", "trigger").byDefault().register();
mapperFactory.classMap(com.devonfw.cobigen.impl.config.entity.io.v2_0.ContainerMatcher.class, com.devonfw.cobigen.impl.config.entity.io.v2_2.ContainerMatcher.class).field("retrieveObjectsRecursively:{isRetrieveObjectsRecursively|setRetrieveObjectsRecursively(new Boolean(%s))|type=java.lang.Boolean}", "retrieveObjectsRecursively:{isRetrieveObjectsRecursively|setRetrieveObjectsRecursively(new Boolean(%s))|type=java.lang.Boolean}").byDefault().register();
mapper = mapperFactory.getMapperFacade();
upgradedConfig = mapper.map(previousConfigurationRootNode, com.devonfw.cobigen.impl.config.entity.io.v2_2.ContextConfiguration.class);
upgradedConfig.setVersion(new BigDecimal("2.2"));
result.setResultConfigurationJaxbRootNode(upgradedConfig);
break;
default:
throw new NotYetSupportedException("An upgrade of the context configuration from a version previous to v2.0 is not yet supported.");
}
return result;
}
Aggregations