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

provide better error message for prepare feature #415

Merged
merged 6 commits into from
Sep 22, 2023
Merged
Changes from 2 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 @@ -34,11 +34,13 @@

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public abstract class PrepareFeatureUtil extends ServerFeatureUtil {

Expand All @@ -53,6 +55,7 @@ public abstract class PrepareFeatureUtil extends ServerFeatureUtil {

private File installJarFile;
private File jsonFile;


public PrepareFeatureUtil(File installDirectory, String openLibertyVersion)
throws PluginScenarioException, PluginExecutionException {
Expand All @@ -79,18 +82,21 @@ public void prepareFeatures(List<String> featureBOMs) throws PluginExecutionExce
String version = coord[2];
File additionalBOM = downloadArtifact(groupId, artifactId, "pom", version);
esaMap.putAll(populateESAMap(additionalBOM));
prepareFeature(groupId, artifactId, version, additionalBOM, esaMap);
if(esaMap.isEmpty()) {
warn("\"The features.json could not be generated because the required feature ESA files were not provided for features-bom at coordinates " + groupId + ":" +artifactId + ":" +version);
}else {
prepareFeature(groupId, artifactId, version, additionalBOM, esaMap);
}
}

}

private Map<File, String> populateESAMap(File additionalBOM) {
Map<File, String> result = new HashMap<File, String>();
try {
result = downloadArtifactsFromBOM(additionalBOM);
result = downloadArtifactsFromBOM(additionalBOM);
} catch (PluginExecutionException e) {
warn(e.getMessage());
warn("A features-bom file must be provided at " + additionalBOM.getAbsolutePath() + ". Please ignore this warning if this is not a user feature.");
warn(e.getMessage());
}

return result;
Expand All @@ -104,57 +110,68 @@ private Map<File, String> populateESAMap(File additionalBOM) {
* @param version
* @throws PluginExecutionException
*/
private void prepareFeature(String groupId, String artifactId, String version, File additionalBOM, Map<File, String> esaMap) throws PluginExecutionException {
try {
String repoLocation = parseRepositoryLocation(additionalBOM, groupId, artifactId, "pom", version);
String targetJsonFile = createArtifactFilePath(repoLocation, groupId, FEATURES_JSON_ARTIFACT_ID, "json",
version);
File generatedJson = generateJson(targetJsonFile, esaMap);
if (generatedJson.exists()) {
jsonFile = generatedJson;
provideJsonFileDependency(generatedJson, groupId, version);
info("The features.json has been generated at the following location: " + generatedJson);
}
} catch (PluginExecutionException e) {
warn(e.getMessage());
}
private void prepareFeature(String groupId, String artifactId, String version, File additionalBOM, Map<File, String> esaMap) {
try {
String repoLocation = parseRepositoryLocation(additionalBOM, groupId, artifactId, "pom", version);
String targetJsonFile = createArtifactFilePath(repoLocation, groupId, FEATURES_JSON_ARTIFACT_ID, "json",
version);
File generatedJson = generateJson(targetJsonFile, esaMap);
if (generatedJson.exists()) {
jsonFile = generatedJson;
provideJsonFileDependency(generatedJson, groupId, version);
info("The features.json has been generated at the following location: " + generatedJson);
jjiwooLim marked this conversation as resolved.
Show resolved Hide resolved
}
cherylking marked this conversation as resolved.
Show resolved Hide resolved

} catch (PluginExecutionException e) {
cherylking marked this conversation as resolved.
Show resolved Hide resolved
warn(e.getMessage());
}
}

/**
* Download the Artifacts mentioned within the additionalBOM pom file
* Download the Artifacts mentioned within the additionalBOM pom file.
* Required artifact properties are "groupId, artifactId, version and type".
* If any of the properties are missing, then it will throw NullPointerException.
*
* @param additionalBOM The BOM file
* @return A map of Files to groupIds
* @throws PluginExecutionException throws error if unable to download the
* artifacts
*/
private Map<File, String> downloadArtifactsFromBOM(File additionalBOM) throws PluginExecutionException {
Map<File, String> result = new HashMap<File, String>();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(additionalBOM);
doc.getDocumentElement().normalize();
NodeList dependencyList = doc.getElementsByTagName("dependency");
for (int itr = 0; itr < dependencyList.getLength(); itr++) {
Node node = dependencyList.item(itr);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
String groupId = eElement.getElementsByTagName("groupId").item(0).getTextContent();
String artifactId = eElement.getElementsByTagName("artifactId").item(0).getTextContent();
String version = eElement.getElementsByTagName("version").item(0).getTextContent();
String type = eElement.getElementsByTagName("type").item(0).getTextContent();

File artifactFile = downloadArtifact(groupId, artifactId, type, version);
result.put(artifactFile, groupId);
}
Map<File, String> result = new HashMap<File, String>();
String[] MavenCoord = {"groupId", "artifactId", "type", "version"};

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(additionalBOM);
doc.getDocumentElement().normalize();
NodeList dependencyList = doc.getElementsByTagName("dependency");
for (int itr = 0; itr < dependencyList.getLength(); itr++) {
Node node = dependencyList.item(itr);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
// error checking
for (String tag : MavenCoord) {
Node tmp_node = eElement.getElementsByTagName(tag).item(0);
if (tmp_node == null) {
throw new PluginExecutionException(
"Error: <" + tag + "> tag nof found in BOM file.");
cherylking marked this conversation as resolved.
Show resolved Hide resolved
}
}
} catch (PluginExecutionException e) { // we were unable to download artifact mentioned in BOM
throw e;
} catch (Exception e) {
throw new PluginExecutionException("Cannot read the BOM file " + additionalBOM.getAbsolutePath(), e);
String groupId = eElement.getElementsByTagName(MavenCoord[0]).item(0).getTextContent();
String artifactId = eElement.getElementsByTagName(MavenCoord[1]).item(0).getTextContent();
String type = eElement.getElementsByTagName(MavenCoord[2]).item(0).getTextContent();
String version = eElement.getElementsByTagName(MavenCoord[3]).item(0).getTextContent();

File artifactFile = downloadArtifact(groupId, artifactId, type, version);
result.put(artifactFile, groupId);
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
debug(e);
throw new PluginExecutionException("Cannot read the BOM file " + additionalBOM.getAbsolutePath() + ". " + e.getMessage());

}
return result;
}

Expand Down Expand Up @@ -291,7 +308,7 @@ private File downloadOverrideJar(String groupId, String artifactId) {
openLibertyVersion + ", " + InstallFeatureUtil.getNextProductVersion(openLibertyVersion)));
} catch (PluginExecutionException e) {
debug("Could not find override bundle " + groupId + ":" + artifactId
+ " for the current Open Liberty version " + openLibertyVersion, e);
+ " for the current Open Liberty version " + openLibertyVersion + e.getMessage());
return null;
}
}
Expand Down