Example usage for java.util.spi ToolProvider findFirst

List of usage examples for java.util.spi ToolProvider findFirst

Introduction

In this page you can find the example usage for java.util.spi ToolProvider findFirst.

Prototype

static Optional<ToolProvider> findFirst(String name) 

Source Link

Document

Returns the first instance of a ToolProvider with the given name, as loaded by ServiceLoader using the system class loader.

Usage

From source file:platform.tooling.support.Runner.java

private Result installRemoteTool(Result result) {
    // download/*from w w w . ja v  a2 s  .c  o  m*/
    var version = request.getVersion();
    var toolArchive = tool.computeArchive(version);
    var toolUri = tool.computeUri(version);
    var toolArchivePath = toolPath.resolve(toolArchive);
    if (Files.notExists(toolArchivePath)) {
        var timeout = (int) TimeUnit.MILLISECONDS.convert(9, TimeUnit.SECONDS);
        try {
            FileUtils.copyURLToFile(toolUri.toURL(), toolArchivePath.toFile(), timeout, timeout);
        } catch (IOException e) {
            throw new UncheckedIOException("Loading tool failed: " + toolUri, e);
        }
    }

    // extract
    var jarTool = ToolProvider.findFirst("jar").orElseThrow();
    var stringWriter = new StringWriter();
    var printWriter = new PrintWriter(stringWriter);
    jarTool.run(printWriter, printWriter, "--list", "--file", toolArchivePath.toString());
    var toolFolderName = stringWriter.toString().split("\\R")[0];
    var toolFolderPath = toolPath.resolve(toolFolderName);
    if (Files.notExists(toolFolderPath)) {
        try {
            jarTool.run(System.out, System.err, "--extract", "--file", toolArchivePath.toString());
            FileUtils.moveDirectoryToDirectory(Paths.get(toolFolderName).toFile(), toolPath.toFile(), true);
        } catch (IOException e) {
            throw new UncheckedIOException("Extracting tool failed: " + toolUri, e);
        }
    }
    result.toolHome = toolFolderPath.normalize().toAbsolutePath();

    // compute program entry point
    var executable = toolFolderPath.resolve(tool.computeExecutablePath());
    if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
        executable.toFile().setExecutable(true);
    }
    result.toolExecutable = executable;
    return result;
}

From source file:platform.tooling.support.Runner.java

private Result runJdkFoundationTool(Result result) throws Exception {
    var toolProvider = ToolProvider.findFirst(tool.getExecutable()).orElseThrow();
    var args = request.getArguments().toArray(new String[0]);
    try (var out = new PrintStream(Files.newOutputStream(result.out)); //
            var err = new PrintStream(Files.newOutputStream(result.err))) {
        result.status = toolProvider.run(out, err, args);
    }//from   ww  w . j a v a  2  s  .c  om
    return result;
}