src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Jlink.java
author pchilanomate
Fri, 31 Aug 2018 10:22:04 -0400
changeset 51610 cdef4df6b0e7
parent 47464 36de9c637393
permissions -rw-r--r--
8206424: Use locking for cleaning ProtectionDomainTable Summary: ServiceThread is now in charge of cleaning ProtectionDomainTable entries Reviewed-by: coleenp, iklam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
     2
 * Copyright (c) 2015, 2017, 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
 */
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 39321
diff changeset
    25
package jdk.tools.jlink.internal;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
    27
import java.lang.module.Configuration;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
    28
import java.lang.module.ModuleFinder;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.ByteOrder;
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47216
diff changeset
    30
import java.nio.file.Files;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47216
diff changeset
    32
import java.nio.file.Paths;
42150
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
    33
import java.util.ArrayList;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Set;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
    39
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
    40
import jdk.internal.module.ModulePath;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.tools.jlink.plugin.Plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.tools.jlink.plugin.PluginException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.builder.ImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * API to call jlink.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public final class Jlink {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
     * Create a plugin.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
     * @param name Plugin name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
     * @param configuration Plugin configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
     * @param pluginsLayer Plugins Layer. null means boot layer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
     * @return A new plugin or null if plugin is unknown.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    public static Plugin newPlugin(String name,
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44367
diff changeset
    59
            Map<String, String> configuration, ModuleLayer pluginsLayer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        Objects.requireNonNull(configuration);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44367
diff changeset
    62
        pluginsLayer = pluginsLayer == null ? ModuleLayer.boot() : pluginsLayer;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        return PluginRepository.newPlugin(configuration, name, pluginsLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
     * A complete plugin configuration. Instances of this class are used to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
     * configure jlink.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static final class PluginsConfiguration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        private final List<Plugin> plugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        private final ImageBuilder imageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        private final String lastSorterPluginName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
         * Empty plugins configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        public PluginsConfiguration() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            this(Collections.emptyList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
         * Plugins configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
         * @param plugins List of plugins.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        public PluginsConfiguration(List<Plugin> plugins) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    89
            this(plugins, null, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
         * Plugins configuration with a last sorter and an ImageBuilder. No
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
         * sorting can occur after the last sorter plugin. The ImageBuilder is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
         * in charge to layout the image content on disk.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
         * @param plugins List of transformer plugins.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
         * @param imageBuilder Image builder.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
         * @param lastSorterPluginName Name of last sorter plugin, no sorting
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
         * can occur after it.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        public PluginsConfiguration(List<Plugin> plugins,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                ImageBuilder imageBuilder, String lastSorterPluginName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            this.plugins = plugins == null ? Collections.emptyList()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    : plugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            this.imageBuilder = imageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            this.lastSorterPluginName = lastSorterPluginName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
         * @return the plugins
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        public List<Plugin> getPlugins() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            return plugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
         * @return the imageBuilder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        public ImageBuilder getImageBuilder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            return imageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
         * @return the lastSorterPluginName
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        public String getLastSorterPluginName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            return lastSorterPluginName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            builder.append("imagebuilder=").append(imageBuilder).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            StringBuilder pluginsBuilder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            for (Plugin p : plugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                pluginsBuilder.append(p).append(",");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            builder.append("plugins=").append(pluginsBuilder).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            builder.append("lastsorter=").append(lastSorterPluginName).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            return builder.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     * Jlink configuration. Instances of this class are used to configure jlink.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    public static final class JlinkConfiguration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        private final Path output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        private final Set<String> modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        private final ByteOrder endian;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   154
        private final ModuleFinder finder;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
         * jlink configuration,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
         * @param output Output directory, must not exist.
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44545
diff changeset
   160
         * @param modules The possibly-empty set of root modules to resolve
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
         * @param endian Jimage byte order. Native order by default
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47216
diff changeset
   162
         * @param finder the ModuleFinder for this configuration
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        public JlinkConfiguration(Path output,
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   165
                                  Set<String> modules,
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47216
diff changeset
   166
                                  ByteOrder endian,
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47216
diff changeset
   167
                                  ModuleFinder finder) {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   168
            this.output = output;
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44545
diff changeset
   169
            this.modules = Objects.requireNonNull(modules);
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   170
            this.endian = Objects.requireNonNull(endian);
47464
36de9c637393 8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents: 47216
diff changeset
   171
            this.finder = finder;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
         * @return the byte ordering
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        public ByteOrder getByteOrder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            return endian;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
         * @return the output
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        public Path getOutput() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            return output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
         * @return the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        public Set<String> getModules() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
            return modules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        /**
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   196
         * Returns {@link ModuleFinder} that finds all observable modules
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   197
         * for this jlink configuration.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   198
         */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   199
        public ModuleFinder finder() {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   200
            return finder;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   201
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   202
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   203
        /**
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   204
         * Returns a {@link Configuration} of the given module path,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   205
         * root modules with full service binding.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   206
         */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   207
        public Configuration resolveAndBind()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   208
        {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   209
            return Configuration.empty().resolveAndBind(finder,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   210
                                                        ModuleFinder.of(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   211
                                                        modules);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   212
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   213
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   214
        /**
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   215
         * Returns a {@link Configuration} of the given module path,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   216
         * root modules with no service binding.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   217
         */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   218
        public Configuration resolve()
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   219
        {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   220
            return Configuration.empty().resolve(finder,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   221
                                                 ModuleFinder.of(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   222
                                                 modules);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   223
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42330
diff changeset
   224
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            builder.append("output=").append(output).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            StringBuilder modsBuilder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            for (String p : modules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                modsBuilder.append(p).append(",");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
            builder.append("modules=").append(modsBuilder).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            builder.append("endian=").append(endian).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
            return builder.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
     * Jlink instance constructor, if a security manager is set, the jlink
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
     * permission is checked.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
    public Jlink() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        if (System.getSecurityManager() != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
            System.getSecurityManager().
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
                    checkPermission(new JlinkPermission("jlink"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
     * Build the image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
     * @param config Jlink config, must not be null.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     * @throws PluginException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
    public void build(JlinkConfiguration config) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        build(config, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     * Build the image with a plugin configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     * @param config Jlink config, must not be null.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     * @param pluginsConfig Plugins config, can be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
     * @throws PluginException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
    public void build(JlinkConfiguration config, PluginsConfiguration pluginsConfig) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        Objects.requireNonNull(config);
42150
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   270
        if (pluginsConfig == null) {
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   271
            pluginsConfig = new PluginsConfiguration();
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   272
        }
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   273
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   274
        // add all auto-enabled plugins from boot layer
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   275
        pluginsConfig = addAutoEnabledPlugins(pluginsConfig);
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   276
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            JlinkTask.createImage(config, pluginsConfig);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            throw new PluginException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
42150
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   284
    private PluginsConfiguration addAutoEnabledPlugins(PluginsConfiguration pluginsConfig) {
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   285
        List<Plugin> plugins = new ArrayList<>(pluginsConfig.getPlugins());
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44367
diff changeset
   286
        List<Plugin> bootPlugins = PluginRepository.getPlugins(ModuleLayer.boot());
42150
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   287
        for (Plugin bp : bootPlugins) {
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   288
            if (Utils.isAutoEnabled(bp)) {
42330
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   289
                try {
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   290
                    bp.configure(Collections.emptyMap());
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   291
                } catch (IllegalArgumentException e) {
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   292
                    if (JlinkTask.DEBUG) {
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   293
                        System.err.println("Plugin " + bp.getName() + " threw exception with config: {}");
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   294
                        e.printStackTrace();
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   295
                    }
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   296
                    throw e;
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 42150
diff changeset
   297
                }
42150
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   298
                plugins.add(bp);
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   299
            }
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   300
        }
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   301
        return new PluginsConfiguration(plugins, pluginsConfig.getImageBuilder(),
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   302
            pluginsConfig.getLastSorterPluginName());
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   303
    }
dd0623828f16 8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents: 41559
diff changeset
   304
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
     * Post process the image with a plugin configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
     * @param image Existing image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
     * @param plugins Plugins cannot be null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
    public void postProcess(ExecutableImage image, List<Plugin> plugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        Objects.requireNonNull(image);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        Objects.requireNonNull(plugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            JlinkTask.postProcessImage(image, plugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            throw new PluginException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
}