test/jdk/java/net/spi/URLStreamHandlerProvider/Basic.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45467 jdk/test/java/net/spi/URLStreamHandlerProvider/Basic.java@99c87a16a8e4
child 51687 1e39953aaed8
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     1
/*
45286
cd809e28c082 8180887: move FileUtils to top level testlibrary
iignatyev
parents: 44358
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     4
 *
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     8
 *
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    13
 * accompanied this code).
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    14
 *
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    18
 *
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    21
 * questions.
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    22
 */
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    23
41355
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    24
import java.io.File;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    25
import java.io.FileWriter;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    26
import java.io.Reader;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    27
import java.io.IOException;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    28
import java.io.InputStream;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    29
import java.io.InputStreamReader;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    30
import java.io.SequenceInputStream;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    31
import java.io.StringWriter;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    32
import java.io.Writer;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    33
import java.nio.file.Files;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    34
import java.nio.file.Path;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    35
import java.nio.file.Paths;
41355
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38883
diff changeset
    36
import java.util.ArrayList;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    37
import java.util.Collection;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    38
import java.util.Collections;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    39
import java.util.List;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    40
import java.util.function.Consumer;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    41
import java.util.stream.Collectors;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    42
import java.util.stream.Stream;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    43
import javax.tools.JavaCompiler;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    44
import javax.tools.JavaFileObject;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    45
import javax.tools.StandardJavaFileManager;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    46
import javax.tools.StandardLocation;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    47
import javax.tools.ToolProvider;
45286
cd809e28c082 8180887: move FileUtils to top level testlibrary
iignatyev
parents: 44358
diff changeset
    48
