src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Utils.java
author pchilanomate
Fri, 31 Aug 2018 10:22:04 -0400
changeset 51610 cdef4df6b0e7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8206424: Use locking for cleaning ProtectionDomainTable Summary: ServiceThread is now in charge of cleaning ProtectionDomainTable entries Reviewed-by: coleenp, iklam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    27
import java.net.URI;
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    28
import java.nio.file.FileSystem;
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    29
import java.nio.file.FileSystems;
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    30
import java.nio.file.Path;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    31
import java.nio.file.PathMatcher;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.ArrayList;
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    33
import java.util.Arrays;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Comparator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.List;
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    36
import java.util.stream.Collectors;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.tools.jlink.plugin.Plugin;
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    38
import jdk.tools.jlink.plugin.Plugin.Category;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
public class Utils {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
    private Utils() {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    44
    // jrt-fs file system
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    45
    private static FileSystem JRT_FILE_SYSTEM;
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    46
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    // current module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    private static final Module THIS_MODULE = Utils.class.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    50
    public static List<String> parseList(String arguments) {
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    51
        return Arrays.stream(arguments.split(","))
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    52
                     .map((p) -> p.trim())
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    53
                     .filter((p) -> !p.isEmpty())
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    54
                     .collect(Collectors.toList());
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    55
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
    58
    public static List<Plugin> getSortedPlugins(List<Plugin> plugins) {
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    59
        List<Plugin> res = new ArrayList<>();
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
    60
        res.addAll(plugins);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        res.sort(new Comparator<Plugin>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            public int compare(Plugin o1, Plugin o2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                return o1.getName().compareTo(o2.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        return res;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static boolean isFunctional(Plugin prov) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37888
diff changeset
    71
        return prov.getState().contains(Plugin.State.FUNCTIONAL);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public static boolean isAutoEnabled(Plugin prov) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37888
diff changeset
    75
        return prov.getState().contains(Plugin.State.AUTO_ENABLED);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    public static boolean isDisabled(Plugin prov) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37888
diff changeset
    79
        return prov.getState().contains(Plugin.State.DISABLED);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    // is this a builtin (jdk.jlink) plugin?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    public static boolean isBuiltin(Plugin prov) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        return THIS_MODULE.equals(prov.getClass().getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    }
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    86
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    87
    public static FileSystem jrtFileSystem() {
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    88
        if (JRT_FILE_SYSTEM == null) {
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    89
            JRT_FILE_SYSTEM = FileSystems.getFileSystem(URI.create("jrt:/"));
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    90
        }
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    91
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
    92
        return JRT_FILE_SYSTEM;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    93
    }
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    94
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    95
    public static PathMatcher getPathMatcher(FileSystem fs, String pattern) {
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    96
        if (!pattern.startsWith("glob:") && !pattern.startsWith("regex:")) {
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    97
            pattern = "glob:" + pattern;
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    98
        }
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
    99
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
   100
        return fs.getPathMatcher(pattern);
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
   101
    }
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
   102
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
   103
    public static PathMatcher getJRTFSPathMatcher(String pattern) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
   104
        return getPathMatcher(jrtFileSystem(), pattern);
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38320
diff changeset
   105
    }
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
   106
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
   107
    public static Path getJRTFSPath(String first, String... more) {
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
   108
        return jrtFileSystem().getPath(first, more);
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38871
diff changeset
   109
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
}