1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.io.IOException;
25import java.net.URI;
26import java.net.URL;
27import java.net.URLClassLoader;
28import java.nio.file.FileSystem;
29import java.nio.file.FileSystems;
30import java.nio.file.Files;
31import java.nio.file.Path;
32import java.nio.file.Paths;
33import java.util.Collections;
34import java.util.stream.Stream;
35
36/**
37 * Basic jrt file system functionality testing
38 *
39 */
40public class Main {
41
42    public static void main(String[] args) throws Exception {
43        String javaHome = args[0];
44        FileSystem fs = null;
45        boolean isInstalled = false;
46        if (args.length == 2) {
47            fs = createFsByInstalledProvider();
48            isInstalled = true;
49        } else {
50            fs = createFsWithURLClassloader(javaHome);
51        }
52
53        Path mods = fs.getPath("/modules");
54        try (Stream<Path> stream = Files.walk(mods)) {
55            stream.forEach(path -> {
56                path.getFileName();
57            });
58        } finally {
59            try {
60                fs.close();
61            } catch (UnsupportedOperationException e) {
62                if (!isInstalled) {
63                    throw new RuntimeException(
64                        "UnsupportedOperationException is thrown unexpectedly");
65                }
66            }
67        }
68    }
69
70    private static FileSystem createFsWithURLClassloader(String javaHome) throws IOException{
71        URL url = Paths.get(javaHome, "lib", "jrt-fs.jar").toUri().toURL();
72        URLClassLoader loader = new URLClassLoader(new URL[] { url });
73        return FileSystems.newFileSystem(URI.create("jrt:/"),
74                                                    Collections.emptyMap(),
75                                                    loader);
76    }
77
78    private static FileSystem createFsByInstalledProvider() throws IOException {
79        return FileSystems.getFileSystem(URI.create("jrt:/"));
80    }
81}
82