jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java
author alanb
Fri, 28 Oct 2016 10:18:07 +0100
changeset 41817 b90ad1de93ea
parent 41766 b5eab76a23a9
child 41831 2dd91b18195b
permissions -rw-r--r--
8168789: ModuleReader.list and ModuleFinder.of update Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.PrintWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.text.MessageFormat;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Locale;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Map.Entry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.MissingResourceException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.ResourceBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
41561
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
    50
import jdk.tools.jlink.internal.plugins.ExcludeFilesPlugin;
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
    51
import jdk.tools.jlink.internal.plugins.ExcludeJmodSectionPlugin;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.tools.jlink.plugin.Plugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    53
import jdk.tools.jlink.plugin.Plugin.Category;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.tools.jlink.builder.DefaultImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import jdk.tools.jlink.builder.ImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import jdk.tools.jlink.plugin.PluginException;
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 40261
diff changeset
    57
import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.tools.jlink.internal.plugins.PluginsResourceBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
    61
import jdk.internal.misc.SharedSecrets;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 * JLink and JImage tools shared helper.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
public final class TaskHelper {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    public static final String JLINK_BUNDLE = "jdk.tools.jlink.resources.jlink";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static final String JIMAGE_BUNDLE = "jdk.tools.jimage.resources.jimage";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    private static final String DEFAULTS_PROPERTY = "jdk.jlink.defaults";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public final class BadArgs extends Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        static final long serialVersionUID = 8765093759964640721L;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        private BadArgs(String key, Object... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            super(bundleHelper.getMessage(key, args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            this.key = key;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            this.args = args;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        public BadArgs showUsage(boolean b) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            showUsage = b;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        public final String key;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        public final Object[] args;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        public boolean showUsage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    public static class Option<T> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        public interface Processing<T> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            void process(T task, String opt, String arg) throws BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        final boolean hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        final Processing<T> processing;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        final boolean hidden;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        final String[] aliases;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        public Option(boolean hasArg, Processing<T> processing, boolean hidden, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            this.hasArg = hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            this.processing = processing;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            this.aliases = aliases;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            this.hidden = hidden;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        public Option(boolean hasArg, Processing<T> processing, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            this(hasArg, processing, false, aliases);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        public boolean isHidden() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            return hidden;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        public boolean matches(String opt) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            for (String a : aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                if (a.equals(opt)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                } else if (opt.startsWith("--")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                        && (hasArg && opt.startsWith(a + "="))) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        public boolean ignoreRest() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        void process(T task, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            processing.process(task, opt, arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        }
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 38457
diff changeset
   139
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 38457
diff changeset
   140
        public String[] aliases() {
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 38457
diff changeset
   141
            return aliases;
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 38457
diff changeset
   142
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    private static class PlugOption extends Option<PluginsOptions> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        public PlugOption(boolean hasArg,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                Processing<PluginsOptions> processing, boolean hidden, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            super(hasArg, processing, hidden, aliases);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        public PlugOption(boolean hasArg,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                Processing<PluginsOptions> processing, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            super(hasArg, processing, aliases);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    private final class PluginsOptions {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        private static final String PLUGINS_PATH = "--plugin-module-path";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        private static final String POST_PROCESS = "--post-process-path";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        private Layer pluginsLayer = Layer.boot();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        private String lastSorter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        private boolean listPlugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        private Path existingImage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        // plugin to args maps. Each plugin may be used more than once in command line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        // Each such occurrence results in a Map of arguments. So, there could be multiple
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        // args maps per plugin instance.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        private final Map<Plugin, List<Map<String, String>>> pluginToMaps = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        private final List<PlugOption> pluginsOptions = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        private final List<PlugOption> mainOptions = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        private PluginsOptions(String pp) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            if (pp != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                String[] dirs = pp.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                List<Path> paths = new ArrayList<>(dirs.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                for (String dir : dirs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                    paths.add(Paths.get(dir));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                pluginsLayer = createPluginsLayer(paths);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            Set<String> optionsSeen = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
            for (Plugin plugin : PluginRepository.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                    getPlugins(pluginsLayer)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                if (!Utils.isDisabled(plugin)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                    addOrderedPluginOptions(plugin, optionsSeen);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
            mainOptions.add(new PlugOption(false,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                    (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                        // This option is handled prior
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                        // to have the options parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                    },
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39321
diff changeset
   199
                    "--plugin-module-path"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            mainOptions.add(new PlugOption(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                Path path = Paths.get(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                if (!Files.exists(path) || !Files.isDirectory(path)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                    throw newBadArgs("err.existing.image.must.exist");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                existingImage = path.toAbsolutePath();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            }, true, POST_PROCESS));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            mainOptions.add(new PlugOption(true,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                    (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                        lastSorter = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                    },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                    true, "--resources-last-sorter"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
            mainOptions.add(new PlugOption(false,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                    (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                        listPlugins = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                    },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                    "--list-plugins"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        private List<Map<String, String>> argListFor(Plugin plugin) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
            List<Map<String, String>> mapList = pluginToMaps.get(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            if (mapList == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                mapList = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
                pluginToMaps.put(plugin, mapList);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            return mapList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        private void addEmptyArgumentMap(Plugin plugin) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            argListFor(plugin).add(Collections.emptyMap());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        private Map<String, String> addArgumentMap(Plugin plugin) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            Map<String, String> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
            argListFor(plugin).add(map);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            return map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        private void addOrderedPluginOptions(Plugin plugin,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            Set<String> optionsSeen) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            String option = plugin.getOption();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            if (option == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            // make sure that more than one plugin does not use the same option!
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
            if (optionsSeen.contains(option)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
                throw new BadArgs("err.plugin.mutiple.options",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                        option);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
            optionsSeen.add(option);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            PlugOption plugOption
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                    = new PlugOption(plugin.hasArguments(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                            (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                                if (!Utils.isFunctional(plugin)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                                    throw newBadArgs("err.provider.not.functional",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                                            option);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
                                if (! plugin.hasArguments()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                                    addEmptyArgumentMap(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
                                    return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                                Map<String, String> m = addArgumentMap(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                                // handle one or more arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                                if (arg.indexOf(':') == -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                                    // single argument case
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                                    m.put(option, arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                                    // This option can accept more than one arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                                    // like --option_name=arg_value:arg2=value2:arg3=value3
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                                    // ":" followed by word char condition takes care of args that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                                    // like Windows absolute paths "C:\foo", "C:/foo" [cygwin] etc.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                                    // This enforces that key names start with a word character.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
                                    String[] args = arg.split(":(?=\\w)", -1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                                    String firstArg = args[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                                    if (firstArg.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
                                        throw newBadArgs("err.provider.additional.arg.error",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
                                            option, arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
                                    m.put(option, firstArg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                                    // process the additional arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
                                    for (int i = 1; i < args.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                                        String addArg = args[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                                        int eqIdx = addArg.indexOf('=');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                                        if (eqIdx == -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                                            throw newBadArgs("err.provider.additional.arg.error",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                                                option, arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
                                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
                                        String addArgName = addArg.substring(0, eqIdx);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                                        String addArgValue = addArg.substring(eqIdx+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                                        if (addArgName.isEmpty() || addArgValue.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
                                            throw newBadArgs("err.provider.additional.arg.error",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
                                                option, arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
                                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                                        m.put(addArgName, addArgValue);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                            "--" + option);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
            pluginsOptions.add(plugOption);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
            if (Utils.isFunctional(plugin)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                if (Utils.isAutoEnabled(plugin)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                    addEmptyArgumentMap(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                if (plugin instanceof DefaultCompressPlugin) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                    plugOption
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                        = new PlugOption(false,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
                            (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
                                Map<String, String> m = addArgumentMap(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
                                m.put(DefaultCompressPlugin.NAME, DefaultCompressPlugin.LEVEL_2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
                            }, "-c");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                    mainOptions.add(plugOption);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
                } else if (plugin instanceof StripDebugPlugin) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                    plugOption
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
                        = new PlugOption(false,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                            (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
                                addArgumentMap(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                            }, "-G");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                    mainOptions.add(plugOption);
41561
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   326
                } else if (plugin instanceof ExcludeJmodSectionPlugin) {
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   327
                    plugOption = new PlugOption(false, (task, opt, arg) -> {
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   328
                            Map<String, String> m = addArgumentMap(plugin);
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   329
                            m.put(ExcludeJmodSectionPlugin.NAME,
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   330
                                  ExcludeJmodSectionPlugin.MAN_PAGES);
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   331
                        }, "--no-man-pages");
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   332
                    mainOptions.add(plugOption);
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   333
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   334
                    plugOption = new PlugOption(false, (task, opt, arg) -> {
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   335
                        Map<String, String> m = addArgumentMap(plugin);
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   336
                        m.put(ExcludeJmodSectionPlugin.NAME,
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   337
                              ExcludeJmodSectionPlugin.INCLUDE_HEADER_FILES);
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   338
                    }, "--no-header-files");
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   339
                    mainOptions.add(plugOption);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        private PlugOption getOption(String name) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            for (PlugOption o : pluginsOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
                if (o.matches(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
                    return o;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
            for (PlugOption o : mainOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                if (o.matches(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
                    return o;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   358
        private PluginsConfiguration getPluginsConfig(Path output
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   359
                    ) throws IOException, BadArgs {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            if (output != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
                if (Files.exists(output)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                    throw new PluginException(PluginsResourceBundle.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
                            getMessage("err.dir.already.exits", output));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
            List<Plugin> pluginsList = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
            for (Entry<Plugin, List<Map<String, String>>> entry : pluginToMaps.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                Plugin plugin = entry.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                List<Map<String, String>> argsMaps = entry.getValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                // same plugin option may be used multiple times in command line.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                // we call configure once for each occurrence. It is upto the plugin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                // to 'merge' and/or 'override' arguments.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                for (Map<String, String> map : argsMaps) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   376
                    plugin.configure(Collections.unmodifiableMap(map));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
                if (!Utils.isDisabled(plugin)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                    pluginsList.add(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
            // recreate or postprocessing don't require an output directory.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
            ImageBuilder builder = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
            if (output != null) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   387
                builder = new DefaultImageBuilder(output);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            return new Jlink.PluginsConfiguration(pluginsList,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   391
                    builder, lastSorter);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
    private static final class ResourceBundleHelper {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
        private final ResourceBundle bundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
        private final ResourceBundle pluginBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
        ResourceBundleHelper(String path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
            Locale locale = Locale.getDefault();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                bundle = ResourceBundle.getBundle(path, locale);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                pluginBundle = ResourceBundle.getBundle("jdk.tools.jlink.resources.plugins", locale);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
            } catch (MissingResourceException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                throw new InternalError("Cannot find jlink resource bundle for locale " + locale);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
        String getMessage(String key, Object... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
            String val;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
                val = bundle.getString(key);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
            } catch (MissingResourceException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                // XXX OK, check in plugin bundle
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                val = pluginBundle.getString(key);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
            return MessageFormat.format(val, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
    public final class OptionsHelper<T> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
        private final List<Option<T>> options;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
        private String[] command;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        private String defaults;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        OptionsHelper(List<Option<T>> options) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
            this.options = options;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
        private boolean hasArgument(String optionName) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
            Option<?> opt = getOption(optionName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
            if (opt == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
                opt = pluginOptions.getOption(optionName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
                if (opt == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                    throw new BadArgs("err.unknown.option", optionName).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
                            showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
            return opt.hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   445
        public boolean shouldListPlugins() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
            return pluginOptions.listPlugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
        private String getPluginsPath(String[] args) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
            String pp = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
            for (int i = 0; i < args.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
                if (args[i].equals(PluginsOptions.PLUGINS_PATH)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
                    if (i == args.length - 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
                        throw new BadArgs("err.no.plugins.path").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
                        i += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
                        pp = args[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
                        if (!pp.isEmpty() && pp.charAt(0) == '-') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
                            throw new BadArgs("err.no.plugins.path").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
            return pp;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
41766
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   468
        // used by jimage. Return unhandled arguments like "create", "describe".
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
        public List<String> handleOptions(T task, String[] args) throws BadArgs {
41766
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   470
            return handleOptions(task, args, true);
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   471
        }
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   472
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   473
        // used by jlink. No unhandled arguments like "create", "describe".
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   474
        void handleOptionsNoUnhandled(T task, String[] args) throws BadArgs {
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   475
            handleOptions(task, args, false);
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   476
        }
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   477
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   478
        // shared code that handles options for both jlink and jimage. jimage uses arguments like
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   479
        // "create", "describe" etc. as "task names". Those arguments are unhandled here and returned
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   480
        // as "unhandled arguments list". jlink does not want such arguments. "collectUnhandled" flag
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   481
        // tells whether to allow for unhandled arguments or not.
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   482
        private List<String> handleOptions(T task, String[] args, boolean collectUnhandled) throws BadArgs {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
            // findbugs warning, copy instead of keeping a reference.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
            command = Arrays.copyOf(args, args.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
            // Must extract it prior to do any option analysis.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
            // Required to interpret custom plugin options.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
            // Unit tests can call Task multiple time in same JVM.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
            pluginOptions = new PluginsOptions(getPluginsPath(args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            // First extract plugins path if any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            String pp = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
            List<String> filteredArgs = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            for (int i = 0; i < args.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                if (args[i].equals(PluginsOptions.PLUGINS_PATH)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                    if (i == args.length - 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                        throw new BadArgs("err.no.plugins.path").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                        warning("warn.thirdparty.plugins.enabled");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                        log.println(bundleHelper.getMessage("warn.thirdparty.plugins"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                        i += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                        String arg = args[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
                        if (!arg.isEmpty() && arg.charAt(0) == '-') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
                            throw new BadArgs("err.no.plugins.path").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
                        pp = args[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
                    filteredArgs.add(args[i]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
            String[] arr = new String[filteredArgs.size()];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            args = filteredArgs.toArray(arr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
41766
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   515
            List<String> rest = collectUnhandled? new ArrayList<>() : null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
            // process options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
            for (int i = 0; i < args.length; i++) {
41766
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   518
                if (args[i].charAt(0) == '-') {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
                    String name = args[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
                    PlugOption pluginOption = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
                    Option<T> option = getOption(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
                    if (option == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
                        pluginOption = pluginOptions.getOption(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
                        if (pluginOption == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
                            throw new BadArgs("err.unknown.option", name).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                                    showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
                    Option<?> opt = pluginOption == null ? option : pluginOption;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
                    String param = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                    if (opt.hasArg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
                        if (name.startsWith("--") && name.indexOf('=') > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
                            param = name.substring(name.indexOf('=') + 1,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
                                    name.length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
                        } else if (i + 1 < args.length) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
                            param = args[++i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
                        if (param == null || param.isEmpty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
                                || (param.length() >= 2 && param.charAt(0) == '-'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
                                && param.charAt(1) == '-')) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
                            throw new BadArgs("err.missing.arg", name).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
                                    showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
                    if (pluginOption != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
                        pluginOption.process(pluginOptions, name, param);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
                        option.process(task, name, param);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
                    if (opt.ignoreRest()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
                        i = args.length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
                } else {
41766
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   555
                    if (collectUnhandled) {
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   556
                        rest.add(args[i]);
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   557
                    } else {
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   558
                        throw new BadArgs("err.orphan.argument", args[i]).
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   559
                            showUsage(true);
b5eab76a23a9 8166810: jlink should fail on extra arguments
sundar
parents: 41561
diff changeset
   560
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
            return rest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
        private Option<T> getOption(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
            for (Option<T> o : options) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
                if (o.matches(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
                    return o;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
        public void showHelp(String progName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
            showHelp(progName, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
        private void showHelp(String progName, boolean showsImageBuilder) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
            log.println(bundleHelper.getMessage("main.usage", progName));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
            for (Option<?> o : options) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
                String name = o.aliases[0].substring(1); // there must always be at least one name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
                name = name.charAt(0) == '-' ? name.substring(1) : name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
                if (o.isHidden() || name.equals("h")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                log.println(bundleHelper.getMessage("main.opt." + name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
            for (Option<?> o : pluginOptions.mainOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
                if (o.aliases[0].equals(PluginsOptions.POST_PROCESS)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
                        && !showsImageBuilder) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
                String name = o.aliases[0].substring(1); // there must always be at least one name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
                name = name.charAt(0) == '-' ? name.substring(1) : name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
                if (o.isHidden()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
                log.println(bundleHelper.getMessage("plugin.opt." + name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
            log.println(bundleHelper.getMessage("main.command.files"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   606
        public void listPlugins() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
            log.println("\n" + bundleHelper.getMessage("main.extended.help"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
            List<Plugin> pluginList = PluginRepository.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
                    getPlugins(pluginOptions.pluginsLayer);
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   610
            for (Plugin plugin : Utils.getSortedPlugins(pluginList)) {
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   611
                showPlugin(plugin, log);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
            }
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38869
diff changeset
   613
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38869
diff changeset
   614
            log.println("\n" + bundleHelper.getMessage("main.extended.help.footer"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   617
        private void showPlugin(Plugin plugin, PrintWriter log) {
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   618
            if (showsPlugin(plugin)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
                log.println("\n" + bundleHelper.getMessage("main.plugin.name")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
                        + ": " + plugin.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
                // print verbose details for non-builtin plugins
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
                if (!Utils.isBuiltin(plugin)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
                    log.println(bundleHelper.getMessage("main.plugin.class")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
                         + ": " + plugin.getClass().getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
                    log.println(bundleHelper.getMessage("main.plugin.module")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
                         + ": " + plugin.getClass().getModule().getName());
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   628
                    Category category = plugin.getType();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
                    log.println(bundleHelper.getMessage("main.plugin.category")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
                         + ": " + category.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                    log.println(bundleHelper.getMessage("main.plugin.state")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
                        + ": " + plugin.getStateDescription());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                String option = plugin.getOption();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                if (option != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                    log.println(bundleHelper.getMessage("main.plugin.option")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
                        + ": --" + plugin.getOption()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
                        + (plugin.hasArguments()? ("=" + plugin.getArgumentsDescription()) : ""));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
                // description can be long spanning more than one line and so
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
                // print a newline after description label.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
                log.println(bundleHelper.getMessage("main.plugin.description")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
                        + ": " + plugin.getDescription());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
        String[] getInputCommand() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
            return command;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
        String getDefaults() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
            return defaults;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
        public Layer getPluginsLayer() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
            return pluginOptions.pluginsLayer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
    private PluginsOptions pluginOptions;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
    private PrintWriter log;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
    private final ResourceBundleHelper bundleHelper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
    public TaskHelper(String path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
        if (!JLINK_BUNDLE.equals(path) && !JIMAGE_BUNDLE.equals(path)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
            throw new IllegalArgumentException("Invalid Bundle");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
        this.bundleHelper = new ResourceBundleHelper(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
    public <T> OptionsHelper<T> newOptionsHelper(Class<T> clazz,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
            Option<?>[] options) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
        List<Option<T>> optionsList = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
        for (Option<?> o : options) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
            @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
            Option<T> opt = (Option<T>) o;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
            optionsList.add(opt);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
        return new OptionsHelper<>(optionsList);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
    public BadArgs newBadArgs(String key, Object... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
        return new BadArgs(key, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
    public String getMessage(String key, Object... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
        return bundleHelper.getMessage(key, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
    public void setLog(PrintWriter log) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
        this.log = log;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
    public void reportError(String key, Object... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
        log.println(bundleHelper.getMessage("error.prefix") + " "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
                + bundleHelper.getMessage(key, args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
    public void reportUnknownError(String message) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
        log.println(bundleHelper.getMessage("error.prefix") + " " + message);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
    public void warning(String key, Object... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
        log.println(bundleHelper.getMessage("warn.prefix") + " "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                + bundleHelper.getMessage(key, args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   710
    public PluginsConfiguration getPluginsConfig(Path output)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
            throws IOException, BadArgs {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   712
        return pluginOptions.getPluginsConfig(output);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
    public Path getExistingImage() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
        return pluginOptions.existingImage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
    public void showVersion(boolean full) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
        log.println(version(full ? "full" : "release"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
    public String version(String key) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
        return System.getProperty("java.version");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
    static Layer createPluginsLayer(List<Path> paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   729
        Path[] dirs = paths.toArray(new Path[0]);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   730
        ModuleFinder finder = SharedSecrets.getJavaLangModuleAccess()
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   731
            .newModulePath(Runtime.version(), true, dirs);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
        Configuration bootConfiguration = Layer.boot().configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
            Configuration cf = bootConfiguration
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 38320
diff changeset
   736
                .resolveRequiresAndUses(ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
                                        finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
                                        Collections.emptySet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            ClassLoader scl = ClassLoader.getSystemClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
            return Layer.boot().defineModulesWithOneLoader(cf, scl);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
            // Malformed plugin modules (e.g.: same package in multiple modules).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
            throw new PluginException("Invalid modules in the plugins path: " + ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   747
    // Display all plugins
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   748
    private static boolean showsPlugin(Plugin plugin) {
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39150
diff changeset
   749
        return (!Utils.isDisabled(plugin) && plugin.getOption() != null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
}