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     public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration plugins)
  72             throws Exception {
  73         return parseConfiguration(plugins, null);
  74     }
  75 
  76     /*
  77      * Create a stack of plugins from a a configuration.
  78      *
  79      */
  80     public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration pluginsConfiguration,
  81             String bom)
  82             throws Exception {
  83         if (pluginsConfiguration == null) {
  84             return new ImagePluginStack(bom);
  85         }
  86         Map<Plugin.CATEGORY, List<Plugin>> plugins = new LinkedHashMap<>();
  87         for (Plugin.CATEGORY cat : CATEGORIES_ORDER) {
  88             plugins.put(cat, new ArrayList<>());
  89         }
  90 
  91         List<String> seen = new ArrayList<>();
  92         // split into categories and check for plugin with same name.
  93         for (Plugin plug : pluginsConfiguration.getPlugins()) {
  94             if (seen.contains(plug.getName())) {
  95                 throw new Exception("Plugin " + plug.getName()
  96                         + " added more than once to stack ");
  97             }
  98             seen.add(plug.getName());
  99             CATEGORY category = Utils.getCategory(plug);
 100             if (category == null) {
 101                 throw new PluginException("Invalid category for "
 102                         + plug.getName());
 103             }
 104             List<Plugin> lst = plugins.get(category);
 105             lst.add(plug);
 106         }
 107 
 108         List<TransformerPlugin> transformerPlugins = new ArrayList<>();
 109         List<PostProcessorPlugin> postProcessingPlugins = new ArrayList<>();
 110         for (Entry<Plugin.CATEGORY, List<Plugin>> entry : plugins.entrySet()) {
 111             // Sort according to plugin constraints
 112             List<Plugin> orderedPlugins = PluginOrderingGraph.sort(entry.getValue());
 113             CATEGORY category = entry.getKey();
 114             for (Plugin p : orderedPlugins) {
 115                 if (Utils.isPostProcessor(category)) {
 116                     @SuppressWarnings("unchecked")
 117                     PostProcessorPlugin pp = (PostProcessorPlugin) p;
 118                     postProcessingPlugins.add(pp);
 119                 } else {
 120                     @SuppressWarnings("unchecked")
 121                     TransformerPlugin trans = (TransformerPlugin) p;
 122                     transformerPlugins.add(trans);
 123                 }
 124             }
 125         }
 126         Plugin lastSorter = null;
 127         for (Plugin plugin : transformerPlugins) {
 128             if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) {
 129                 lastSorter = plugin;
 130                 break;
 131             }
 132         }
 133         if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) {
 134             throw new IOException("Unknown last plugin "
 135                     + pluginsConfiguration.getLastSorterPluginName());
 136         }
 137         ImageBuilder builder = pluginsConfiguration.getImageBuilder();
 138         if (builder == null) {
 139             // This should be the case for jimage only creation or post-install.
 140             builder = new ImageBuilder() {
 141 
 142                 @Override
 143                 public DataOutputStream getJImageOutputStream() {
 144                     throw new PluginException("No directory setup to store files");
 145                 }
 146 
 147                 @Override
 148                 public ExecutableImage getExecutableImage() {
 149                     throw new PluginException("No directory setup to store files");
 150                 }
 151 
 152                 @Override
 153                 public void storeFiles(Pool files, String bom) {
 154                     throw new PluginException("No directory setup to store files");
 155                 }
 156             };
 157         }
 158 
 159         PluginContext ctxt = pluginsConfiguration.getPluginContext();
 160         return new ImagePluginStack(builder, transformerPlugins,
 161                 lastSorter, postProcessingPlugins, ctxt, bom);
 162     }
 163 }