jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImagePluginConfiguration.java
author jlaskey
Tue, 28 Jun 2016 16:07:23 -0300
changeset 39308 f6517755057f
parent 39129 4b2086305b68
child 39321 c60f34e8c057
permissions -rw-r--r--
8160459: jlink minor code clean up Reviewed-by: 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.plugin.ExecutableImage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.tools.jlink.builder.ImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.tools.jlink.Jlink;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import jdk.tools.jlink.plugin.Plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import jdk.tools.jlink.plugin.PluginException;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    40
import jdk.tools.jlink.plugin.Plugin.Category;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    41
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.tools.jlink.plugin.PostProcessorPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.plugin.TransformerPlugin;
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
 * Plugins configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public final class ImagePluginConfiguration {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    50
    private static final List<Category> CATEGORIES_ORDER = new ArrayList<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    static {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    53
        CATEGORIES_ORDER.add(Category.FILTER);
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);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    57
        CATEGORIES_ORDER.add(Category.COMPRESSOR);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    58
        CATEGORIES_ORDER.add(Category.METAINFO_ADDER);
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());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    88
            Category category = Utils.getCategory(plug);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        List<TransformerPlugin> transformerPlugins = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        List<PostProcessorPlugin> postProcessingPlugins = new ArrayList<>();
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
    99
        plugins.entrySet().stream().forEach((entry) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            // Sort according to plugin constraints
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            List<Plugin> orderedPlugins = PluginOrderingGraph.sort(entry.getValue());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   102
            Category category = entry.getKey();
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
   103
            orderedPlugins.stream().forEach((p) -> {
39129
4b2086305b68 8159593: Plugin Set<Category> getType() should return a Category
sundar
parents: 38320
diff changeset
   104
                if (category.isPostProcessor()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    @SuppressWarnings("unchecked")
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
   106
                            PostProcessorPlugin pp = (PostProcessorPlugin) p;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                    postProcessingPlugins.add(pp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                    @SuppressWarnings("unchecked")
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
   110
                            TransformerPlugin trans = (TransformerPlugin) p;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                    transformerPlugins.add(trans);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                }
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
   113
            });
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39129
diff changeset
   114
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        Plugin lastSorter = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        for (Plugin plugin : transformerPlugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                lastSorter = plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                break;
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
        if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            throw new IOException("Unknown last plugin "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                    + pluginsConfiguration.getLastSorterPluginName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        ImageBuilder builder = pluginsConfiguration.getImageBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        if (builder == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            // This should be the case for jimage only creation or post-install.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            builder = new ImageBuilder() {
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 DataOutputStream getJImageOutputStream() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                public ExecutableImage getExecutableImage() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    throw new PluginException("No directory setup to store files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                @Override
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   142
                public void storeFiles(ModulePool files) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    throw new PluginException("No directory setup to store files");
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        return new ImagePluginStack(builder, transformerPlugins,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   149
                lastSorter, postProcessingPlugins);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
}