use of com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration 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);
}
}
}
use of com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration in project cobigen by devonfw.
the class ContextConfigurationReader method loadTriggers.
/**
* Loads all {@link Trigger}s of the static context into the local representation
*
* @return a {@link List} containing all the {@link Trigger}s
*/
public Map<String, Trigger> loadTriggers() {
Map<String, Trigger> triggers = Maps.newHashMap();
for (Path contextFile : this.contextConfigurations.keySet()) {
ContextConfiguration contextConfiguration = this.contextConfigurations.get(contextFile);
for (com.devonfw.cobigen.impl.config.entity.io.Trigger t : contextConfiguration.getTrigger()) {
// templateFolder property is optional in schema version 2.2. If not set take the path of the context.xml file
String templateFolder = t.getTemplateFolder();
if (templateFolder.isEmpty() || templateFolder.equals("/")) {
templateFolder = contextFile.getParent().getFileName().toString();
}
triggers.put(t.getId(), new Trigger(t.getId(), t.getType(), templateFolder, Charset.forName(t.getInputCharset()), loadMatchers(t), loadContainerMatchers(t)));
}
}
return triggers;
}
Aggregations