Skip to content

Commit

Permalink
chore(jmc-agent): clean up probe template utility (#241) (#242)
Browse files Browse the repository at this point in the history
* chore(template): correct template name for logs

* chore(template): return template when uploading

* fix(template): should truly close the inputstream

(cherry picked from commit 8c1adac)

Co-authored-by: Thuan Vo <thvo@redhat.com>
  • Loading branch information
mergify[bot] and Thuan Vo authored Jun 22, 2023
1 parent 1dfe1c8 commit 1cb41eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,20 @@ public LocalProbeTemplateService(FileSystem fs, Environment env) throws IOExcept
}
}

public void addTemplate(InputStream inputStream, String filename)
public ProbeTemplate addTemplate(InputStream inputStream, String filename)
throws FileAlreadyExistsException, IOException, SAXException {
Path path = fs.pathOf(env.getEnv(TEMPLATE_PATH), filename);
if (fs.exists(path)) {
throw new FileAlreadyExistsException(
String.format("Probe template \"%s\" already exists.", filename));
}
try (inputStream) {
ProbeTemplate template = new ProbeTemplate();
template.setFileName(filename);
// If validation fails this will throw a ProbeValidationException with details
template.deserialize(inputStream);
Path path = fs.pathOf(env.getEnv(TEMPLATE_PATH), filename);
if (fs.exists(path)) {
throw new FileAlreadyExistsException(template.getFileName());
}
fs.writeString(path, template.serialize());
return template;
}
}

Expand All @@ -108,9 +111,10 @@ public void deleteTemplate(String templateName) throws IOException {
}
}

public String getTemplate(String templateName) throws IOException, SAXException {
public String getTemplateContent(String templateName) throws IOException, SAXException {
Path probeTemplatePath = fs.pathOf(env.getEnv(TEMPLATE_PATH), templateName);
ProbeTemplate template = new ProbeTemplate();
template.setFileName(templateName);
template.deserialize(fs.newInputStream(probeTemplatePath));
return template.serialize();
}
Expand All @@ -136,8 +140,8 @@ public List<ProbeTemplate> getTemplates() throws FlightRecorderException {
Path fileName = path.getFileName();
if (fileName != null) {
ProbeTemplate template = new ProbeTemplate();
template.deserialize(stream);
template.setFileName(fileName.toString());
template.deserialize(stream);
templates.add(template);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/cryostat/core/agent/ProbeTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void deserialize(InputStream xmlStream) throws IOException, SAXException
}

Document document = builder.parse(stream);
stream.close();
stream.trulyClose();
NodeList elements;

// parse global configurations
Expand Down

0 comments on commit 1cb41eb

Please sign in to comment.