1 /*
   2  * Copyright (c) 2016, 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 
  26 package jdk.tools.jlink.internal.packager;
  27 
  28 
  29 import jdk.tools.jlink.builder.DefaultImageBuilder;
  30 import jdk.tools.jlink.internal.Jlink;
  31 import jdk.tools.jlink.internal.JlinkTask;
  32 import jdk.tools.jlink.plugin.Plugin;
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.lang.module.ModuleFinder;
  37 import java.nio.file.Path;
  38 import java.util.ArrayList;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.util.Map;
  42 import java.util.Set;
  43 
  44 /**
  45  * AppRuntimeImageBuilder is a private API used only by the Java Packager to generate
  46  * a Java runtime image using jlink. AppRuntimeImageBuilder encapsulates the
  47  * arguments that jlink requires to generate this image. To create the image call the
  48  * build() method.
  49  */
  50 public final class AppRuntimeImageBuilder {
  51     private Path outputDir = null;
  52     private List<Path> modulePath = null;
  53     private Set<String> addModules = null;
  54     private Set<String> limitModules = null;
  55     private String excludeFileList = null;
  56     private Map<String, String> userArguments = null;
  57     private Boolean stripNativeCommands = null;
  58 
  59     public AppRuntimeImageBuilder() {}
  60 
  61     public void setOutputDir(Path value) {
  62         outputDir = value;
  63     }
  64 
  65     public void setModulePath(List<Path> value) {
  66         modulePath = value;
  67     }
  68 
  69     public void setAddModules(Set<String> value) {
  70         addModules = value;
  71     }
  72 
  73     public void setLimitModules(Set<String> value) {
  74         limitModules = value;
  75     }
  76 
  77     public void setExcludeFileList(String value) {
  78         excludeFileList = value;
  79     }
  80 
  81     public void setStripNativeCommands(boolean value) {
  82         stripNativeCommands = value;
  83     }
  84 
  85     public void setUserArguments(Map<String, String> value) {
  86         userArguments = value;
  87     }
  88 
  89     public void build() throws IOException {
  90         // jlink main arguments
  91         Jlink.JlinkConfiguration jlinkConfig = new Jlink.JlinkConfiguration(
  92             new File("").toPath(), // Unused
  93             modulePath, addModules, limitModules);
  94 
  95         // plugin configuration
  96         List<Plugin> plugins = new ArrayList<Plugin>();
  97 
  98         if (stripNativeCommands) {
  99             plugins.add(Jlink.newPlugin(
 100                         "strip-native-commands",
 101                         Collections.singletonMap("strip-native-commands", "on"),
 102                         null));
 103         }
 104 
 105         if (excludeFileList != null && !excludeFileList.isEmpty()) {
 106             plugins.add(Jlink.newPlugin(
 107                         "exclude-files",
 108                         Collections.singletonMap("exclude-files", excludeFileList),
 109                         null));
 110         }
 111 
 112         // add user supplied jlink arguments
 113         for (Map.Entry<String, String> entry : userArguments.entrySet()) {
 114             String key = entry.getKey();
 115             String value = entry.getValue();
 116             plugins.add(Jlink.newPlugin(key,
 117                                         Collections.singletonMap(key, value),
 118                                         null));
 119         }
 120 
 121         // build the image
 122         Jlink.PluginsConfiguration pluginConfig = new Jlink.PluginsConfiguration(
 123             plugins, new DefaultImageBuilder(outputDir), null);
 124         Jlink jlink = new Jlink();
 125         jlink.build(jlinkConfig, pluginConfig);
 126     }
 127 
 128     /*
 129      * Returns a ModuleFinder that limits observability to the given root
 130      * modules, their transitive dependences, plus a set of other modules.
 131      */
 132     public static ModuleFinder moduleFinder(List<Path> modulepaths,
 133                                             Set<String> roots,
 134                                             Set<String> otherModules) {
 135         return JlinkTask.newModuleFinder(modulepaths, roots, otherModules);
 136     }
 137 }