src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImagePluginConfiguration.java
author mr
Tue, 29 Oct 2019 08:26:55 -0700
changeset 58842 6c255334120d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232080: jlink plugins for vendor information and run-time options Reviewed-by: ihse, alanb, kvn, bobv, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.DataOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
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.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.LinkedHashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Map.Entry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import jdk.tools.jlink.builder.ImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.tools.jlink.plugin.Plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.tools.jlink.plugin.PluginException;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    38
import jdk.tools.jlink.plugin.Plugin.Category;
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: 39505
diff changeset
    39
import jdk.tools.jlink.plugin.ResourcePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * Plugins configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
public final class ImagePluginConfiguration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents: 41559
diff changeset
    46
    // Order in which plugins are applied. Note that COMPRESSOR type plugins should come
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents: 41559
diff changeset
    47
    // after any plugin that reads .class resources and operate on binary data.
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents: 41559
diff changeset
    48
    // Plugin.Category enum element order matches this order for ease of read.
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    49
    private static final List<Category> CATEGORIES_ORDER = new ArrayList<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    static {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    52
        CATEGORIES_ORDER.add(Category.FILTER);
58842
6c255334120d 8232080: jlink plugins for vendor information and run-time options
mr
parents: 47216
diff changeset
    53
        CATEGORIES_ORDER.add(Category.ADDER);
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    54
        CATEGORIES_ORDER.add(Category.TRANSFORMER);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    55
        CATEGORIES_ORDER.add(Category.MODULEINFO_TRANSFORMER);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    56
        CATEGORIES_ORDER.add(Category.SORTER);
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents: 41559
diff changeset
    57
        CATEGORIES_ORDER.add(Category.METAINFO_ADDER);
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    58
        CATEGORIES_ORDER.add(Category.COMPRESSOR);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    59
        CATEGORIES_ORDER.add(Category.VERIFIER);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    60
        CATEGORIES_ORDER.add(Category.PROCESSOR);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    61
        CATEGORIES_ORDER.add(Category.PACKAGER);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    private ImagePluginConfiguration() {
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
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
     * Create a stack of plugins from a a configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    70
    public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration pluginsConfiguration)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        if (pluginsConfiguration == null) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    73
            return new ImagePluginStack();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        }
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    75
        Map<Category, List<Plugin>> plugins = new LinkedHashMap<>();
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    76
        for (Category cat : CATEGORIES_ORDER) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            plugins.put(cat, new ArrayList<>());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        List<String> seen = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        // split into categories and check for plugin with same name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        for (Plugin plug : pluginsConfiguration.getPlugins()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            if (seen.contains(plug.getName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                throw new Exception("Plugin " + plug.getName()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                        + " added more than once to stack ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            seen.add(plug.getName());
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
    88
            Category category = plug.getType();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            if (category == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                throw new PluginException("Invalid category for "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                        + plug.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            List<Plugin> lst = plugins.get(category);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            lst.add(plug);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
    97
        List<Plugin> orderedPlugins = new ArrayList<>();
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    98
        plugins.entrySet().stream().forEach((entry) -> {
39505
be0bbd76ae51 8161055: Remove plugin ordering by isAfter, isBefore.
sundar
parents: 39321
diff changeset
    99
            orderedPlugins.addAll(entry.getValue());
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
   100
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        Plugin lastSorter = null;
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
   102
        for (Plugin plugin : orderedPlugins) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                lastSorter = plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            throw new IOException("Unknown last plugin "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                    + pluginsConfiguration.getLastSorterPluginName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        ImageBuilder builder = pluginsConfiguration.getImageBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        if (builder == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            // This should be the case for jimage only creation or post-install.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            builder = new ImageBuilder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                public DataOutputStream getJImageOutputStream() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                public ExecutableImage getExecutableImage() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                @Override
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: 39505
diff changeset
   128
                public void storeFiles(ResourcePool files) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39308
diff changeset
   134
        return new ImagePluginStack(builder, orderedPlugins, lastSorter);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
}