jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImagePluginConfiguration.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
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.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.LinkedHashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Map.Entry;
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.Properties;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import jdk.tools.jlink.plugin.ExecutableImage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import jdk.tools.jlink.builder.ImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.tools.jlink.Jlink;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.tools.jlink.plugin.Plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.plugin.PluginContext;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.PluginException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.tools.jlink.plugin.Plugin.CATEGORY;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.tools.jlink.plugin.Pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.tools.jlink.plugin.PostProcessorPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.tools.jlink.plugin.TransformerPlugin;
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
 * Plugins configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
public final class ImagePluginConfiguration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private static final List<Plugin.CATEGORY> CATEGORIES_ORDER = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        CATEGORIES_ORDER.add(Plugin.CATEGORY.FILTER);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        CATEGORIES_ORDER.add(Plugin.CATEGORY.TRANSFORMER);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        CATEGORIES_ORDER.add(Plugin.CATEGORY.MODULEINFO_TRANSFORMER);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        CATEGORIES_ORDER.add(Plugin.CATEGORY.SORTER);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        CATEGORIES_ORDER.add(Plugin.CATEGORY.COMPRESSOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        CATEGORIES_ORDER.add(Plugin.CATEGORY.VERIFIER);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        CATEGORIES_ORDER.add(Plugin.CATEGORY.PROCESSOR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        CATEGORIES_ORDER.add(Plugin.CATEGORY.PACKAGER);
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
    private ImagePluginConfiguration() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration plugins)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        return parseConfiguration(plugins, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    }
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
     * Create a stack of plugins from a a configuration.
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
    public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration pluginsConfiguration,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            String bom)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        if (pluginsConfiguration == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            return new ImagePluginStack(bom);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        Map<Plugin.CATEGORY, List<Plugin>> plugins = new LinkedHashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        for (Plugin.CATEGORY cat : CATEGORIES_ORDER) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            plugins.put(cat, new ArrayList<>());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        List<String> seen = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        // split into categories and check for plugin with same name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        for (Plugin plug : pluginsConfiguration.getPlugins()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            if (seen.contains(plug.getName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                throw new Exception("Plugin " + plug.getName()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                        + " added more than once to stack ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            seen.add(plug.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            CATEGORY category = Utils.getCategory(plug);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            if (category == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                throw new PluginException("Invalid category for "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                        + plug.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            List<Plugin> lst = plugins.get(category);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            lst.add(plug);
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
        List<TransformerPlugin> transformerPlugins = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        List<PostProcessorPlugin> postProcessingPlugins = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        for (Entry<Plugin.CATEGORY, List<Plugin>> entry : plugins.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            // Sort according to plugin constraints
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            List<Plugin> orderedPlugins = PluginOrderingGraph.sort(entry.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            CATEGORY category = entry.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            for (Plugin p : orderedPlugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                if (Utils.isPostProcessor(category)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                    @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                    PostProcessorPlugin pp = (PostProcessorPlugin) p;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                    postProcessingPlugins.add(pp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                    @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                    TransformerPlugin trans = (TransformerPlugin) p;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    transformerPlugins.add(trans);
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
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        Plugin lastSorter = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        for (Plugin plugin : transformerPlugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                lastSorter = plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                break;
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
        if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            throw new IOException("Unknown last plugin "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                    + pluginsConfiguration.getLastSorterPluginName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        ImageBuilder builder = pluginsConfiguration.getImageBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        if (builder == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            // This should be the case for jimage only creation or post-install.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            builder = new ImageBuilder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                public DataOutputStream getJImageOutputStream() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                    throw new PluginException("No directory setup to store files");
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
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                public ExecutableImage getExecutableImage() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                public void storeFiles(Pool files, String bom) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        PluginContext ctxt = pluginsConfiguration.getPluginContext();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        return new ImagePluginStack(builder, transformerPlugins,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                lastSorter, postProcessingPlugins, ctxt, bom);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
}