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.Collections;
  32 import java.util.HashMap;
  33 import java.util.LinkedHashMap;
  34 import java.util.List;
  35 import java.util.Map;
  36 import java.util.Map.Entry;
  37 import java.util.Objects;
  38 import java.util.Properties;
  39 import jdk.tools.jlink.plugin.ExecutableImage;
  40 import jdk.tools.jlink.builder.ImageBuilder;
  41 import jdk.tools.jlink.Jlink;
  42 import jdk.tools.jlink.plugin.Plugin;
  43 import jdk.tools.jlink.plugin.PluginContext;
  44 import jdk.tools.jlink.plugin.PluginException;
  45 import jdk.tools.jlink.plugin.Plugin.CATEGORY;
  46 import jdk.tools.jlink.plugin.Pool;
  47 import jdk.tools.jlink.plugin.PostProcessorPlugin;
  48 import jdk.tools.jlink.plugin.TransformerPlugin;
  49 
  50 /**
  51  * Plugins configuration.
  52  */
  53 public final class ImagePluginConfiguration {
  54 
  55     private static final List<Plugin.CATEGORY> CATEGORIES_ORDER = new ArrayList<>();
  56 
  57     static {
  58         CATEGORIES_ORDER.add(Plugin.CATEGORY.FILTER);
  59         CATEGORIES_ORDER.add(Plugin.CATEGORY.TRANSFORMER);
  60         CATEGORIES_ORDER.add(Plugin.CATEGORY.MODULEINFO_TRANSFORMER);
  61         CATEGORIES_ORDER.add(Plugin.CATEGORY.SORTER);
  62         CATEGORIES_ORDER.add(Plugin.CATEGORY.COMPRESSOR);
  63         CATEGORIES_ORDER.add(Plugin.CATEGORY.VERIFIER);
  64         CATEGORIES_ORDER.add(Plugin.CATEGORY.PROCESSOR);
  65         CATEGORIES_ORDER.add(Plugin.CATEGORY.PACKAGER);
  66     }
  67 
  68     private ImagePluginConfiguration() {
  69     }
  70 
  71     /*
  72      * Create a stack of plugins from a a configuration.
  73      */
  74     public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration pluginsConfiguration)
  75             throws Exception {
  76         if (pluginsConfiguration == null) {
  77             return new ImagePluginStack();
  78         }
  79         Map<Plugin.CATEGORY, List<Plugin>> plugins = new LinkedHashMap<>();
  80         for (Plugin.CATEGORY cat : CATEGORIES_ORDER) {
  81             plugins.put(cat, new ArrayList<>());
  82         }
  83 
  84         List<String> seen = new ArrayList<>();
  85         // split into categories and check for plugin with same name.
  86         for (Plugin plug : pluginsConfiguration.getPlugins()) {
  87             if (seen.contains(plug.getName())) {
  88                 throw new Exception("Plugin " + plug.getName()
  89                         + " added more than once to stack ");
  90             }
  91             seen.add(plug.getName());
  92             CATEGORY category = Utils.getCategory(plug);
  93             if (category == null) {
  94                 throw new PluginException("Invalid category for "
  95                         + plug.getName());
  96             }
  97             List<Plugin> lst = plugins.get(category);
  98             lst.add(plug);
  99         }
 100 
 101         List<TransformerPlugin> transformerPlugins = new ArrayList<>();
 102         List<PostProcessorPlugin> postProcessingPlugins = new ArrayList<>();
 103         for (Entry<Plugin.CATEGORY, List<Plugin>> entry : plugins.entrySet()) {
 104             // Sort according to plugin constraints
 105             List<Plugin> orderedPlugins = PluginOrderingGraph.sort(entry.getValue());
 106             CATEGORY category = entry.getKey();
 107             for (Plugin p : orderedPlugins) {
 108                 if (Utils.isPostProcessor(category)) {
 109                     @SuppressWarnings("unchecked")
 110                     PostProcessorPlugin pp = (PostProcessorPlugin) p;
 111                     postProcessingPlugins.add(pp);
 112                 } else {
 113                     @SuppressWarnings("unchecked")
 114                     TransformerPlugin trans = (TransformerPlugin) p;
 115                     transformerPlugins.add(trans);
 116                 }
 117             }
 118         }
 119         Plugin lastSorter = null;
 120         for (Plugin plugin : transformerPlugins) {
 121             if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) {
 122                 lastSorter = plugin;
 123                 break;
 124             }
 125         }
 126         if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) {
 127             throw new IOException("Unknown last plugin "
 128                     + pluginsConfiguration.getLastSorterPluginName());
 129         }
 130         ImageBuilder builder = pluginsConfiguration.getImageBuilder();
 131         if (builder == null) {
 132             // This should be the case for jimage only creation or post-install.
 133             builder = new ImageBuilder() {
 134 
 135                 @Override
 136                 public DataOutputStream getJImageOutputStream() {
 137                     throw new PluginException("No directory setup to store files");
 138                 }
 139 
 140                 @Override
 141                 public ExecutableImage getExecutableImage() {
 142                     throw new PluginException("No directory setup to store files");
 143                 }
 144 
 145                 @Override
 146                 public void storeFiles(Pool files) {
 147                     throw new PluginException("No directory setup to store files");
 148                 }
 149             };
 150         }
 151 
 152         PluginContext ctxt = pluginsConfiguration.getPluginContext();
 153         return new ImagePluginStack(builder, transformerPlugins,
 154                 lastSorter, postProcessingPlugins, ctxt);
 155     }
 156 }