import jdk.test.lib.util.FileUtils;
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    49
import jdk.testlibrary.JDKToolFinder;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    50
import static java.lang.String.format;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    51
import static java.util.Arrays.asList;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    52
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    53
/*
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    54
 * @test
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    55
 * @bug 8064924
44358
d1d1a888d492 8177383: Wrong @modules in java/io/FilePermission/ReadFileOnPath.java
amlu
parents: 41355
diff changeset
    56
 * @modules jdk.compiler
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    57
 * @summary Basic test for URLStreamHandlerProvider
45286
cd809e28c082 8180887: move FileUtils to top level testlibrary
iignatyev
parents: 44358
diff changeset
    58
 * @library /lib/testlibrary /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45286
diff changeset
    59
 * @build jdk.test.lib.Platform
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45286
diff changeset
    60
 *        jdk.test.lib.util.FileUtils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45286
diff changeset
    61
 *        jdk.testlibrary.JDKToolFinder
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    62
 * @compile Basic.java Child.java
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    63
 * @run main Basic
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    64
 */
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    65
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    66
public class Basic {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    67
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    68
    static final Path TEST_SRC = Paths.get(System.getProperty("test.src", "."));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    69
    static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", "."));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    70
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    71
    public static void main(String[] args) throws Throwable {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    72
        unknownProtocol("foo", UNKNOWN);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    73
        unknownProtocol("bar", UNKNOWN);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    74
        viaProvider("baz", KNOWN);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    75
        viaProvider("bert", KNOWN);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    76
        viaProvider("ernie", UNKNOWN, "-Djava.security.manager");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    77
        viaProvider("curly", UNKNOWN, "-Djava.security.manager");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    78
        viaProvider("larry", KNOWN, "-Djava.security.manager",
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    79
                "-Djava.security.policy=" + TEST_SRC + File.separator + "basic.policy");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    80
        viaProvider("moe", KNOWN, "-Djava.security.manager",
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    81
                "-Djava.security.policy=" + TEST_SRC + File.separator + "basic.policy");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    82
        viaBadProvider("tom", SCE);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    83
        viaBadProvider("jerry", SCE);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    84
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    85
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    86
    static final Consumer<Result> KNOWN = r -> {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    87
        if (r.exitValue != 0 || !r.output.isEmpty())
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    88
            throw new RuntimeException(r.output);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    89
    };
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    90
    static final Consumer<Result> UNKNOWN = r -> {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    91
        if (r.exitValue == 0 ||
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    92
            !r.output.contains("java.net.MalformedURLException: unknown protocol")) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    93
            throw new RuntimeException("exitValue: "+ r.exitValue + ", output:[" +r.output +"]");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    94
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    95
    };
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    96
    static final Consumer<Result> SCE = r -> {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    97
        if (r.exitValue == 0 ||
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    98
            !r.output.contains("java.util.ServiceConfigurationError")) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
    99
            throw new RuntimeException("exitValue: "+ r.exitValue + ", output:[" +r.output +"]");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   100
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   101
    };
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   102
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   103
    static void unknownProtocol(String protocol, Consumer<Result> resultChecker) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   104
        System.out.println("\nTesting " + protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   105
        Result r = java(Collections.emptyList(), asList(TEST_CLASSES),
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   106
                "Child", protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   107
        resultChecker.accept(r);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   108
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   109
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   110
    static void viaProvider(String protocol, Consumer<Result> resultChecker,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   111
                            String... sysProps)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   112
        throws Exception
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   113
    {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   114
        viaProviderWithTemplate(protocol, resultChecker,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   115
                                TEST_SRC.resolve("provider.template"),
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   116
                                sysProps);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   117
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   118
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   119
    static void viaBadProvider(String protocol, Consumer<Result> resultChecker,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   120
                               String... sysProps)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   121
        throws Exception
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   122
    {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   123
        viaProviderWithTemplate(protocol, resultChecker,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   124
                                TEST_SRC.resolve("bad.provider.template"),
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   125
                                sysProps);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   126
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   127
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   128
    static void viaProviderWithTemplate(String protocol,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   129
                                        Consumer<Result> resultChecker,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   130
                                        Path template, String... sysProps)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   131
        throws Exception
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   132
    {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   133
        System.out.println("\nTesting " + protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   134
        Path testRoot = Paths.get("URLStreamHandlerProvider-" + protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   135
        if (Files.exists(testRoot))
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   136
            FileUtils.deleteFileTreeWithRetry(testRoot);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   137
        Files.createDirectory(testRoot);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   138
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   139
        Path srcPath = Files.createDirectory(testRoot.resolve("src"));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   140
        Path srcClass = createProvider(protocol, template, srcPath);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   141
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   142
        Path build = Files.createDirectory(testRoot.resolve("build"));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   143
        javac(build, srcClass);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   144
        createServices(build, protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   145
        Path testJar = testRoot.resolve("test.jar");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   146
        jar(testJar, build);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   147
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   148
        List<String> props = new ArrayList<>();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   149
        for (String p : sysProps)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   150
            props.add(p);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   151
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   152
        Result r = java(props, asList(testJar, TEST_CLASSES),
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   153
                        "Child", protocol);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   154
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   155
        resultChecker.accept(r);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   156
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   157
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   158
    static String platformPath(String p) { return p.replace("/", File.separator); }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   159
    static String binaryName(String name) { return name.replace(".", "/"); }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   160
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   161
    static final String SERVICE_IMPL_PREFIX = "net.java.openjdk.test";
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   162
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   163
    static void createServices(Path dst, String protocol) throws IOException {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   164
        Path services = Files.createDirectories(dst.resolve("META-INF")
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   165
                                                   .resolve("services"));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   166
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   167
        final String implName =  SERVICE_IMPL_PREFIX + "." + protocol + ".Provider";
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   168
        Path s = services.resolve("java.net.spi.URLStreamHandlerProvider");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   169
        FileWriter fw = new FileWriter(s.toFile());
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   170
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   171
            fw.write(implName);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   172
        } finally {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   173
            fw.close();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   174
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   175
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   176
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   177
    static Path createProvider(String protocol, Path srcTemplate, Path dst)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   178
        throws IOException
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   179
    {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   180
        String pkg = SERVICE_IMPL_PREFIX + "." + protocol;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   181
        Path classDst = dst.resolve(platformPath(binaryName(pkg)));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   182
        Files.createDirectories(classDst);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   183
        Path classPath = classDst.resolve("Provider.java");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   184
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   185
        List<String> lines = Files.lines(srcTemplate)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   186
                                  .map(s -> s.replaceAll("\\$package", pkg))
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   187
                                  .map(s -> s.replaceAll("\\$protocol", protocol))
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   188
                                  .collect(Collectors.toList());
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   189
        Files.write(classPath, lines);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   190
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   191
        return classPath;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   192
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   193
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   194
    static void jar(Path jarName, Path jarRoot) { String jar = getJDKTool("jar");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   195
        ProcessBuilder p = new ProcessBuilder(jar, "cf", jarName.toString(),
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   196
                "-C", jarRoot.toString(), ".");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   197
        quickFail(run(p));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   198
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   199
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   200
    static void javac(Path dest, Path... sourceFiles) throws IOException {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   201
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   202
        try (StandardJavaFileManager fileManager =
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   203
                    compiler.getStandardFileManager(null, null, null)) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   204
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   205
            List<File> files = Stream.of(sourceFiles)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   206
                    .map(p -> p.toFile())
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   207
                    .collect(Collectors.toList());
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   208
            List<File> dests = Stream.of(dest)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   209
                    .map(p -> p.toFile())
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   210
                    .collect(Collectors.toList());
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   211
            Iterable<? extends JavaFileObject> compilationUnits =
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   212
                    fileManager.getJavaFileObjectsFromFiles(files);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   213
            fileManager.setLocation(StandardLocation.CLASS_OUTPUT, dests);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   214
            JavaCompiler.CompilationTask task =
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   215
                    compiler.getTask(null, fileManager, null, null, null, compilationUnits);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   216
            boolean passed = task.call();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   217
            if (!passed)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   218
                throw new RuntimeException("Error compiling " + files);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   219
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   220
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   221
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   222
    static void quickFail(Result r) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   223
        if (r.exitValue != 0)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   224
            throw new RuntimeException(r.output);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   225
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   226
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   227
    static Result java(List<String> sysProps, Collection<Path> classpath,
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   228
                       String classname, String arg) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   229
        String java = getJDKTool("java");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   230
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   231
        List<String> commands = new ArrayList<>();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   232
        commands.add(java);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   233
        for (String prop : sysProps)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   234
            commands.add(prop);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   235
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   236
        String cp = classpath.stream()
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   237
                .map(Path::toString)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   238
                .collect(Collectors.joining(File.pathSeparator));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   239
        commands.add("-cp");
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   240
        commands.add(cp);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   241
        commands.add(classname);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   242
        commands.add(arg);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   243
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   244
        return run(new ProcessBuilder(commands));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   245
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   246
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   247
    static Result run(ProcessBuilder pb) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   248
        Process p = null;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   249
        System.out.println("running: " + pb.command());
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   250
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   251
            p = pb.start();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   252
        } catch (IOException e) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   253
            throw new RuntimeException(
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   254
                    format("Couldn't start process '%s'", pb.command()), e);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   255
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   256
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   257
        String output;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   258
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   259
            output = toString(p.getInputStream(), p.getErrorStream());
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   260
        } catch (IOException e) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   261
            throw new RuntimeException(
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   262
                    format("Couldn't read process output '%s'", pb.command()), e);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   263
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   264
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   265
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   266
            p.waitFor();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   267
        } catch (InterruptedException e) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   268
            throw new RuntimeException(
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   269
                    format("Process hasn't finished '%s'", pb.command()), e);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   270
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   271
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   272
        return new Result(p.exitValue(), output);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   273
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   274
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   275
    static final String DEFAULT_IMAGE_BIN = System.getProperty("java.home")
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   276
            + File.separator + "bin" + File.separator;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   277
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   278
    static String getJDKTool(String name) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   279
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   280
            return JDKToolFinder.getJDKTool(name);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   281
        } catch (Exception x) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   282
            return DEFAULT_IMAGE_BIN + name;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   283
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   284
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   285
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   286
    static String toString(InputStream... src) throws IOException {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   287
        StringWriter dst = new StringWriter();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   288
        Reader concatenated =
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   289
                new InputStreamReader(
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   290
                        new SequenceInputStream(
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   291
                                Collections.enumeration(asList(src))));
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   292
        copy(concatenated, dst);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   293
        return dst.toString();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   294
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   295
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   296
    static void copy(Reader src, Writer dst) throws IOException {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   297
        int len;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   298
        char[] buf = new char[1024];
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   299
        try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   300
            while ((len = src.read(buf)) != -1)
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   301
                dst.write(buf, 0, len);
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   302
        } finally {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   303
            try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   304
                src.close();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   305
            } catch (IOException ignored1) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   306
            } finally {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   307
                try {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   308
                    dst.close();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   309
                } catch (IOException ignored2) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   310
                }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   311
            }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   312
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   313
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   314
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   315
    static class Result {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   316
        final int exitValue;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   317
        final String output;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   318
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   319
        private Result(int exitValue, String output) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   320
            this.exitValue = exitValue;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   321
            this.output = output;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   322
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   323
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents:
diff changeset
   324
}