Search in sources :

Example 1 with JsonWriter

use of com.oracle.svm.configure.json.JsonWriter in project graal by oracle.

the class ConfigurationWithOriginsResultWriter method writeToDirectory.

@Override
public List<Path> writeToDirectory(Path directoryPath) throws IOException {
    List<Path> writtenPaths = new ArrayList<>();
    for (ConfigurationFile configFile : ConfigurationFile.values()) {
        if (configFile.canBeGeneratedByAgent()) {
            Path filePath = directoryPath.resolve(configFile.getFileName(CONFIG_WITH_ORIGINS_FILE_SUFFIX));
            try (JsonWriter writer = new JsonWriter(filePath)) {
                rootNode.writeJson(writer, configFile);
            }
            writtenPaths.add(filePath);
        }
    }
    return writtenPaths;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) ConfigurationFile(com.oracle.svm.core.configure.ConfigurationFile) JsonWriter(com.oracle.svm.configure.json.JsonWriter)

Example 2 with JsonWriter

use of com.oracle.svm.configure.json.JsonWriter in project graal by oracle.

the class TraceFileWriter method traceEntry.

@Override
protected void traceEntry(Map<String, Object> entry) {
    try {
        StringWriter str = new StringWriter();
        try (JsonWriter json = new JsonWriter(str)) {
            json.append('{');
            boolean first = true;
            for (Entry<String, ?> mapEntry : entry.entrySet()) {
                if (!first) {
                    json.append(", ");
                }
                json.quote(mapEntry.getKey()).append(':');
                if (mapEntry.getValue() instanceof Object[]) {
                    printArray(json, (Object[]) mapEntry.getValue());
                } else {
                    printValue(json, mapEntry.getValue());
                }
                first = false;
            }
            json.append('}');
        }
        traceEntry(str.toString());
    } catch (IOException e) {
        throw VMError.shouldNotReachHere(e);
    }
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) JsonWriter(com.oracle.svm.configure.json.JsonWriter)

Example 3 with JsonWriter

use of com.oracle.svm.configure.json.JsonWriter in project graal by oracle.

the class ResourceConfigurationTest method printJson.

