src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
author mchung
Wed, 31 Jan 2018 15:15:09 -0800
changeset 48692 60c19c384333
parent 48543 7067fe4e054e
permissions -rw-r--r--
8196310: jlink --suggest-providers fails with missing resource if --output specified Reviewed-by: alanb, lancea, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48327
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
    27
import java.io.BufferedInputStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.PrintWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.Configuration;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42762
diff changeset
    33
import java.lang.module.FindException;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
    34
import java.lang.module.ModuleDescriptor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.lang.module.ResolutionException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.nio.file.Paths;
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    44
import java.util.ArrayList;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    45
import java.util.Arrays;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    46
import java.util.Comparator;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    47
import java.util.Date;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    48
import java.util.HashMap;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    49
import java.util.HashSet;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    50
import java.util.List;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    51
import java.util.Locale;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    52
import java.util.Map;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    53
import java.util.Objects;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    54
import java.util.Optional;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
    55
import java.util.Set;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import java.util.stream.Collectors;
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
    57
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.tools.jlink.internal.TaskHelper.BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 41484
diff changeset
    61
import jdk.tools.jlink.internal.Jlink.JlinkConfiguration;
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 41484
diff changeset
    62
import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import jdk.tools.jlink.internal.TaskHelper.Option;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
import jdk.tools.jlink.internal.ImagePluginStack.ImageProvider;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
import jdk.tools.jlink.plugin.PluginException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
import jdk.tools.jlink.builder.DefaultImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
import jdk.tools.jlink.plugin.Plugin;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
    69
import jdk.internal.module.ModulePath;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
    70
