jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
author mchung
Sun, 19 Jun 2016 16:46:49 -0700
changeset 39125 e97953d38291
parent 38869 4bac8f3bce74
child 39156 92844b32bcec
permissions -rw-r--r--
8155955: packager needs to determine the root modules to create JRE image Reviewed-by: alanb
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.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ResolutionException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.reflect.InvocationTargetException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.Paths;
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    42
import java.util.*;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.internal.module.ConfigurableModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.module.ConfigurableModuleFinder.Phase;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.tools.jlink.internal.TaskHelper.BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.tools.jlink.internal.TaskHelper.Option;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.tools.jlink.internal.ImagePluginStack.ImageProvider;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.tools.jlink.plugin.ExecutableImage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.tools.jlink.Jlink.JlinkConfiguration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.tools.jlink.Jlink.PluginsConfiguration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import jdk.tools.jlink.plugin.PluginException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import jdk.tools.jlink.builder.DefaultImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import jdk.tools.jlink.plugin.Plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 * Implementation for the jlink tool.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 * ## Should use jdk.joptsimple some day.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
public class JlinkTask {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    65
    private static final boolean DEBUG = Boolean.getBoolean("jlink.debug");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    private static <T extends Throwable> void fail(Class<T> type,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            String format,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            Object... args) throws T {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        String msg = new Formatter().format(format, args).toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            T t = type.getConstructor(String.class).newInstance(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            throw t;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        } catch (InstantiationException |
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                InvocationTargetException |
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                NoSuchMethodException |
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                IllegalAccessException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            throw new InternalError("Unable to create an instance of " + type, e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    private static final TaskHelper taskHelper
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            = new TaskHelper(JLINK_BUNDLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    static Option<?>[] recognizedOptions = {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        new Option<JlinkTask>(false, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            task.options.help = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        }, "--help"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            String[] dirs = arg.split(File.pathSeparator);
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    91
            Arrays.stream(dirs)
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    92
                  .map(Paths::get)
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    93
                  .forEach(task.options.modulePath::add);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        }, "--modulepath", "--mp"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            for (String mn : arg.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                if (mn.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                    throw taskHelper.newBadArgs("err.mods.must.be.specified",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                            "--limitmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                task.options.limitMods.add(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        }, "--limitmods"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            for (String mn : arg.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                if (mn.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                    throw taskHelper.newBadArgs("err.mods.must.be.specified",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                            "--addmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                task.options.addMods.add(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        }, "--addmods"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            Path path = Paths.get(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            task.options.output = path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        }, "--output"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            if ("little".equals(arg)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                task.options.endian = ByteOrder.LITTLE_ENDIAN;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            } else if ("big".equals(arg)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                task.options.endian = ByteOrder.BIG_ENDIAN;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        }, "--endian"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        new Option<JlinkTask>(false, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            task.options.version = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        }, "--version"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            Path path = Paths.get(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            if (Files.exists(path)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                throw taskHelper.newBadArgs("err.dir.exists", path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            task.options.packagedModulesPath = path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }, true, "--keep-packaged-modules"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            task.options.saveoptsfile = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        }, "--saveopts"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        new Option<JlinkTask>(false, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            task.options.fullVersion = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }, true, "--fullversion"),};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    private static final String PROGNAME = "jlink";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    private final OptionsValues options = new OptionsValues();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    private static final OptionsHelper<JlinkTask> optionsHelper
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    private PrintWriter log;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    void setLog(PrintWriter out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        log = out;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        taskHelper.setLog(log);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
     * Result codes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    static final int EXIT_OK = 0, // Completed with no errors.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            EXIT_ERROR = 1, // Completed but reported errors.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            EXIT_CMDERR = 2, // Bad command-line arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            EXIT_SYSERR = 3, // System error or resource exhaustion.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            EXIT_ABNORMAL = 4;// terminated abnormally
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    static class OptionsValues {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        boolean help;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        String  saveoptsfile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        boolean version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        boolean fullVersion;
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   169
        List<Path> modulePath = new ArrayList<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        Set<String> limitMods = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        Set<String> addMods = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        Path output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        Path packagedModulesPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        ByteOrder endian = ByteOrder.nativeOrder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    int run(String[] args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        if (log == null) {
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 38457
diff changeset
   179
            setLog(new PrintWriter(System.err, true));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            optionsHelper.handleOptions(this, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
            if (options.help) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                optionsHelper.showHelp(PROGNAME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                return EXIT_OK;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            if (optionsHelper.listPlugins()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                optionsHelper.listPlugins(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                return EXIT_OK;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
            if (options.version || options.fullVersion) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                taskHelper.showVersion(options.fullVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                return EXIT_OK;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
            if (taskHelper.getExistingImage() == null) {
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   196
                if (options.modulePath == null || options.modulePath.isEmpty()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                    throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                createImage();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                postProcessOnly(taskHelper.getExistingImage());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
            if (options.saveoptsfile != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
            return EXIT_OK;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   209
        } catch (UncheckedIOException | PluginException | IllegalArgumentException |
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   210
                 IOException | ResolutionException e) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
            log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage());
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   212
            if (DEBUG) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   213
                e.printStackTrace(log);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   214
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            return EXIT_ERROR;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        } catch (BadArgs e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
            taskHelper.reportError(e.key, e.args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            if (e.showUsage) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   221
            if (DEBUG) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   222
                e.printStackTrace(log);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   223
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            return EXIT_CMDERR;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        } catch (Throwable x) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   226
            log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            x.printStackTrace(log);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            return EXIT_ABNORMAL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            log.flush();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     * Jlink API entry point.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
    public static void createImage(JlinkConfiguration config,
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   238
                                   PluginsConfiguration plugins)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
        Objects.requireNonNull(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        Objects.requireNonNull(config.getOutput());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        plugins = plugins == null ? new PluginsConfiguration() : plugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        if (config.getModulepaths().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            throw new Exception("Empty module paths");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        }
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   247
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        ModuleFinder finder
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   249
                = newModuleFinder(config.getModulepaths(), config.getLimitmods(), config.getModules());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        // First create the image provider
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        ImageProvider imageProvider
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                = createImageProvider(finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                                      checkAddMods(config.getModules()),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                                      config.getLimitmods(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                                      config.getByteOrder(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                                      null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        // Then create the Plugin Stack
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   260
        ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        //Ask the stack to proceed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        stack.operate(imageProvider);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     * Jlink API entry point.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
    public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
        Objects.requireNonNull(image);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
        Objects.requireNonNull(postProcessorPlugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
        ImagePluginStack stack = ImagePluginConfiguration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                parseConfiguration(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        stack.operate((ImagePluginStack stack1) -> image);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    private void postProcessOnly(Path existingImage) throws Exception {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   281
        PluginsConfiguration config = taskHelper.getPluginsConfig(null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        if (img == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            throw taskHelper.newBadArgs("err.existing.image.invalid");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
        postProcessImage(img, config.getPlugins());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    private void createImage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        if (options.output == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
            throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
        ModuleFinder finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                = newModuleFinder(options.modulePath, options.limitMods, options.addMods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            options.addMods = checkAddMods(options.addMods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        } catch (IllegalArgumentException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            throw taskHelper.newBadArgs("err.mods.must.be.specified", "--addmods")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                    .showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        // First create the image provider
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
        ImageProvider imageProvider
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                = createImageProvider(finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                        options.addMods,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                        options.limitMods,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                        options.endian,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                        options.packagedModulesPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        // Then create the Plugin Stack
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        ImagePluginStack stack = ImagePluginConfiguration.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   311
                parseConfiguration(taskHelper.getPluginsConfig(options.output));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        //Ask the stack to proceed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        stack.operate(imageProvider);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
    private static Set<String> checkAddMods(Set<String> addMods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        if (addMods.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            throw new IllegalArgumentException("no modules to add");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        return addMods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   324
    public static ModuleFinder newModuleFinder(List<Path> paths,
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   325
                                               Set<String> limitMods,
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   326
                                               Set<String> addMods)
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   327
    {
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   328
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
   329
        ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[0]));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        // jmods are located at link-time
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
        if (finder instanceof ConfigurableModuleFinder) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
            ((ConfigurableModuleFinder) finder).configurePhase(Phase.LINK_TIME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
        // if limitmods is specified then limit the universe
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
        if (!limitMods.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
            finder = limitFinder(finder, limitMods, addMods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        return finder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   343
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   344
    private static Path toPathLocation(ResolvedModule m) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   345
        Optional<URI> ouri = m.reference().location();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   346
        if (!ouri.isPresent())
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   347
            throw new InternalError(m + " does not have a location");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   348
        URI uri = ouri.get();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   349
        return Paths.get(uri);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   350
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   351
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
    private static ImageProvider createImageProvider(ModuleFinder finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
                                                     Set<String> addMods,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
                                                     Set<String> limitMods,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
                                                     ByteOrder order,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
                                                     Path retainModulesPath)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
        if (addMods.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            throw new IllegalArgumentException("empty modules and limitmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
        Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                .resolveRequires(finder,
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37779
diff changeset
   365
                                 ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                                 addMods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   368
        Map<String, Path> mods = cf.modules().stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   369
            .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
        return new ImageHelper(cf, mods, order, retainModulesPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     * Returns a ModuleFinder that limits observability to the given root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     * modules, their transitive dependences, plus a set of other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
    private static ModuleFinder limitFinder(ModuleFinder finder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
            Set<String> roots,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
            Set<String> otherMods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
        // resolve all root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                .resolveRequires(finder,
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 37779
diff changeset
   384
                                 ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                                 roots);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        // module name -> reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
        Map<String, ModuleReference> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        cf.modules().forEach(m -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            ModuleReference mref = m.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
            map.put(mref.descriptor().name(), mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   394
        // add the other modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   395
        otherMods.stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   396
            .map(finder::find)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   397
            .flatMap(Optional::stream)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   398
            .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   399
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
        // set of modules that are observable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
        Set<ModuleReference> mrefs = new HashSet<>(map.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                return Optional.ofNullable(map.get(name));
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
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                return mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
    private String getSaveOpts() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
        sb.append('#').append(new Date()).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
        for (String c : optionsHelper.getInputCommand()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
            sb.append(c).append(" ");
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
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
    private static String getBomHeader() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
        sb.append("#").append(new Date()).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        sb.append("#Please DO NOT Modify this file").append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        return sb.toString();
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 String genBOMContent() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        sb.append(getBomHeader());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
        StringBuilder command = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
        for (String c : optionsHelper.getInputCommand()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
            command.append(c).append(" ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        sb.append("command").append(" = ").append(command);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
        sb.append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
    private static String genBOMContent(JlinkConfiguration config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            PluginsConfiguration plugins)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
            throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
        sb.append(getBomHeader());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        sb.append(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        sb.append(plugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
    private static class ImageHelper implements ImageProvider {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
        final Set<Archive> archives;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
        final ByteOrder order;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
        final Path packagedModulesPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
        ImageHelper(Configuration cf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                    Map<String, Path> modsPaths,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                    ByteOrder order,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                    Path packagedModulesPath) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
            archives = modsPaths.entrySet().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                                .map(e -> newArchive(e.getKey(), e.getValue()))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                                .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
            this.order = order;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
            this.packagedModulesPath = packagedModulesPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        private Archive newArchive(String module, Path path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            if (path.toString().endsWith(".jmod")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
                return new JmodArchive(module, path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
            } else if (path.toString().endsWith(".jar")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                return new ModularJarArchive(module, path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
            } else if (Files.isDirectory(path)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                return new DirArchive(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
                fail(RuntimeException.class,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
                        "Selected module %s (%s) not in jmod or modular jar format",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
                        module,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
                        path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
        public ExecutableImage retrieve(ImagePluginStack stack) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            ExecutableImage image = ImageFileCreator.create(archives, order, stack);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            if (packagedModulesPath != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
                // copy the packaged modules to the given path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
                Files.createDirectories(packagedModulesPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                for (Archive a : archives) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                    Path file = a.getPath();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                    Path dest = packagedModulesPath.resolve(file.getFileName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                    Files.copy(file, dest);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
            return image;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
    private static enum Section {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
        NATIVE_LIBS("native", nativeDir()),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        NATIVE_CMDS("bin", "bin"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        CLASSES("classes", "classes"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
        CONFIG("conf", "conf"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
        UNKNOWN("unknown", "unknown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        private static String nativeDir() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            if (System.getProperty("os.name").startsWith("Windows")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
                return "bin";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
                return "lib";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        private final String jmodDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
        private final String imageDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
        Section(String jmodDir, String imageDir) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            this.jmodDir = jmodDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            this.imageDir = imageDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
        String imageDir() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            return imageDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
        String jmodDir() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            return jmodDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        boolean matches(String path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
            return path.startsWith(jmodDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        static Section getSectionFromName(String dir) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            if (Section.NATIVE_LIBS.matches(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
                return Section.NATIVE_LIBS;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
            } else if (Section.NATIVE_CMDS.matches(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
                return Section.NATIVE_CMDS;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            } else if (Section.CLASSES.matches(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
                return Section.CLASSES;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            } else if (Section.CONFIG.matches(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
                return Section.CONFIG;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                return Section.UNKNOWN;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
}