Search in sources :

Example 1 with VersionValidator

use of com.devonfw.cobigen.impl.config.versioning.VersionValidator in project cobigen by devonfw.

the class ContextConfigurationReader method readConfiguration.

/**
 * Reads the context configuration.
 */
private void readConfiguration() {
    // workaround to make JAXB work in OSGi context by
    // https://github.com/ControlSystemStudio/cs-studio/issues/2530#issuecomment-450991188
    final ClassLoader orig = Thread.currentThread().getContextClassLoader();
    if (JvmUtil.isRunningJava9OrLater()) {
        Thread.currentThread().setContextClassLoader(JAXBContext.class.getClassLoader());
    }
    this.contextConfigurations = new HashMap<>();
    for (Path contextFile : this.contextFiles) {
        try (InputStream in = Files.newInputStream(contextFile)) {
            Unmarshaller unmarschaller = JAXBContext.newInstance(ContextConfiguration.class).createUnmarshaller();
            // Unmarshal without schema checks for getting the version attribute of the root node.
            // This is necessary to provide an automatic upgrade client later on
            Object rootNode = unmarschaller.unmarshal(in);
            if (rootNode instanceof ContextConfiguration) {
                BigDecimal configVersion = ((ContextConfiguration) rootNode).getVersion();
                if (configVersion == null) {
                    throw new InvalidConfigurationException(contextFile, "The required 'version' attribute of node \"contextConfiguration\" has not been set");
                } else {
                    VersionValidator validator = new VersionValidator(Type.CONTEXT_CONFIGURATION, MavenMetadata.VERSION);
                    validator.validate(configVersion.floatValue());
                }
            } else {
                throw new InvalidConfigurationException(contextFile, "Unknown Root Node. Use \"contextConfiguration\" as root Node");
            }
            // If we reach this point, the configuration version and root node has been validated.
            // Unmarshal with schema checks for checking the correctness and give the user more hints to
            // correct his failures
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            ContextConfigurationVersion latestConfigurationVersion = ContextConfigurationVersion.getLatest();
            try (InputStream schemaStream = getClass().getResourceAsStream("/schema/" + latestConfigurationVersion + "/contextConfiguration.xsd");
                InputStream configInputStream = Files.newInputStream(contextFile)) {
                Schema schema = schemaFactory.newSchema(new StreamSource(schemaStream));
                unmarschaller.setSchema(schema);
                rootNode = unmarschaller.unmarshal(configInputStream);
                this.contextConfigurations.put(contextFile, (ContextConfiguration) rootNode);
            }
        } catch (JAXBException e) {
            // try getting SAXParseException for better error handling and user support
            Throwable parseCause = ExceptionUtil.getCause(e, SAXParseException.class, UnmarshalException.class);
            String message = "";
            if (parseCause != null && parseCause.getMessage() != null) {
                message = parseCause.getMessage();
            }
            throw new InvalidConfigurationException(contextFile, "Could not parse configuration file:\n" + message, e);
        } catch (SAXException e) {
            // Should never occur. Programming error.
            throw new IllegalStateException("Could not parse context configuration schema. Please state this as a bug.");
        } catch (NumberFormatException e) {
            // So provide help
            throw new InvalidConfigurationException("Invalid version number defined. The version of the context configuration should consist of 'major.minor' version.");
        } catch (IOException e) {
            throw new InvalidConfigurationException(contextFile, "Could not read context configuration file.", e);
        } finally {
            Thread.currentThread().setContextClassLoader(orig);
        }
    }
}
Also used : Path(java.nio.file.Path) SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) VersionValidator(com.devonfw.cobigen.impl.config.versioning.VersionValidator) JAXBContext(jakarta.xml.bind.JAXBContext) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) UnmarshalException(jakarta.xml.bind.UnmarshalException) Unmarshaller(jakarta.xml.bind.Unmarshaller) ContextConfiguration(com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration) ContextConfigurationVersion(com.devonfw.cobigen.impl.config.constant.ContextConfigurationVersion)

Example 2 with VersionValidator

use of com.devonfw.cobigen.impl.config.versioning.VersionValidator in project cobigen by devonfw.

the class TemplatesConfigurationReader method readConfiguration.

/**
 * Reads the templates configuration.
 */
