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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package jdk.tools.jlink.internal;
26
27import java.net.URI;
28import java.nio.file.FileSystem;
29import java.nio.file.FileSystems;
30import java.nio.file.Path;
31import java.nio.file.PathMatcher;
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.Comparator;
35import java.util.List;
36import java.util.stream.Collectors;
37import jdk.tools.jlink.plugin.Plugin;
38import jdk.tools.jlink.plugin.Plugin.Category;
39
40public class Utils {
41
42    private Utils() {}
43
44    // jrt-fs file system
45    private static FileSystem JRT_FILE_SYSTEM;
46
47    // current module
48    private static final Module THIS_MODULE = Utils.class.getModule();
49
50    public static List<String> parseList(String arguments) {
51        return Arrays.stream(arguments.split(","))
52                     .map((p) -> p.trim())
53                     .filter((p) -> !p.isEmpty())
54                     .collect(Collectors.toList());
55    }
56
57
58    public static List<Plugin> getSortedPlugins(List<Plugin> plugins) {
59        List<Plugin> res = new ArrayList<>();
60        res.addAll(plugins);
61        res.sort(new Comparator<Plugin>() {
62            @Override
63            public int compare(Plugin o1, Plugin o2) {
64                return o1.getName().compareTo(o2.getName());
65            }
66        });
67        return res;
68    }
69
70    public static boolean isFunctional(Plugin prov) {
71        return prov.getState().contains(Plugin.State.FUNCTIONAL);
72    }
73
74    public static boolean isAutoEnabled(Plugin prov) {
75        return prov.getState().contains(Plugin.State.AUTO_ENABLED);
76    }
77
78    public static boolean isDisabled(Plugin prov) {
79        return prov.getState().contains(Plugin.State.DISABLED);
80    }
81
82    // is this a builtin (jdk.jlink) plugin?
83    public static boolean isBuiltin(Plugin prov) {
84        return THIS_MODULE.equals(prov.getClass().getModule());
85    }
86
87    public static FileSystem jrtFileSystem() {
88        if (JRT_FILE_SYSTEM == null) {
89            JRT_FILE_SYSTEM = FileSystems.getFileSystem(URI.create("jrt:/"));
90        }
91
92        return JRT_FILE_SYSTEM;
93    }
94
95    public static PathMatcher getPathMatcher(FileSystem fs, String pattern) {
96        if (!pattern.startsWith("glob:") && !pattern.startsWith("regex:")) {
97            pattern = "glob:" + pattern;
98        }
99
100        return fs.getPathMatcher(pattern);
101    }
102
103    public static PathMatcher getJRTFSPathMatcher(String pattern) {
104        return getPathMatcher(jrtFileSystem(), pattern);
105    }
106
107    public static Path getJRTFSPath(String first, String... more) {
108        return jrtFileSystem().getPath(first, more);
109    }
110}
111