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     private static final List<Category> CATEGORIES_ORDER = new ArrayList<>();
  47 
  48     static {
  49         CATEGORIES_ORDER.add(Category.FILTER);
  50         CATEGORIES_ORDER.add(Category.TRANSFORMER);
  51         CATEGORIES_ORDER.add(Category.MODULEINFO_TRANSFORMER);
  52         CATEGORIES_ORDER.add(Category.SORTER);
  53         CATEGORIES_ORDER.add(Category.COMPRESSOR);
  54         CATEGORIES_ORDER.add(Category.METAINFO_ADDER);
  55         CATEGORIES_ORDER.add(Category.VERIFIER);
  56         CATEGORIES_ORDER.add(Category.PROCESSOR);
  57         CATEGORIES_ORDER.add(Category.PACKAGER);
  58     }
  59 
  60     private ImagePluginConfiguration() {
  61     }
  62 
  63     /*
  64      * Create a stack of plugins from a a configuration.
  65      */
  66     public static ImagePluginStack parseConfiguration(Jlink.PluginsConfiguration pluginsConfiguration)
  67             throws Exception {
  68         if (pluginsConfiguration == null) {
  69             return new ImagePluginStack();
  70         }
  71         Map<Category, List<Plugin>> plugins = new LinkedHashMap<>();
  72         for (Category cat : CATEGORIES_ORDER) {
  73             plugins.put(cat, new ArrayList<>());
  74         }
  75 
  76         List<String> seen = new ArrayList<>();
  77         // split into categories and check for plugin with same name.
  78         for (Plugin plug : pluginsConfiguration.getPlugins()) {
  79             if (seen.contains(plug.getName())) {
  80                 throw new Exception("Plugin " + plug.getName()
  81                         + " added more than once to stack ");
  82             }
  83             seen.add(plug.getName());
  84             Category category = plug.getType();
  85             if (category == null) {
  86                 throw new PluginException("Invalid category for "
  87                         + plug.getName());
  88             }
  89             List<Plugin> lst = plugins.get(category);
  90             lst.add(plug);
  91         }
  92 
  93         List<Plugin> orderedPlugins = new ArrayList<>();
  94         plugins.entrySet().stream().forEach((entry) -> {
  95             orderedPlugins.addAll(entry.getValue());
  96         });
  97         Plugin lastSorter = null;
  98         for (Plugin plugin : orderedPlugins) {
  99             if (plugin.getName().equals(pluginsConfiguration.getLastSorterPluginName())) {
 100                 lastSorter = plugin;
 101                 break;
 102             }
 103         }
 104         if (pluginsConfiguration.getLastSorterPluginName() != null && lastSorter == null) {
 105             throw new IOException("Unknown last plugin "
 106                     + pluginsConfiguration.getLastSorterPluginName());
 107         }
 108         ImageBuilder builder = pluginsConfiguration.getImageBuilder();
 109         if (builder == null) {
 110             // This should be the case for jimage only creation or post-install.
 111             builder = new ImageBuilder() {
 112 
 113                 @Override
 114                 public DataOutputStream getJImageOutputStream() {
 115                     throw new PluginException("No directory setup to store files");
 116                 }
 117 
 118                 @Override
 119                 public ExecutableImage getExecutableImage() {
 120                     throw new PluginException("No directory setup to store files");
 121                 }
 122 
 123                 @Override
 124                 public void storeFiles(ResourcePool files) {
 125                     throw new PluginException("No directory setup to store files");
 126                 }
 127             };
 128         }
 129 
 130         return new ImagePluginStack(builder, orderedPlugins, lastSorter);
 131     }
 132 }
--- EOF ---