private void readConfiguration() {
    // workaround to make JAXB work in OSGi context by
    // https://github.com/ControlSystemStudio/cs-studio/issues/2530#issuecomment-450991188
    final ClassLoader orig = Thread.currentThread().getContextClassLoader();
    if (JvmUtil.isRunningJava9OrLater()) {
        Thread.currentThread().setContextClassLoader(JAXBContext.class.getClassLoader());
    }
    try (InputStream in = Files.newInputStream(this.configFilePath)) {
        Unmarshaller unmarschaller = JAXBContext.newInstance(TemplatesConfiguration.class).createUnmarshaller();
        // Unmarshal without schema checks for getting the version attribute of the root node.
        // This is necessary to provide an automatic upgrade client later on
        Object rootNode = unmarschaller.unmarshal(in);
        if (rootNode instanceof TemplatesConfiguration) {
            BigDecimal configVersion = ((TemplatesConfiguration) rootNode).getVersion();
            if (configVersion == null) {
                throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "The required 'version' attribute of node \"templatesConfiguration\" has not been set");
            } else {
                VersionValidator validator = new VersionValidator(Type.TEMPLATES_CONFIGURATION, MavenMetadata.VERSION);
                validator.validate(configVersion.floatValue());
            }
        } else {
            throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Unknown Root Node. Use \"templatesConfiguration\" as root Node");
        }
        // If we reach this point, the configuration version and root node has been validated.
        // Unmarshal with schema checks for checking the correctness and give the user more hints to
        // correct his failures
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        TemplatesConfigurationVersion latestConfigurationVersion = TemplatesConfigurationVersion.getLatest();
        try (InputStream schemaStream = getClass().getResourceAsStream("/schema/" + latestConfigurationVersion + "/templatesConfiguration.xsd");
            InputStream configInputStream = Files.newInputStream(this.configFilePath)) {
            Schema schema = schemaFactory.newSchema(new StreamSource(schemaStream));
            unmarschaller.setSchema(schema);
            rootNode = unmarschaller.unmarshal(configInputStream);
            this.configNode = (TemplatesConfiguration) rootNode;
        }
    } catch (JAXBException e) {
        // try getting SAXParseException for better error handling and user support
        Throwable parseCause = ExceptionUtil.getCause(e, SAXParseException.class, UnmarshalException.class);
        String message = "";
        if (parseCause != null && parseCause.getMessage() != null) {
            message = parseCause.getMessage();
        }
        throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Could not parse configuration file:\n" + message, e);
    } catch (SAXException e) {
        // Should never occur. Programming error.
        throw new IllegalStateException("Could not parse templates configuration schema. Please state this as a bug.");
    } catch (NumberFormatException e) {
        // So provide help
        throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Invalid version number defined. The version of the templates configuration should consist of 'major.minor' version.", e);
    } catch (IOException e) {
        throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Could not read templates configuration file.", e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) VersionValidator(com.devonfw.cobigen.impl.config.versioning.VersionValidator) JAXBContext(jakarta.xml.bind.JAXBContext) IOException(java.io.IOException) TemplatesConfiguration(com.devonfw.cobigen.impl.config.entity.io.TemplatesConfiguration) BigDecimal(java.math.BigDecimal) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) UnmarshalException(jakarta.xml.bind.UnmarshalException) Unmarshaller(jakarta.xml.bind.Unmarshaller) TemplatesConfigurationVersion(com.devonfw.cobigen.impl.config.constant.TemplatesConfigurationVersion)

Example 3 with VersionValidator

use of com.devonfw.cobigen.impl.config.versioning.VersionValidator in project cobigen by devonfw.

the class VersionValidatorTest method testValidCobiGenVersion_higherButNonBreaking_templatesConfiguration.

/**
 * Testing the CobiGen version with higher version number than configuration. However, there is no breaking change in
 * between.
 */
@Test
public void testValidCobiGenVersion_higherButNonBreaking_templatesConfiguration() {
    VersionValidator validator = new VersionValidator(Type.TEMPLATES_CONFIGURATION, "1.5.0");
    validator.validate(1.2f);
}
Also used : VersionValidator(com.devonfw.cobigen.impl.config.versioning.VersionValidator) Test(org.junit.Test)

Example 4 with VersionValidator

use of com.devonfw.cobigen.impl.config.versioning.VersionValidator in project cobigen by devonfw.

the class VersionValidatorTest method testInvalidCobiGenVersion_tooOld_contextConfiguration.

/**
 * Testing the CobiGen version to be too old for the provided configuration version, which indicates to make an update
 * of CobiGen to read the configuration properly.
 *
 * @author mbrunnli (May 17, 2016)
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidCobiGenVersion_tooOld_contextConfiguration() {
    try {
        VersionValidator validator = new VersionValidator(Type.CONTEXT_CONFIGURATION, "2.0.2");
        validator.validate(2.1f);
    } catch (InvalidConfigurationException e) {
        assertThat(e.getMessage()).matches(".* version '2.1' .* context configuration is unknown .* CobiGen '2.0'.*");
        throw e;
    }
}
Also used : VersionValidator(com.devonfw.cobigen.impl.config.versioning.VersionValidator) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 5 with VersionValidator

use of com.devonfw.cobigen.impl.config.versioning.VersionValidator in project cobigen by devonfw.

the class VersionValidatorTest method testInvalidCobiGenVersion_tooNew_contextConfiguration.

/**
 * Testing the CobiGen version to be too new for the provided configuration version, which indicates to make an update
 * of the configuration to read it properly.
 *
 * @author mbrunnli (May 17, 2016)
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidCobiGenVersion_tooNew_contextConfiguration() {
    try {
        VersionValidator validator = new VersionValidator(Type.CONTEXT_CONFIGURATION, "2.1.0");
        validator.validate(1.2f);
    } catch (InvalidConfigurationException e) {
        assertThat(e.getMessage()).matches(".* version '1.2' has to be upgraded .*");
        throw e;
    }
}
Also used : VersionValidator(com.devonfw.cobigen.impl.config.versioning.VersionValidator) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) Test(org.junit.Test)

Aggregations

VersionValidator (com.devonfw.cobigen.impl.config.versioning.VersionValidator)5 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)4 Test (org.junit.Test)3 JAXBContext (jakarta.xml.bind.JAXBContext)2 JAXBException (jakarta.xml.bind.JAXBException)2 UnmarshalException (jakarta.xml.bind.UnmarshalException)2 Unmarshaller (jakarta.xml.bind.Unmarshaller)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 BigDecimal (java.math.BigDecimal)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Schema (javax.xml.validation.Schema)2 SchemaFactory (javax.xml.validation.SchemaFactory)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 ContextConfigurationVersion (com.devonfw.cobigen.impl.config.constant.ContextConfigurationVersion)1 TemplatesConfigurationVersion (com.devonfw.cobigen.impl.config.constant.TemplatesConfigurationVersion)1 ContextConfiguration (com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration)1 TemplatesConfiguration (com.devonfw.cobigen.impl.config.entity.io.TemplatesConfiguration)1 Path (java.nio.file.Path)1