ImagePluginConfiguration.java revision 16245:c510a5c610e1
1271186Sbr/* 2271186Sbr * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3271186Sbr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4271186Sbr * 5271186Sbr * This code is free software; you can redistribute it and/or modify it 6271186Sbr * under the terms of the GNU General Public License version 2 only, as 7271186Sbr * published by the Free Software Foundation. Oracle designates this 8271186Sbr * particular file as subject to the "Classpath" exception as provided 9271186Sbr * by Oracle in the LICENSE file that accompanied this code. 10271186Sbr * 11271186Sbr * This code is distributed in the hope that it will be useful, but WITHOUT 12271186Sbr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13271186Sbr * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14271186Sbr * version 2 for more details (a copy is included in the LICENSE file that 15271186Sbr * accompanied this code). 16271186Sbr * 17271186Sbr * You should have received a copy of the GNU General Public License version 18271186Sbr * 2 along with this work; if not, write to the Free Software Foundation, 19271186Sbr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20271186Sbr * 21271186Sbr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22271186Sbr * or visit www.oracle.com if you need additional information or have any 23271186Sbr * questions. 24271186Sbr */ 25271186Sbrpackage jdk.tools.jlink.internal; 26271186Sbr 27271186Sbrimport java.io.DataOutputStream; 28271186Sbr 29271186Sbrimport java.io.IOException; 30271186Sbrimport java.util.ArrayList; 31271186Sbrimport java.util.LinkedHashMap; 32271186Sbrimport java.util.List; 33271200Sbrimport java.util.Map; 34271200Sbrimport java.util.Map.Entry; 35271200Sbrimport jdk.tools.jlink.builder.ImageBuilder; 36271200Sbrimport jdk.tools.jlink.plugin.Plugin; 37271200Sbrimport jdk.tools.jlink.plugin.PluginException; 38271200Sbrimport jdk.tools.jlink.plugin.Plugin.Category; 39import jdk.tools.jlink.plugin.ResourcePool; 40 41/** 42 * Plugins configuration. 43 */ 44public 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.TRANSFORMER); 54 CATEGORIES_ORDER.add(Category.MODULEINFO_TRANSFORMER); 55 CATEGORIES_ORDER.add(Category.SORTER); 56 CATEGORIES_ORDER.add(Category.METAINFO_ADDER); 57 CATEGORIES_ORDER.add(Category.COMPRESSOR); 58 CATEGORIES_ORDER.add(Category.VERIFIER); 59 CATEGORIES_ORDER.add(Category.PROCESSOR); 60 CATEGORIES_ORDER.add(Category.PACKAGER); 61 } 62 63 private ImagePluginConfiguration() { 64 } 65 66 /* 67 * Create a stack of plugins from a a configuration. 68 */ 69 public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration pluginsConfiguration) 70 throws Exception { 71 if (pluginsConfiguration == null) { 72 return new ImagePluginStack(); 73 } 74 Map<Category, List<Plugin>> plugins = new LinkedHashMap<>(); 75 for (Category cat : CATEGORIES_ORDER) { 76 plugins.put(cat, new ArrayList<>()); 77 } 78 79 List<String> seen = new ArrayList<>(); 80 // split into categories and check for plugin with same name. 81 for (Plugin plug : pluginsConfiguration.getPlugins()) { 82 if (seen.contains(plug.getName())) { 83 throw new Exception("Plugin " + plug.getName() 84 + " added more than once to stack "); 85 } 86 seen.add(plug.getName()); 87 Category category = plug.getType(); 88 if (category == null) { 89 throw new PluginException("Invalid category for " 90 + plug.getName()); 91 } 92 List<Plugin> lst = plugins.get(category); 93 lst.add(plug); 94 } 95 96 List<Plugin> orderedPlugins = new ArrayList<>(); 97 plugins.entrySet().stream().forEach((entry) -> { 98 orderedPlugins.addAll(entry.getValue()); 99 }); 100 Plugin lastSorter = null; 101 for (Plugin plugin : orderedPlugins) { 102 if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) { 103 lastSorter = plugin; 104 break; 105 } 106 } 107 if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) { 108 throw new IOException("Unknown last plugin " 109 + pluginsConfiguration.getLastSorterPluginName()); 110 } 111 ImageBuilder builder = pluginsConfiguration.getImageBuilder(); 112 if (builder == null) { 113 // This should be the case for jimage only creation or post-install. 114 builder = new ImageBuilder() { 115 116 @Override 117 public DataOutputStream getJImageOutputStream() { 118 throw new PluginException("No directory setup to store files"); 119 } 120 121 @Override 122 public ExecutableImage getExecutableImage() { 123 throw new PluginException("No directory setup to store files"); 124 } 125 126 @Override 127 public void storeFiles(ResourcePool files) { 128 throw new PluginException("No directory setup to store files"); 129 } 130 }; 131 } 132 133 return new ImagePluginStack(builder, orderedPlugins, lastSorter); 134 } 135} 136