@Test
public void printJson() {
    ResourceConfiguration rc = new ResourceConfiguration();
    ConfigurationCondition defaultCond = ConfigurationCondition.alwaysTrue();
    rc.addResourcePattern(defaultCond, ".*/Resource.*txt$");
    rc.ignoreResourcePattern(defaultCond, ".*/Resource2.txt$");
    PipedWriter pw = new PipedWriter();
    JsonWriter jw = new JsonWriter(pw);
    try (PipedReader pr = new PipedReader()) {
        pr.connect(pw);
        Thread writerThread = new Thread(() -> {
            try (JsonWriter w = jw) {
                rc.printJson(w);
            } catch (IOException e) {
                Assert.fail(e.getMessage());
            }
        });
        List<String> addedResources = new LinkedList<>();
        List<String> ignoredResources = new LinkedList<>();
        ResourcesRegistry registry = new ResourcesRegistry() {

            @Override
            public void addResources(ConfigurationCondition condition, String pattern) {
                addedResources.add(pattern);
            }

            @Override
            public void ignoreResources(ConfigurationCondition condition, String pattern) {
                ignoredResources.add(pattern);
            }

            @Override
            public void addResourceBundles(ConfigurationCondition condition, String name) {
            }

            @Override
            public void addResourceBundles(ConfigurationCondition condition, String basename, Collection<Locale> locales) {
            }

            @Override
            public void addClassBasedResourceBundle(ConfigurationCondition condition, String basename, String className) {
            }
        };
        ResourceConfigurationParser rcp = new ResourceConfigurationParser(registry, true);
        writerThread.start();
        rcp.parseAndRegister(pr);
        writerThread.join();
        Assert.assertTrue(addedResources.contains(".*/Resource.*txt$"));
        Assert.assertTrue(ignoredResources.contains(".*/Resource2.txt$"));
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ConfigurationCondition(org.graalvm.nativeimage.impl.ConfigurationCondition) IOException(java.io.IOException) JsonWriter(com.oracle.svm.configure.json.JsonWriter) PipedReader(java.io.PipedReader) LinkedList(java.util.LinkedList) ResourceConfiguration(com.oracle.svm.configure.config.ResourceConfiguration) ResourcesRegistry(com.oracle.svm.core.configure.ResourcesRegistry) PipedWriter(java.io.PipedWriter) Collection(java.util.Collection) ResourceConfigurationParser(com.oracle.svm.core.configure.ResourceConfigurationParser) Test(org.junit.Test)

Example 4 with JsonWriter

use of com.oracle.svm.configure.json.JsonWriter in project graal by oracle.

the class ConfigurationResultWriter method writeToDirectory.

@Override
public List<Path> writeToDirectory(Path directoryPath) throws IOException {
    List<Path> writtenPaths = new ArrayList<>();
    for (ConfigurationFile configFile : ConfigurationFile.values()) {
        if (configFile.canBeGeneratedByAgent()) {
            Path filePath = directoryPath.resolve(configFile.getFileName());
            try (JsonWriter writer = new JsonWriter(filePath)) {
                processor.getConfiguration(configFile).printJson(writer);
                /* Add an extra EOF newline */
                writer.newline();
            }
            writtenPaths.add(filePath);
        }
    }
    return writtenPaths;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) ConfigurationFile(com.oracle.svm.core.configure.ConfigurationFile) JsonWriter(com.oracle.svm.configure.json.JsonWriter)

Example 5 with JsonWriter

use of com.oracle.svm.configure.json.JsonWriter in project graal by oracle.

the class ConfigurationTool method generate.

@SuppressWarnings("fallthrough")
private static void generate(Iterator<String> argsIter, boolean acceptTraceFileArgs) throws IOException {
    List<URI> traceInputs = new ArrayList<>();
    boolean builtinCallerFilter = true;
    boolean builtinHeuristicFilter = true;
    List<URI> callerFilters = new ArrayList<>();
    ConfigurationSet omittedInputSet = new ConfigurationSet();
    ConfigurationSet inputSet = new ConfigurationSet();
    ConfigurationSet outputSet = new ConfigurationSet();
    while (argsIter.hasNext()) {
        String[] parts = argsIter.next().split("=", 2);
        String current = parts[0];
        String value = (parts.length > 1) ? parts[1] : null;
        ConfigurationSet set = outputSet;
        switch(current) {
            case "--input-dir":
                inputSet.addDirectory(requirePath(current, value));
                break;
            case "--output-dir":
                Path directory = requirePath(current, value);
                if (!Files.exists(directory)) {
                    Files.createDirectory(directory);
                } else if (!Files.isDirectory(directory)) {
                    throw new NoSuchFileException(value);
                }
                outputSet.addDirectory(directory);
                break;
            case "--omit-from-input-dir":
                omittedInputSet.addDirectory(requirePath(current, value));
                break;
            case "--reflect-input":
                // fall through
                set = inputSet;
            case "--reflect-output":
                set.getReflectConfigPaths().add(requirePathUri(current, value));
                break;
            case "--jni-input":
                // fall through
                set = inputSet;
            case "--jni-output":
                set.getJniConfigPaths().add(requirePathUri(current, value));
                break;
            case "--proxy-input":
                // fall through
                set = inputSet;
            case "--proxy-output":
                set.getProxyConfigPaths().add(requirePathUri(current, value));
                break;
            case "--resource-input":
                // fall through
                set = inputSet;
            case "--resource-output":
                set.getResourceConfigPaths().add(requirePathUri(current, value));
                break;
            case "--serialization-input":
                // fall through
                set = inputSet;
            case "--serialization-output":
                set.getSerializationConfigPaths().add(requirePathUri(current, value));
                break;
            case "--predefined-classes-input":
                // fall through
                set = inputSet;
            case "--predefined-classes-output":
                set.getPredefinedClassesConfigPaths().add(requirePathUri(current, value));
                break;
            case "--trace-input":
                traceInputs.add(requirePathUri(current, value));
                break;
            case // legacy
            "--no-filter":
                builtinCallerFilter = false;
                builtinHeuristicFilter = false;
                break;
            case "--no-builtin-caller-filter":
                builtinCallerFilter = false;
                break;
            case "--no-builtin-heuristic-filter":
                builtinHeuristicFilter = false;
                break;
            case "--caller-filter-file":
                callerFilters.add(requirePathUri(current, value));
                break;
            case "--":
                if (acceptTraceFileArgs) {
                    argsIter.forEachRemaining(arg -> traceInputs.add(Paths.get(arg).toUri()));
                } else {
                    throw new UsageException("Unknown argument: " + current);
                }
                break;
            default:
                if (!acceptTraceFileArgs || current.startsWith("-")) {
                    throw new UsageException("Unknown argument: " + current);
                }
                traceInputs.add(Paths.get(current).toUri());
                break;
        }
    }
    failIfAgentLockFilesPresent(inputSet, omittedInputSet, outputSet);
    RuleNode callersFilter = null;
    if (!builtinCallerFilter) {
        callersFilter = RuleNode.createRoot();
        callersFilter.addOrGetChildren("**", RuleNode.Inclusion.Include);
    }
    if (!callerFilters.isEmpty()) {
        if (callersFilter == null) {
            callersFilter = AccessAdvisor.copyBuiltinCallerFilterTree();
        }
        for (URI uri : callerFilters) {
            try {
                FilterConfigurationParser parser = new FilterConfigurationParser(callersFilter);
                parser.parseAndRegister(uri);
            } catch (Exception e) {
                throw new UsageException("Cannot parse filter file " + uri + ": " + e);
            }
        }
        callersFilter.removeRedundantNodes();
    }
    AccessAdvisor advisor = new AccessAdvisor();
    advisor.setHeuristicsEnabled(builtinHeuristicFilter);
    if (callersFilter != null) {
        advisor.setCallerFilterTree(callersFilter);
    }
    TraceProcessor p;
    TraceProcessor omittedInputTraceProcessor;
    try {
        omittedInputTraceProcessor = new TraceProcessor(advisor, omittedInputSet.loadJniConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadReflectConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadProxyConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadResourceConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadSerializationConfig(ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputSet.loadPredefinedClassesConfig(null, null, ConfigurationSet.FAIL_ON_EXCEPTION), null);
        List<Path> predefinedClassDestDirs = new ArrayList<>();
        for (URI pathUri : outputSet.getPredefinedClassesConfigPaths()) {
            Path subdir = Files.createDirectories(Paths.get(pathUri).getParent().resolve(ConfigurationFile.PREDEFINED_CLASSES_AGENT_EXTRACTED_SUBDIR));
            subdir = Files.createDirectories(subdir);
            predefinedClassDestDirs.add(subdir);
        }
        Predicate<String> shouldExcludeClassesWithHash = omittedInputTraceProcessor.getPredefinedClassesConfiguration()::containsClassWithHash;
        p = new TraceProcessor(advisor, inputSet.loadJniConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadReflectConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadProxyConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadResourceConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadSerializationConfig(ConfigurationSet.FAIL_ON_EXCEPTION), inputSet.loadPredefinedClassesConfig(predefinedClassDestDirs.toArray(new Path[0]), shouldExcludeClassesWithHash, ConfigurationSet.FAIL_ON_EXCEPTION), omittedInputTraceProcessor);
    } catch (IOException e) {
        throw e;
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    if (traceInputs.isEmpty() && inputSet.isEmpty()) {
        throw new UsageException("No inputs specified.");
    }
    for (URI uri : traceInputs) {
        try (Reader reader = Files.newBufferedReader(Paths.get(uri))) {
            p.process(reader);
        }
    }
    if (outputSet.isEmpty()) {
        System.err.println("Warning: no outputs specified, validating inputs only.");
    }
    for (URI uri : outputSet.getReflectConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getReflectionConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getJniConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getJniConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getProxyConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getProxyConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getResourceConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getResourceConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getSerializationConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getSerializationConfiguration().printJson(writer);
        }
    }
    for (URI uri : outputSet.getPredefinedClassesConfigPaths()) {
        try (JsonWriter writer = new JsonWriter(Paths.get(uri))) {
            p.getPredefinedClassesConfiguration().printJson(writer);
        }
    }
}
Also used : Path(java.nio.file.Path) RuleNode(com.oracle.svm.configure.filters.RuleNode) TraceProcessor(com.oracle.svm.configure.trace.TraceProcessor) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) FilterConfigurationParser(com.oracle.svm.configure.filters.FilterConfigurationParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URI(java.net.URI) AccessAdvisor(com.oracle.svm.configure.trace.AccessAdvisor) JsonWriter(com.oracle.svm.configure.json.JsonWriter) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ConfigurationSet(com.oracle.svm.configure.config.ConfigurationSet)

Aggregations

JsonWriter (com.oracle.svm.configure.json.JsonWriter)6 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 ConfigurationFile (com.oracle.svm.core.configure.ConfigurationFile)2 ConfigurationSet (com.oracle.svm.configure.config.ConfigurationSet)1 ResourceConfiguration (com.oracle.svm.configure.config.ResourceConfiguration)1 FilterConfigurationParser (com.oracle.svm.configure.filters.FilterConfigurationParser)1 RuleNode (com.oracle.svm.configure.filters.RuleNode)1 AccessAdvisor (com.oracle.svm.configure.trace.AccessAdvisor)1 TraceProcessor (com.oracle.svm.configure.trace.TraceProcessor)1 ResourceConfigurationParser (com.oracle.svm.core.configure.ResourceConfigurationParser)1 ResourcesRegistry (com.oracle.svm.core.configure.ResourcesRegistry)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PipedReader (java.io.PipedReader)1 PipedWriter (java.io.PipedWriter)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1