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