import jdk.internal.module.ModuleResolution;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * Implementation for the jlink tool.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * ## Should use jdk.joptsimple some day.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
public class JlinkTask {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    78
    static final boolean DEBUG = Boolean.getBoolean("jlink.debug");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
    80
    // jlink API ignores by default. Remove when signing is implemented.
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
    81
    static final boolean IGNORE_SIGNING_DEFAULT = true;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private static final TaskHelper taskHelper
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            = new TaskHelper(JLINK_BUNDLE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39156
diff changeset
    86
    private static final Option<?>[] recognizedOptions = {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        new Option<JlinkTask>(false, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            task.options.help = true;
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48327
diff changeset
    89
        }, "--help", "-h", "-?"),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        new Option<JlinkTask>(true, (task, opt, arg) -> {
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
    91
            // if used multiple times, the last one wins!
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
    92
            // So, clear previous values, if any.
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
    93
            task.options.modulePath.clear();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            String[] dirs = arg.split(File.pathSeparator);
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
    95
            int i = 0;
39125
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    96
            Arrays.stream(dirs)
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    97
                  .map(Paths::get)
e97953d38291 8155955: packager needs to determine the root modules to create JRE image
mchung
parents: 38869
diff changeset
    98
                  .forEach(task.options.modulePath::add);
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
    99
        }, "--module-path", "-p"),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        new Option<JlinkTask>(true, (task, opt, arg) -> {
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   101
            // if used multiple times, the last one wins!
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   102
            // So, clear previous values, if any.
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   103
            task.options.limitMods.clear();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            for (String mn : arg.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                if (mn.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                    throw taskHelper.newBadArgs("err.mods.must.be.specified",
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
   107
                            "--limit-modules");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                task.options.limitMods.add(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            }
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
   111
        }, "--limit-modules"),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            for (String mn : arg.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                if (mn.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                    throw taskHelper.newBadArgs("err.mods.must.be.specified",
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
   116
                            "--add-modules");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                task.options.addMods.add(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            }
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
   120
        }, "--add-modules"),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            Path path = Paths.get(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            task.options.output = path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        }, "--output"),
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   125
        new Option<JlinkTask>(false, (task, opt, arg) -> {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   126
            task.options.bindServices = true;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   127
        }, "--bind-services"),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   128
        new Option<JlinkTask>(false, (task, opt, arg) -> {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   129
            task.options.suggestProviders = true;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   130
        }, "--suggest-providers", "", true),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        new Option<JlinkTask>(true, (task, opt, arg) -> {
42762
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   132
            String[] values = arg.split("=");
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   133
            // check values
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   134
            if (values.length != 2 || values[0].isEmpty() || values[1].isEmpty()) {
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   135
                throw taskHelper.newBadArgs("err.launcher.value.format", arg);
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   136
            } else {
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   137
                String commandName = values[0];
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   138
                String moduleAndMain = values[1];
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   139
                int idx = moduleAndMain.indexOf("/");
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   140
                if (idx != -1) {
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   141
                    if (moduleAndMain.substring(0, idx).isEmpty()) {
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   142
                        throw taskHelper.newBadArgs("err.launcher.module.name.empty", arg);
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   143
                    }
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   144
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   145
                    if (moduleAndMain.substring(idx + 1).isEmpty()) {
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   146
                        throw taskHelper.newBadArgs("err.launcher.main.class.empty", arg);
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   147
                    }
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   148
                }
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   149
                task.options.launchers.put(commandName, moduleAndMain);
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   150
            }
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   151
        }, "--launcher"),
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   152
        new Option<JlinkTask>(true, (task, opt, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            if ("little".equals(arg)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                task.options.endian = ByteOrder.LITTLE_ENDIAN;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            } else if ("big".equals(arg)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                task.options.endian = ByteOrder.BIG_ENDIAN;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        }, "--endian"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        new Option<JlinkTask>(false, (task, opt, arg) -> {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   162
            task.options.verbose = true;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   163
        }, "--verbose", "-v"),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   164
        new Option<JlinkTask>(false, (task, opt, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            task.options.version = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }, "--version"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            Path path = Paths.get(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            if (Files.exists(path)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                throw taskHelper.newBadArgs("err.dir.exists", path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            task.options.packagedModulesPath = path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        }, true, "--keep-packaged-modules"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        new Option<JlinkTask>(true, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            task.options.saveoptsfile = arg;
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39834
diff changeset
   176
        }, "--save-opts"),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        new Option<JlinkTask>(false, (task, opt, arg) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            task.options.fullVersion = true;
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   179
        }, true, "--full-version"),
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   180
        new Option<JlinkTask>(false, (task, opt, arg) -> {
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   181
            task.options.ignoreSigning = true;
42174
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   182
        }, "--ignore-signing-information"),};
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
    private static final String PROGNAME = "jlink";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    private final OptionsValues options = new OptionsValues();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    private static final OptionsHelper<JlinkTask> optionsHelper
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
            = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
    private PrintWriter log;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   191
    void setLog(PrintWriter out, PrintWriter err) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        log = out;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        taskHelper.setLog(log);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
     * Result codes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
     */
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   199
    static final int
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   200
            EXIT_OK = 0, // Completed with no errors.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            EXIT_ERROR = 1, // Completed but reported errors.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            EXIT_CMDERR = 2, // Bad command-line arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            EXIT_SYSERR = 3, // System error or resource exhaustion.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
            EXIT_ABNORMAL = 4;// terminated abnormally
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
    static class OptionsValues {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        boolean help;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        String  saveoptsfile;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   209
        boolean verbose;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        boolean version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
        boolean fullVersion;
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   212
        final List<Path> modulePath = new ArrayList<>();
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   213
        final Set<String> limitMods = new HashSet<>();
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   214
        final Set<String> addMods = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        Path output;
42762
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   216
        final Map<String, String> launchers = new HashMap<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        Path packagedModulesPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        ByteOrder endian = ByteOrder.nativeOrder();
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   219
        boolean ignoreSigning = false;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   220
        boolean bindServices = false;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   221
        boolean suggestProviders = false;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
    int run(String[] args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        if (log == null) {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   226
            setLog(new PrintWriter(System.out, true),
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   227
                   new PrintWriter(System.err, true));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        try {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   230
            List<String> remaining = optionsHelper.handleOptions(this, args);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   231
            if (remaining.size() > 0 && !options.suggestProviders) {
48692
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   232
                throw taskHelper.newBadArgs("err.orphan.arguments",
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   233
                                            remaining.stream().collect(Collectors.joining(" ")))
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   234
                                .showUsage(true);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   235
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
            if (options.help) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                optionsHelper.showHelp(PROGNAME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                return EXIT_OK;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            }
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
   240
            if (optionsHelper.shouldListPlugins()) {
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
   241
                optionsHelper.listPlugins();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                return EXIT_OK;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            if (options.version || options.fullVersion) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
                taskHelper.showVersion(options.fullVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                return EXIT_OK;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   248
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   249
            if (taskHelper.getExistingImage() != null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                postProcessOnly(taskHelper.getExistingImage());
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   251
                return EXIT_OK;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   254
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   255
            if (options.modulePath.isEmpty()) {
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   256
                // no --module-path specified - try to set $JAVA_HOME/jmods if that exists
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   257
                Path jmods = getDefaultModulePath();
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   258
                if (jmods != null) {
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   259
                    options.modulePath.add(jmods);
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   260
                }
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   261
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   262
                if (options.modulePath.isEmpty()) {
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   263
                     throw taskHelper.newBadArgs("err.modulepath.must.be.specified")
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   264
                                 .showUsage(true);
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   265
                }
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   266
            }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   267
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   268
            JlinkConfiguration config = initJlinkConfig();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   269
            if (options.suggestProviders) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   270
                suggestProviders(config, remaining);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   271
            } else {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   272
                createImage(config);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   273
                if (options.saveoptsfile != null) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   274
                    Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   275
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            return EXIT_OK;
41561
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41559
diff changeset
   279
        } catch (PluginException | IllegalArgumentException |
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42762
diff changeset
   280
                 UncheckedIOException |IOException | FindException | ResolutionException e) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage());
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   282
            if (DEBUG) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   283
                e.printStackTrace(log);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   284
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            return EXIT_ERROR;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
        } catch (BadArgs e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            taskHelper.reportError(e.key, e.args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
            if (e.showUsage) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   291
            if (DEBUG) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   292
                e.printStackTrace(log);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   293
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
            return EXIT_CMDERR;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        } catch (Throwable x) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   296
            log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            x.printStackTrace(log);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            return EXIT_ABNORMAL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            log.flush();
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
     * Jlink API entry point.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
    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
   308
                                   PluginsConfiguration plugins)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        Objects.requireNonNull(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        Objects.requireNonNull(config.getOutput());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        plugins = plugins == null ? new PluginsConfiguration() : plugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        // First create the image provider
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   315
        ImageProvider imageProvider =
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   316
                createImageProvider(config,
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   317
                                    null,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   318
                                    IGNORE_SIGNING_DEFAULT,
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   319
                                    false,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   320
                                    false,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   321
                                    null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        // Then create the Plugin Stack
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   324
        ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
        //Ask the stack to proceed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        stack.operate(imageProvider);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
     * Jlink API entry point.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
    public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        Objects.requireNonNull(image);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
        Objects.requireNonNull(postProcessorPlugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
        PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
        ImagePluginStack stack = ImagePluginConfiguration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                parseConfiguration(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        stack.operate((ImagePluginStack stack1) -> image);
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 void postProcessOnly(Path existingImage) throws Exception {
42762
11a1ad340d4f 8170289: Re-examine entry point support in jlink
sundar
parents: 42703
diff changeset
   345
        PluginsConfiguration config = taskHelper.getPluginsConfig(null, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
        ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        if (img == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
            throw taskHelper.newBadArgs("err.existing.image.invalid");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        postProcessImage(img, config.getPlugins());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   353
    // the token for "all modules on the module path"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   354
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   355
    private JlinkConfiguration initJlinkConfig() throws BadArgs {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   356
        Set<String> roots = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   357
        for (String mod : options.addMods) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   358
            if (mod.equals(ALL_MODULE_PATH)) {
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   359
                ModuleFinder finder = newModuleFinder(options.modulePath, options.limitMods, Set.of());
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   360
                // all observable modules are roots
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   361
                finder.findAll()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   362
                      .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   363
                      .map(ModuleReference::descriptor)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   364
                      .map(ModuleDescriptor::name)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   365
                      .forEach(mn -> roots.add(mn));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   366
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   367
                roots.add(mod);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   368
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   369
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   370
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   371
        ModuleFinder finder = newModuleFinder(options.modulePath, options.limitMods, roots);
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   372
        if (!finder.find("java.base").isPresent()) {
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   373
            Path defModPath = getDefaultModulePath();
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   374
            if (defModPath != null) {
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   375
                options.modulePath.add(defModPath);
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   376
            }
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   377
            finder = newModuleFinder(options.modulePath, options.limitMods, roots);
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   378
        }
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   379
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   380
        return new JlinkConfiguration(options.output,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   381
                                      roots,
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   382
                                      options.endian,
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   383
                                      finder);
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   384
    }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   385
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   386
    private void createImage(JlinkConfiguration config) throws Exception {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   387
        if (options.output == null) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   388
            throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   389
        }
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   390
        if (options.addMods.isEmpty()) {
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   391
            throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules")
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   392
                            .showUsage(true);
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   393
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   394
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
        // First create the image provider
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   396
        ImageProvider imageProvider = createImageProvider(config,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41919
diff changeset
   397
                                                          options.packagedModulesPath,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   398
                                                          options.ignoreSigning,
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   399
                                                          options.bindServices,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   400
                                                          options.verbose,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   401
                                                          log);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
        // Then create the Plugin Stack
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   404
        ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   405
            taskHelper.getPluginsConfig(options.output, options.launchers));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
        //Ask the stack to proceed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
        stack.operate(imageProvider);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   411
    /**
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   412
     * @return the system module path or null
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   413
     */
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   414
    public static Path getDefaultModulePath() {
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   415
        Path jmods = Paths.get(System.getProperty("java.home"), "jmods");
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   416
        return Files.isDirectory(jmods)? jmods : null;
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   417
    }
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   418
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   419
    /*
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   420
     * Returns a module finder of the given module path that limits
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   421
     * the observable modules to those in the transitive closure of
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   422
     * the modules specified in {@code limitMods} plus other modules
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   423
     * specified in the {@code roots} set.
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   424
     *
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   425
     * @throws IllegalArgumentException if java.base module is present
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   426
     * but its descriptor has no version
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   427
     */
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   428
    public static ModuleFinder newModuleFinder(List<Path> paths,
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   429
                                               Set<String> limitMods,
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   430
                                               Set<String> roots)
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   431
    {
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   432
        if (Objects.requireNonNull(paths).isEmpty()) {
48071
a5cadeee8763 8185130: jlink should throw error if target image and current JDK versions don't match
sundar
parents: 47737
diff changeset
   433
             throw new IllegalArgumentException(taskHelper.getMessage("err.empty.module.path"));
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   434
        }
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   435
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   436
        Path[] entries = paths.toArray(new Path[0]);
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   437
        Runtime.Version version = Runtime.version();
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   438
        ModuleFinder finder = ModulePath.of(version, true, entries);
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   439
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   440
        if (finder.find("java.base").isPresent()) {
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   441
            // use the version of java.base module, if present, as
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   442
            // the release version for multi-release JAR files
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   443
            ModuleDescriptor.Version v = finder.find("java.base").get()
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   444
                .descriptor().version().orElseThrow(() ->
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   445
                    new IllegalArgumentException("No version in java.base descriptor")
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   446
                );
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   447
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   448
            // java.base version is different than the current runtime version
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   449
            version = Runtime.Version.parse(v.toString());
48327
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 48223
diff changeset
   450
            if (Runtime.version().feature() != version.feature() ||
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 48223
diff changeset
   451
                Runtime.version().interim() != version.interim())
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 48223
diff changeset
   452
            {
48071
a5cadeee8763 8185130: jlink should throw error if target image and current JDK versions don't match
sundar
parents: 47737
diff changeset
   453
                // jlink version and java.base version do not match.
a5cadeee8763 8185130: jlink should throw error if target image and current JDK versions don't match
sundar
parents: 47737
diff changeset
   454
                // We do not (yet) support this mode.
a5cadeee8763 8185130: jlink should throw error if target image and current JDK versions don't match
sundar
parents: 47737
diff changeset
   455
                throw new IllegalArgumentException(taskHelper.getMessage("err.jlink.version.mismatch",
48327
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 48223
diff changeset
   456
                    Runtime.version().feature(), Runtime.version().interim(),
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 48223
diff changeset
   457
                    version.feature(), version.interim()));
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   458
            }
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   459
        }
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   460
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   461
        // if limitmods is specified then limit the universe
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   462
        if (limitMods != null && !limitMods.isEmpty()) {
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47399
diff changeset
   463
            finder = limitFinder(finder, limitMods, Objects.requireNonNull(roots));
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   464
        }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   465
        return finder;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41766
diff changeset
   466
    }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   467
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   468
    private static Path toPathLocation(ResolvedModule m) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   469
        Optional<URI> ouri = m.reference().location();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   470
        if (!ouri.isPresent())
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   471
            throw new InternalError(m + " does not have a location");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   472
        URI uri = ouri.get();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   473
        return Paths.get(uri);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   474
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   475
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   476
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   477
    private static ImageProvider createImageProvider(JlinkConfiguration config,
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   478
                                                     Path retainModulesPath,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   479
                                                     boolean ignoreSigning,
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   480
                                                     boolean bindService,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   481
                                                     boolean verbose,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   482
                                                     PrintWriter log)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
            throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
    {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   485
        Configuration cf = bindService ? config.resolveAndBind()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   486
                                       : config.resolve();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
47399
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   488
        cf.modules().stream()
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   489
            .map(ResolvedModule::reference)
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   490
            .filter(mref -> mref.descriptor().isAutomatic())
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   491
            .findAny()
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   492
            .ifPresent(mref -> {
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   493
                String loc = mref.location().map(URI::toString).orElse("<unknown>");
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   494
                throw new IllegalArgumentException(
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   495
                    taskHelper.getMessage("err.automatic.module", mref.descriptor().name(), loc));
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   496
            });
fb677b3f0888 8189671: jlink should clearly report error when an automatic module is used
sundar
parents: 47216
diff changeset
   497
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   498
        if (verbose && log != null) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   499
            // print modules to be linked in
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   500
            cf.modules().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   501
              .sorted(Comparator.comparing(ResolvedModule::name))
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   502
              .forEach(rm -> log.format("%s %s%n",
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   503
                                        rm.name(), rm.reference().location().get()));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   504
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   505
            // print provider info
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   506
            Set<ModuleReference> references = cf.modules().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   507
                .map(ResolvedModule::reference).collect(Collectors.toSet());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   508
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   509
            String msg = String.format("%n%s:", taskHelper.getMessage("providers.header"));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   510
            printProviders(log, msg, references);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   511
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   513
        // emit a warning for any incubating modules in the configuration
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   514
        if (log != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   515
            String im = cf.modules()
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   516
                          .stream()
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   517
                          .map(ResolvedModule::reference)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   518
                          .filter(ModuleResolution::hasIncubatingWarning)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   519
                          .map(ModuleReference::descriptor)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   520
                          .map(ModuleDescriptor::name)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   521
                          .collect(Collectors.joining(", "));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   522
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   523
            if (!"".equals(im))
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   524
                log.println("WARNING: Using incubator modules: " + im);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   525
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42339
diff changeset
   526
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   527
        Map<String, Path> mods = cf.modules().stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   528
            .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation));
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   529
        return new ImageHelper(cf, mods, config.getByteOrder(), retainModulesPath, ignoreSigning);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   532
    /*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
     * Returns a ModuleFinder that limits observability to the given root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
     * modules, their transitive dependences, plus a set of other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
     */
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   536
    public static ModuleFinder limitFinder(ModuleFinder finder,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   537
                                           Set<String> roots,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   538
                                           Set<String> otherMods) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        // resolve all root modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
        Configuration cf = Configuration.empty()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42762
diff changeset
   542
                .resolve(finder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42762
diff changeset
   543
                         ModuleFinder.of(),
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42762
diff changeset
   544
                         roots);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
        // module name -> reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
        Map<String, ModuleReference> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
        cf.modules().forEach(m -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
            ModuleReference mref = m.reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
            map.put(mref.descriptor().name(), mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   553
        // add the other modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   554
        otherMods.stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   555
            .map(finder::find)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   556
            .flatMap(Optional::stream)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   557
            .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   558
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        // set of modules that are observable
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
        Set<ModuleReference> mrefs = new HashSet<>(map.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
                return Optional.ofNullable(map.get(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
                return mrefs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   575
    /*
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   576
     * Returns a map of each service type to the modules that use it
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   577
     * It will include services that are provided by a module but may not used
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   578
     * by any of the observable modules.
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   579
     */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   580
    private static Map<String, Set<String>> uses(Set<ModuleReference> modules) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   581
        // collects the services used by the modules and print uses
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   582
        Map<String, Set<String>> services = new HashMap<>();
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   583
        modules.stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   584
               .map(ModuleReference::descriptor)
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   585
               .forEach(md -> {
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   586
                   // include services that may not be used by any observable modules
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   587
                   md.provides().forEach(p ->
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   588
                       services.computeIfAbsent(p.service(), _k -> new HashSet<>()));
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   589
                   md.uses().forEach(s -> services.computeIfAbsent(s, _k -> new HashSet<>())
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   590
                                                  .add(md.name()));
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   591
               });
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   592
        return services;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   593
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   594
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   595
    private static void printProviders(PrintWriter log,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   596
                                       String header,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   597
                                       Set<ModuleReference> modules) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   598
        printProviders(log, header, modules, uses(modules));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   599
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   600
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   601
    /*
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   602
     * Prints the providers that are used by the specified services.
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   603
     *
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   604
     * The specified services maps a service type name to the modules
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   605
     * using the service type which may be empty if no observable module uses
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   606
     * that service.
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   607
     */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   608
    private static void printProviders(PrintWriter log,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   609
                                       String header,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   610
                                       Set<ModuleReference> modules,
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   611
                                       Map<String, Set<String>> serviceToUses) {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   612
        if (modules.isEmpty())
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   613
            return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   614
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   615
        // Build a map of a service type to the provider modules
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   616
        Map<String, Set<ModuleDescriptor>> providers = new HashMap<>();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   617
        modules.stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   618
            .map(ModuleReference::descriptor)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   619
            .forEach(md -> {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   620
                md.provides().stream()
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   621
                  .filter(p -> serviceToUses.containsKey(p.service()))
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   622
                  .forEach(p -> providers.computeIfAbsent(p.service(), _k -> new HashSet<>())
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   623
                                         .add(md));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   624
            });
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   625
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   626
        if (!providers.isEmpty()) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   627
            log.println(header);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   628
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   629
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   630
        // print the providers of the service types used by the specified modules
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   631
        // sorted by the service type name and then provider's module name
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   632
        providers.entrySet().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   633
            .sorted(Map.Entry.comparingByKey())
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   634
            .forEach(e -> {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   635
                String service = e.getKey();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   636
                e.getValue().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   637
                 .sorted(Comparator.comparing(ModuleDescriptor::name))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   638
                 .forEach(md ->
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   639
                     md.provides().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   640
                       .filter(p -> p.service().equals(service))
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   641
                       .forEach(p -> {
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   642
                           String usedBy;
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   643
                           if (serviceToUses.get(p.service()).isEmpty()) {
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   644
                               usedBy = "not used by any observable module";
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   645
                           } else {
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   646
                               usedBy = serviceToUses.get(p.service()).stream()
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   647
                                            .sorted()
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   648
                                            .collect(Collectors.joining(",", "used by ", ""));
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   649
                           }
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   650
                           log.format("  %s provides %s %s%n",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   651
                                      md.name(), p.service(), usedBy);
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   652
                       })
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   653
                 );
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   654
            });
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   655
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   656
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   657
    private void suggestProviders(JlinkConfiguration config, List<String> args)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   658
        throws BadArgs
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   659
    {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   660
        if (args.size() > 1) {
48692
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   661
            List<String> arguments = args.get(0).startsWith("-")
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   662
                                        ? args
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   663
                                        : args.subList(1, args.size());
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   664
            throw taskHelper.newBadArgs("err.invalid.arg.for.option",
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   665
                                        "--suggest-providers",
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   666
                                        arguments.stream().collect(Collectors.joining(" ")));
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   667
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   668
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   669
        if (options.bindServices) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   670
            log.println(taskHelper.getMessage("no.suggested.providers"));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   671
            return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   672
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   673
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   674
        ModuleFinder finder = config.finder();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   675
        if (args.isEmpty()) {
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   676
            // print providers used by the observable modules without service binding
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   677
            Set<ModuleReference> mrefs = finder.findAll();
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   678
            // print uses of the modules that would be linked into the image
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   679
            mrefs.stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   680
                 .sorted(Comparator.comparing(mref -> mref.descriptor().name()))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   681
                 .forEach(mref -> {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   682
                     ModuleDescriptor md = mref.descriptor();
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   683
                     log.format("%s %s%n", md.name(),
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   684
                                mref.location().get());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   685
                     md.uses().stream().sorted()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   686
                       .forEach(s -> log.format("    uses %s%n", s));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   687
                 });
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   688
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   689
            String msg = String.format("%n%s:", taskHelper.getMessage("suggested.providers.header"));
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   690
            printProviders(log, msg, mrefs, uses(mrefs));
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   691
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   692
        } else {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   693
            // comma-separated service types, if specified
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   694
            Set<String> names = Stream.of(args.get(0).split(","))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   695
                .collect(Collectors.toSet());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   696
            // find the modules that provide the specified service
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   697
            Set<ModuleReference> mrefs = finder.findAll().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   698
                .filter(mref -> mref.descriptor().provides().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   699
                                    .map(ModuleDescriptor.Provides::service)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   700
                                    .anyMatch(names::contains))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   701
                .collect(Collectors.toSet());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   702
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   703
            // find the modules that uses the specified services
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   704
            Map<String, Set<String>> uses = new HashMap<>();
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   705
            names.forEach(s -> uses.computeIfAbsent(s, _k -> new HashSet<>()));
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   706
            finder.findAll().stream()
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   707
                  .map(ModuleReference::descriptor)
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   708
                  .forEach(md -> md.uses().stream()
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   709
                                   .filter(names::contains)
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   710
                                   .forEach(s -> uses.get(s).add(md.name())));
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   711
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   712
            // check if any name given on the command line are not provided by any module
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   713
            mrefs.stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   714
                 .flatMap(mref -> mref.descriptor().provides().stream()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   715
                                      .map(ModuleDescriptor.Provides::service))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   716
                 .forEach(names::remove);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   717
            if (!names.isEmpty()) {
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   718
                log.println(taskHelper.getMessage("warn.provider.notfound",
48692
60c19c384333 8196310: jlink --suggest-providers fails with missing resource if --output specified
mchung
parents: 48543
diff changeset
   719
                    names.stream().sorted().collect(Collectors.joining(","))));
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   720
            }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   721
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   722
            String msg = String.format("%n%s:", taskHelper.getMessage("suggested.providers.header"));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   723
            printProviders(log, msg, mrefs, uses);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   724
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   725
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 44359
diff changeset
   726
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
    private String getSaveOpts() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
        sb.append('#').append(new Date()).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
        for (String c : optionsHelper.getInputCommand()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
            sb.append(c).append(" ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
    private static String getBomHeader() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
        sb.append("#").append(new Date()).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
        sb.append("#Please DO NOT Modify this file").append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
    private String genBOMContent() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
        sb.append(getBomHeader());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
        StringBuilder command = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
        for (String c : optionsHelper.getInputCommand()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
            command.append(c).append(" ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
        sb.append("command").append(" = ").append(command);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
        sb.append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
    private static String genBOMContent(JlinkConfiguration config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
            PluginsConfiguration plugins)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
            throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
        sb.append(getBomHeader());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
        sb.append(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
        sb.append(plugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
    private static class ImageHelper implements ImageProvider {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
        final ByteOrder order;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
        final Path packagedModulesPath;
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   770
        final boolean ignoreSigning;
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   771
        final Runtime.Version version;
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   772
        final Set<Archive> archives;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
        ImageHelper(Configuration cf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
                    Map<String, Path> modsPaths,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
                    ByteOrder order,
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   777
                    Path packagedModulesPath,
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   778
                    boolean ignoreSigning) throws IOException {
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   779
            this.order = order;
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   780
            this.packagedModulesPath = packagedModulesPath;
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   781
            this.ignoreSigning = ignoreSigning;
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   782
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   783
            // use the version of java.base module, if present, as
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   784
            // the release version for multi-release JAR files
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   785
            this.version = cf.findModule("java.base")
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   786
                .map(ResolvedModule::reference)
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   787
                .map(ModuleReference::descriptor)
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   788
                .flatMap(ModuleDescriptor::version)
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   789
                .map(ModuleDescriptor.Version::toString)
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   790
                .map(Runtime.Version::parse)
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   791
                .orElse(Runtime.version());
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   792
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   793
            this.archives = modsPaths.entrySet().stream()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
                                .map(e -> newArchive(e.getKey(), e.getValue()))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
                                .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
        private Archive newArchive(String module, Path path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   799
            if (path.toString().endsWith(".jmod")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
                return new JmodArchive(module, path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
            } else if (path.toString().endsWith(".jar")) {
47737
acfedb75dd51 8177471: jlink should use the version from java.base.jmod to find modules
sundar
parents: 47464
diff changeset
   802
                ModularJarArchive modularJarArchive = new ModularJarArchive(module, path, version);
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   803
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   804
                Stream<Archive.Entry> signatures = modularJarArchive.entries().filter((entry) -> {
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   805
                    String name = entry.name().toUpperCase(Locale.ENGLISH);
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   806
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   807
                    return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   808
                                name.endsWith(".SF") ||
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   809
                                name.endsWith(".DSA") ||
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   810
                                name.endsWith(".RSA") ||
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   811
                                name.endsWith(".EC") ||
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   812
                                name.startsWith("META-INF/SIG-")
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   813
                            );
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   814
                });
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   815
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   816
                if (signatures.count() != 0) {
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   817
                    if (ignoreSigning) {
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   818
                        System.err.println(taskHelper.getMessage("warn.signing", path));
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   819
                    } else {
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   820
                        throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   821
                    }
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   822
                }
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   823
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   824
                return modularJarArchive;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   825
            } else if (Files.isDirectory(path)) {
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   826
                Path modInfoPath = path.resolve("module-info.class");
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   827
                if (Files.isRegularFile(modInfoPath)) {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   828
                    return new DirArchive(path, findModuleName(modInfoPath));
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   829
                } else {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   830
                    throw new IllegalArgumentException(
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   831
                        taskHelper.getMessage("err.not.a.module.directory", path));
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   832
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   833
            } else {
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   834
                throw new IllegalArgumentException(
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 41831
diff changeset
   835
                    taskHelper.getMessage("err.not.modular.format", module, path));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   836
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   837
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   838
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   839
        private static String findModuleName(Path modInfoPath) {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   840
            try (BufferedInputStream bis = new BufferedInputStream(
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   841
                    Files.newInputStream(modInfoPath))) {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   842
                return ModuleDescriptor.read(bis).name();
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   843
            } catch (IOException exp) {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   844
                throw new IllegalArgumentException(taskHelper.getMessage(
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   845
                    "err.cannot.read.module.info", modInfoPath), exp);
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   846
            }
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   847
        }
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 48071
diff changeset
   848
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
        public ExecutableImage retrieve(ImagePluginStack stack) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
            ExecutableImage image = ImageFileCreator.create(archives, order, stack);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
            if (packagedModulesPath != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
                // copy the packaged modules to the given path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
                Files.createDirectories(packagedModulesPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
                for (Archive a : archives) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
                    Path file = a.getPath();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
                    Path dest = packagedModulesPath.resolve(file.getFileName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
                    Files.copy(file, dest);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
            return image;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   862
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   863
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   864
}