8279825: JFR: JFCModel shouldn't need FilePermission to read predefined .jfc files

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2022-01-14 14:31:42 +00:00
parent 9f30ec174f
commit e8f494cd5f
4 changed files with 40 additions and 22 deletions

View File

@ -25,9 +25,11 @@
package jdk.jfr.internal.jfc;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
@ -62,9 +64,11 @@ public final class JFC {
private final String content;
private final String filename;
private final String name;
private final SafePath path;
private Configuration configuration;
public KnownConfiguration(SafePath knownPath) throws IOException {
this.path = knownPath;
this.content = readContent(knownPath);
this.name = nameFromPath(knownPath.toPath());
this.filename = nullSafeFileName(knownPath.toPath());
@ -270,4 +274,13 @@ public final class JFC {
}
throw new NoSuchFileException("Could not locate configuration with name " + name);
}
public static Reader newReader(SafePath sf) throws IOException {
for (KnownConfiguration c : getKnownConfigurations()) {
if (c.path.equals(sf)) {
return new StringReader(c.content);
}
}
return new FileReader(sf.toFile(), StandardCharsets.UTF_8);
}
}

View File

@ -26,6 +26,7 @@ package jdk.jfr.internal.jfc.model;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
@ -35,6 +36,7 @@ import java.util.Map;
import java.util.function.Consumer;
import jdk.jfr.internal.SecuritySupport.SafePath;
import jdk.jfr.internal.jfc.JFC;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -43,9 +45,13 @@ public final class JFCModel {
private final Map<String, List<ControlElement>> controls = new LinkedHashMap<>();
private final XmlConfiguration configuration;
public JFCModel(SafePath file, Consumer<String> logger) throws ParseException, IOException {
this.configuration = createConfiguration(file);
this.configuration.validate();
private JFCModel(XmlConfiguration configuration) throws ParseException {
configuration.validate();
this.configuration = configuration;
}
public JFCModel(Reader reader, Consumer<String> logger) throws ParseException, IOException {
this(Parser.parse(reader));
addControls();
wireConditions();
wireSettings(logger);
@ -55,7 +61,7 @@ public final class JFCModel {
this.configuration = new XmlConfiguration();
this.configuration.setAttribute("version", "2.0");
for (SafePath file : files) {
JFCModel model = new JFCModel(file, logger);
JFCModel model = JFCModel.create(file, logger);
for (var entry : model.controls.entrySet()) {
String name = entry.getKey();
// Fail-fast checks that prevents an ambiguous file to be written later
@ -70,6 +76,18 @@ public final class JFCModel {
}
}
public static JFCModel create(SafePath file, Consumer<String> logger) throws ParseException, IOException {
if (file.toString().equals("none")) {
XmlConfiguration configuration = new XmlConfiguration();
configuration.setAttribute("version", "2.0");
configuration.setAttribute("label", "None");
return new JFCModel(configuration);
}
try (Reader r = JFC.newReader(file)) {
return new JFCModel(r, logger);
}
}
public void setLabel(String label) {
configuration.setAttribute("label", label);
}
@ -205,14 +223,4 @@ public final class JFCModel {
private void add(ControlElement control) {
controls.computeIfAbsent(control.getName(), x -> new ArrayList<>()).add(control);
}
private XmlConfiguration createConfiguration(SafePath file) throws ParseException, IOException {
if (file.toString().equals("none")) {
XmlConfiguration configuration = new XmlConfiguration();
configuration.setAttribute("version", "2.0");
configuration.setAttribute("label", "None");
return configuration;
}
return Parser.parse(file.toPath());
}
}

View File

@ -24,9 +24,8 @@
*/
package jdk.jfr.internal.jfc.model;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayDeque;
import java.util.Deque;
@ -38,15 +37,13 @@ import jdk.internal.org.xml.sax.helpers.DefaultHandler;
import jdk.internal.util.xml.SAXParser;
import jdk.internal.util.xml.impl.SAXParserImpl;
import static java.nio.charset.StandardCharsets.UTF_8;
final class Parser {
static XmlConfiguration parse(Path path) throws ParseException, IOException {
try (FileReader r = new FileReader(path.toFile(), UTF_8)) {
static XmlConfiguration parse(Reader reader) throws ParseException, IOException {
try {
SAXParser saxParser = new SAXParserImpl();
ConfigurationHandler handler = new ConfigurationHandler();
saxParser.parse(new InputSource(r), handler);
saxParser.parse(new InputSource(reader), handler);
return handler.configuration;
} catch (SAXException sp) {
ParseException pe = new ParseException(sp.getMessage(), -1);

View File

@ -127,7 +127,7 @@ final class Configure extends Command {
}
private void displayParameters(PrintStream stream, SafePath path, String name) throws ParseException, IOException {
JFCModel parameters = new JFCModel(path, l -> stream.println("Warning! " + l));
JFCModel parameters = JFCModel.create(path, l -> stream.println("Warning! " + l));
stream.println();
stream.println("Options for " + name + ":");
stream.println();