Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1696: Fixed refactoring #1695

Draft
wants to merge 19 commits into
base: config-reader-refactoring
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,18 @@ public static int createUpdateTemplatesDialog() {
*/
public static String downloadJar(boolean isDownloadSource) throws MalformedURLException, IOException {

String fileName = "";

ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
progressMonitor.open();
progressMonitor.getProgressMonitor().beginTask("downloading latest templates...", 0);

File templatesDirectory = getTemplatesDirectory();
try {
fileName = TemplatesJarUtil.downloadLatestDevon4jTemplates(isDownloadSource, templatesDirectory);
TemplatesJarUtil.downloadLatestDevon4jTemplates(isDownloadSource, templatesDirectory);
} finally {
progressMonitor.close();
}

return fileName;
return templatesDirectory.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public static String downloadJarFromURL(String downloadURL, Path templateSetDire
/**
* Downloads the latest devon4j templates
*
* @param isDownloadSource true if downloading source jar file
* @param isDownloadSource true if downloading source jar file
* @param templatesDirectory directory where the templates jar are located
*/
public static void downloadLatestDevon4jTemplates(boolean isDownloadSource, File templatesDirectory) {

downloadJar(TemplatesJarConstants.DEVON4J_TEMPLATES_GROUPID,
TemplatesJarConstants.DEVON4J_TEMPLATES_ARTIFACTID, "LATEST", isDownloadSource, templatesDirectory);
downloadJar(TemplatesJarConstants.DEVON4J_TEMPLATES_GROUPID, TemplatesJarConstants.DEVON4J_TEMPLATES_ARTIFACTID,
"LATEST", isDownloadSource, templatesDirectory);
}

/**
Expand Down Expand Up @@ -201,7 +201,7 @@ private static Set<MavenCoordinate> getMatchingTemplates(List<MavenCoordinate> m
HashSet<MavenCoordinate> existingTemplates = new HashSet<>();

for (MavenCoordinate mavenCoordinate : mavenCoordinates) {
try (Stream<Path> directory = Files.list(path)){
try (Stream<Path> directory = Files.list(path)) {
if (directory.anyMatch(f -> (f.getFileName().toString().contains(mavenCoordinate.getArtifactId())))) {
existingTemplates.add(mavenCoordinate);
}
Expand Down Expand Up @@ -317,7 +317,7 @@ public static List<Path> getJarFiles(Path templatesDirectory) {
ArrayList<Path> jarPaths = new ArrayList<>();
try (Stream<Path> files = Files.list(templatesDirectory)) {
files.forEach(path -> {
if (path.endsWith(".jar")) {
if (path.toString().endsWith(".jar")) {
jarPaths.add(path);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import java.io.File;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader;

/**
* The {@link ContextConfiguration} is a configuration data wrapper for all information about templates and the target
Expand All @@ -23,18 +22,18 @@ public class ContextConfiguration {
/**
* All available {@link Trigger}s
*/
private Map<String, Trigger> triggers;
private Map<String, Trigger> triggers = new HashMap<>();

/**
* Path of the configuration. Might point to a folder or a jar or maybe even something different in future.
*/
private Path configurationPath;


/**
* Constructor needed only for {@link com.devonfw.cobigen.impl.config.reader.ContextConfigurationCollector}
*/
public ContextConfiguration() {

}

/**
Expand All @@ -43,7 +42,8 @@ public ContextConfiguration() {
* @param configRoot root path for the configuration of CobiGen
* @throws InvalidConfigurationException thrown if the {@link File} is not valid with respect to the context.xsd
*/
public ContextConfiguration(BigDecimal version, Map<String, Trigger> triggers, Path configRoot) throws InvalidConfigurationException {
public ContextConfiguration(BigDecimal version, Map<String, Trigger> triggers, Path configRoot)
throws InvalidConfigurationException {

this.version = version;
this.configurationPath = configRoot;
Expand Down Expand Up @@ -81,10 +81,17 @@ public Trigger getTrigger(String id) {

/**
* Merges another context configuration into _this_ context configuration instance
*
* @param contextConfiguration to be merged
*/
public ContextConfiguration merge(ContextConfiguration contextConfiguration) {
triggers.putAll(contextConfiguration.triggers);

List<Trigger> contextTriggers = contextConfiguration.getTriggers();
Map<String, Trigger> triggerMap = new HashMap<>();
for (Trigger trigger : contextTriggers) {
triggerMap.put(trigger.getId(), trigger);
}
triggers.putAll(triggerMap);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ public enum ContextConfigurationVersion implements ConfigurationVersionEnum {
* <li>added links and tags, made templateFolder optional</li>
* </ul>
*/
v3_0(3.0f, false);
v3_0(3.0f, false),

/**
* ChangeLog:
* <ul>
* <li>added links and tags, made templateFolder optional</li>
* </ul>
*/
v6_0(6.0f, false);

/** Comparable float representation of the version number. */
private float floatRepresentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ public enum TemplatesConfigurationVersion implements ConfigurationVersionEnum {
* <li>added explanation attribute to increments</li>
* </ul>
*/
v5_0(5.0f, true);
v5_0(5.0f, true),

/**
* ChangeLog:
* <ul>
* <li>added explanation attribute to increments</li>
* </ul>
*/
v6_0(6.0f, true);

/** Comparable float representation of the version number. */
private float floatRepresentation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package com.devonfw.cobigen.impl.config.reader;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.util.ExceptionUtil;
import com.devonfw.cobigen.api.util.JvmUtil;
import com.devonfw.cobigen.impl.config.constant.ConfigurationVersionEnum;
import com.devonfw.cobigen.impl.config.constant.MavenMetadata;
import com.devonfw.cobigen.impl.config.versioning.VersionValidator;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.UnmarshalException;
import jakarta.xml.bind.Unmarshaller;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class JaxbDeserializer {

/**
* Reads the templates configuration.
*/
<R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deserializeTo, Class<E> versionEnum, String rootNodeName) {
<R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deserializeTo, Class<E> versionEnum,
String rootNodeName) {

// workaround to make JAXB work in OSGi context by
// https://github.com/ControlSystemStudio/cs-studio/issues/2530#issuecomment-450991188
Expand All @@ -48,14 +52,15 @@ <R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deseri
BigDecimal configVersion = (BigDecimal) rootNode.getClass().getDeclaredMethod("getVersion").invoke(rootNode);
if (configVersion == null) {
throw new InvalidConfigurationException(file.toUri().toString(),
"The required 'version' attribute of node \""+ rootNodeName + "\" has not been set");
"The required 'version' attribute of node \"" + rootNodeName + "\" has not been set");
} else {
VersionValidator validator = new VersionValidator(VersionValidator.Type.TEMPLATES_CONFIGURATION, MavenMetadata.VERSION);
VersionValidator validator = new VersionValidator(VersionValidator.Type.TEMPLATES_CONFIGURATION,
MavenMetadata.VERSION);
validator.validate(configVersion.floatValue());
}
} else {
throw new InvalidConfigurationException(file.toUri().toString(),
"Unknown Root Node. Use \""+ rootNodeName + "\" as root Node");
"Unknown Root Node. Use \"" + rootNodeName + "\" as root Node");
}

// If we reach this point, the configuration version and root node has been validated.
Expand All @@ -64,12 +69,10 @@ <R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deseri
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

E latestConfigurationVersion = versionEnum.cast(versionEnum.getDeclaredMethod("getLatest").invoke(null));
try (
InputStream schemaStream = getClass()
.getResourceAsStream("/schema/" + latestConfigurationVersion + "/"+rootNodeName+".xsd");
InputStream configInputStream = Files.newInputStream(file)) {
URL schemaStream = getClass().getResource("/schema/" + latestConfigurationVersion + "/" + rootNodeName + ".xsd");
try (InputStream configInputStream = Files.newInputStream(file)) {

Schema schema = schemaFactory.newSchema(new StreamSource(schemaStream));
Schema schema = schemaFactory.newSchema(schemaStream);
unmarschaller.setSchema(schema);
rootNode = unmarschaller.unmarshal(configInputStream);
return deserializeTo.cast(rootNode);
Expand All @@ -82,16 +85,16 @@ <R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deseri
message = parseCause.getMessage();
}
throw new InvalidConfigurationException(file.toUri().toString(),
"Could not parse configuration file:\n" + message, e);
"Could not parse configuration file:\n" + message, e);
} catch (SAXException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// Should never occur. Programming error.
throw new IllegalStateException("Could not parse configuration schema. Please state this as a bug.");
throw new IllegalStateException("Could not parse configuration schema. Please state this as a bug.", e);
} catch (NumberFormatException e) {
// The version number is currently the only xml value which will be parsed to a number data type
// So provide help
throw new InvalidConfigurationException(file.toUri().toString(),
"Invalid version number defined. The version of the configuration should consist of 'major.minor' version.",
e);
"Invalid version number defined. The version of the configuration should consist of 'major.minor' version.",
e);
} catch (IOException e) {
throw new InvalidConfigurationException(file.toUri().toString(), "Could not read configuration file.", e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.devonfw.cobigen.impl.config.reader;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.impl.config.constant.TemplatesConfigurationVersion;
import com.devonfw.cobigen.impl.config.entity.TemplateSet;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration;
import com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration;
import com.devonfw.cobigen.impl.config.entity.io.TemplatesConfiguration;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import com.devonfw.cobigen.impl.util.FileSystemUtil;

/**
* The {@link TemplateSetReader} combines everything from the {@link TemplatesConfigurationReader} and
Expand All @@ -19,12 +19,14 @@
public class TemplateSetReader extends JaxbDeserializer {

private final Path templateSetFile;

/**
* List with the paths of the configuration locations for the template-set.xml files
*/
// TODO: Check if this map can replace templateSetFile and configLocation, see:
// https://github.com/devonfw/cobigen/issues/1668
private Map<Path, Path> configLocations = new HashMap<>();

private ContextConfiguration contextConfiguration;

private com.devonfw.cobigen.impl.config.ContextConfiguration contextConfigurationBo;
Expand All @@ -33,17 +35,25 @@ public class TemplateSetReader extends JaxbDeserializer {

private final ConfigurationReader configurationReader;


/**
* The TemplateSetConfigurationManager manages adapted and downloaded template sets
* <p>
* TODO: Check if it can be integrated into this reader, see: https://github.com/devonfw/cobigen/issues/1668
*/
// private final TemplateSetConfigurationManager templateSetConfigurationManager = new TemplateSetConfigurationManager();
// private final TemplateSetConfigurationManager templateSetConfigurationManager = new
// TemplateSetConfigurationManager();

public TemplateSetReader(Path rootDir, ConfigurationReader configurationReader) {

if (rootDir.toString().endsWith(".jar")) {
this.templateSetFile = FileSystemUtil.createFileSystemDependentPath(rootDir.toUri())
.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME);
} else {
this.templateSetFile = FileSystemUtil.createFileSystemDependentPath(rootDir.toUri())
.resolve(ConfigurationConstants.MAVEN_CONFIGURATION_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME);
}

TemplateSetReader(Path rootDir, ConfigurationReader configurationReader) {
this.templateSetFile = rootDir.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME);
this.configurationReader = configurationReader;
deserializeConfigFile();
}
Expand All @@ -53,19 +63,24 @@ public class TemplateSetReader extends JaxbDeserializer {
*/
private void deserializeConfigFile() {

TemplateSetConfiguration templateSetConfiguration = deserialize(templateSetFile, com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration.class, TemplatesConfigurationVersion.class, "templateSetConfiguration");
TemplateSetConfiguration templateSetConfiguration = deserialize(templateSetFile,
com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration.class, TemplatesConfigurationVersion.class,
"templateSetConfiguration");
contextConfiguration = templateSetConfiguration.getContextConfiguration();
templatesConfiguration = templateSetConfiguration.getTemplatesConfiguration();
}

public com.devonfw.cobigen.impl.config.ContextConfiguration readContextConfiguration() {
if(contextConfigurationBo == null) {

if (contextConfigurationBo == null) {
contextConfigurationBo = new ContextConfigurationReader(contextConfiguration, templateSetFile).read();
}
return contextConfigurationBo;
}

public com.devonfw.cobigen.impl.config.TemplatesConfiguration readTemplatesConfiguration(Trigger trigger) {
return new TemplatesConfigurationReader(templatesConfiguration, templateSetFile.getParent(), configurationReader, templateSetFile).read(trigger);

return new TemplatesConfigurationReader(templatesConfiguration, templateSetFile.getParent(), configurationReader,
templateSetFile).read(trigger);
}
}
Loading
Loading