|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package jdk.jfr.internal.jfc; |
|
|
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.Reader; |
|
import java.nio.charset.StandardCharsets; |
|
import java.nio.file.Files; |
|
import java.nio.file.NoSuchFileException; |
|
import java.nio.file.Path; |
|
import java.nio.file.Paths; |
|
import java.text.ParseException; |
|
import java.util.ArrayList; |
|
import java.util.Arrays; |
|
import java.util.List; |
|
|
|
import jdk.jfr.Configuration; |
|
import jdk.jfr.internal.LogLevel; |
|
import jdk.jfr.internal.LogTag; |
|
import jdk.jfr.internal.Logger; |
|
import jdk.jfr.internal.SecuritySupport; |
|
import jdk.jfr.internal.SecuritySupport.SafePath; |
|
|
|
|
|
|
|
*/ |
|
public final class JFC { |
|
private static final int BUFFER_SIZE = 8192; |
|
private static final int MAXIMUM_FILE_SIZE = 1024 * 1024; |
|
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8; |
|
private static volatile List<KnownConfiguration> knownConfigurations; |
|
|
|
|
|
|
|
|
|
*/ |
|
private static final class KnownConfiguration { |
|
private final String content; |
|
private final String filename; |
|
private final String name; |
|
private Configuration configuration; |
|
|
|
public KnownConfiguration(SafePath knownPath) throws IOException { |
|
this.content = readContent(knownPath); |
|
this.name = nameFromPath(knownPath.toPath()); |
|
this.filename = nullSafeFileName(knownPath.toPath()); |
|
} |
|
|
|
public boolean isNamed(String name) { |
|
return filename.equals(name) || this.name.equals(name); |
|
} |
|
|
|
public Configuration getConfigurationFile() throws IOException, ParseException { |
|
if (configuration == null) { |
|
configuration = JFCParser.createConfiguration(name, content); |
|
} |
|
return configuration; |
|
} |
|
|
|
public String getName() { |
|
return name; |
|
} |
|
|
|
private static String readContent(SafePath knownPath) throws IOException { |
|
if (SecuritySupport.getFileSize(knownPath) > MAXIMUM_FILE_SIZE) { |
|
throw new IOException("Configuration with more than " |
|
+ MAXIMUM_FILE_SIZE + " characters can't be read."); |
|
} |
|
try (InputStream r = SecuritySupport.newFileInputStream(knownPath)) { |
|
return JFC.readContent(r); |
|
} |
|
} |
|
} |
|
|
|
private JFC() { |
|
// private utility class |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static Configuration create(String name, Reader reader) throws IOException, ParseException { |
|
return JFCParser.createConfiguration(name, reader); |
|
} |
|
|
|
private static String nullSafeFileName(Path file) throws IOException { |
|
Path filename = file.getFileName(); |
|
if (filename == null) { |
|
throw new IOException("Path has no file name"); |
|
} |
|
return filename.toString(); |
|
} |
|
|
|
public static String nameFromPath(Path file) throws IOException { |
|
String f = nullSafeFileName(file); |
|
if (f.endsWith(JFCParser.FILE_EXTENSION)) { |
|
return f.substring(0, f.length() - JFCParser.FILE_EXTENSION.length()); |
|
} else { |
|
return f; |
|
} |
|
} |
|
|
|
|
|
public static Configuration createKnown(String name) throws IOException, ParseException { |
|
|
|
for (KnownConfiguration known : getKnownConfigurations()) { |
|
if (known.isNamed(name)) { |
|
return known.getConfigurationFile(); |
|
} |
|
} |
|
|
|
SafePath path = SecuritySupport.JFC_DIRECTORY; |
|
if (path != null && SecuritySupport.exists(path)) { |
|
for (String extension : Arrays.asList("", JFCParser.FILE_EXTENSION)) { |
|
SafePath file = new SafePath(path.toPath().resolveSibling(name + extension)); |
|
if (SecuritySupport.exists(file) && !SecuritySupport.isDirectory(file)) { |
|
try (Reader r = SecuritySupport.newFileReader(file)) { |
|
String jfcName = nameFromPath(file.toPath()); |
|
return JFCParser.createConfiguration(jfcName, r); |
|
} |
|
} |
|
} |
|
} |
|
|
|
// Assume path included in name |
|
|
|
Path localPath = Paths.get(name); |
|
String jfcName = nameFromPath(localPath); |
|
try (Reader r = Files.newBufferedReader(localPath)) { |
|
return JFCParser.createConfiguration(jfcName, r); |
|
} |
|
} |
|
|
|
private static String readContent(InputStream source) throws IOException { |
|
byte[] bytes = read(source, BUFFER_SIZE); |
|
return new String(bytes, StandardCharsets.UTF_8); |
|
} |
|
|
|
|
|
private static byte[] read(InputStream source, int initialSize) throws IOException { |
|
int capacity = initialSize; |
|
byte[] buf = new byte[capacity]; |
|
int nread = 0; |
|
int n; |
|
for (;;) { |
|
// read to EOF which may read more or less than initialSize (eg: file |
|
|
|
while ((n = source.read(buf, nread, capacity - nread)) > 0) |
|
nread += n; |
|
|
|
// if last call to source.read() returned -1, we are done |
|
|
|
if (n < 0 || (n = source.read()) < 0) |
|
break; |
|
|
|
|
|
if (capacity <= MAX_BUFFER_SIZE - capacity) { |
|
capacity = Math.max(capacity << 1, BUFFER_SIZE); |
|
} else { |
|
if (capacity == MAX_BUFFER_SIZE) |
|
throw new OutOfMemoryError("Required array size too large"); |
|
capacity = MAX_BUFFER_SIZE; |
|
} |
|
buf = Arrays.copyOf(buf, capacity); |
|
buf[nread++] = (byte)n; |
|
} |
|
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static List<Configuration> getConfigurations() { |
|
List<Configuration> configs = new ArrayList<>(); |
|
for (KnownConfiguration knownConfig : getKnownConfigurations()) { |
|
try { |
|
configs.add(knownConfig.getConfigurationFile()); |
|
} catch (IOException e) { |
|
Logger.log(LogTag.JFR, LogLevel.WARN, "Could not load configuration " + knownConfig.getName() + ". " + e.getMessage()); |
|
} catch (ParseException e) { |
|
Logger.log(LogTag.JFR, LogLevel.WARN, "Could not parse configuration " + knownConfig.getName() + ". " + e.getMessage()); |
|
} |
|
} |
|
return configs; |
|
} |
|
|
|
private static List<KnownConfiguration> getKnownConfigurations() { |
|
if (knownConfigurations == null) { |
|
List<KnownConfiguration> configProxies = new ArrayList<>(); |
|
for (SafePath p : SecuritySupport.getPredefinedJFCFiles()) { |
|
try { |
|
configProxies.add(new KnownConfiguration(p)); |
|
} catch (IOException ioe) { |
|
// ignore |
|
} |
|
} |
|
knownConfigurations = configProxies; |
|
} |
|
return knownConfigurations; |
|
} |
|
|
|
public static Configuration getPredefined(String name) throws IOException, ParseException { |
|
for (KnownConfiguration knownConfig : getKnownConfigurations()) { |
|
if (knownConfig.getName().equals(name)) { |
|
return knownConfig.getConfigurationFile(); |
|
} |
|
} |
|
throw new NoSuchFileException("Could not locate configuration with name " + name); |
|
} |
|
} |