1 /*
   2  * Copyright (c) 2020, 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.incubator.jpackage.internal;
  26 
  27 import java.io.IOException;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 import java.util.Map;
  33 import java.util.stream.Stream;
  34 import static jdk.incubator.jpackage.internal.StandardBundlerParam.LAUNCHER_DATA;
  35 import static jdk.incubator.jpackage.internal.StandardBundlerParam.APP_NAME;
  36 import static jdk.incubator.jpackage.internal.StandardBundlerParam.JAVA_OPTIONS;
  37 import static jdk.incubator.jpackage.internal.StandardBundlerParam.ARGUMENTS;
  38 
  39 /**
  40  * App launcher's config file.
  41  */
  42 final class CfgFile {
  43     CfgFile() {
  44         appLayout = ApplicationLayout.platformAppImage();
  45     }
  46 
  47     CfgFile initFromParams(Map<String, ? super Object> params) {
  48         launcherData = LAUNCHER_DATA.fetchFrom(params);
  49         launcherName = APP_NAME.fetchFrom(params);
  50         javaOptions = JAVA_OPTIONS.fetchFrom(params);
  51         arguments = ARGUMENTS.fetchFrom(params);
  52         return this;
  53     }
  54 
  55     void create(Path appImage) throws IOException {
  56         List<Map.Entry<String, Object>> content = new ArrayList<>();
  57 
  58         ApplicationLayout appCfgLayout = createAppCfgLayout();
  59 
  60         content.add(Map.entry("[Application]", SECTION_TAG));
  61 
  62         if (launcherData.isModular()) {
  63             content.add(Map.entry("app.mainmodule", launcherData.moduleName()
  64                     + "/" + launcherData.qualifiedClassName()));
  65         } else {
  66             // If the app is contained in an unnamed jar then launch it the
  67             // legacy way and the main class string must be
  68             // of the format com/foo/Main
  69             if (launcherData.mainJarName() != null) {
  70                 content.add(Map.entry("app.classpath",
  71                         appCfgLayout.appDirectory().resolve(
  72                                 launcherData.mainJarName())));
  73             }
  74             content.add(Map.entry("app.mainclass",
  75                     launcherData.qualifiedClassName()));
  76         }
  77 
  78         for (var value : launcherData.classPath()) {
  79             content.add(Map.entry("app.classpath",
  80                     appCfgLayout.appDirectory().resolve(value).toString()));
  81         }
  82 
  83         ApplicationLayout appImagelayout = appLayout.resolveAt(appImage);
  84         Path modsDir = appImagelayout.appModsDirectory();
  85         if (!javaOptions.isEmpty() || Files.isDirectory(modsDir)) {
  86             content.add(Map.entry("[JavaOptions]", SECTION_TAG));
  87             for (var value : javaOptions) {
  88                 content.add(Map.entry("java-options", value));
  89             }
  90             content.add(Map.entry("java-options", "--module-path"));
  91             content.add(Map.entry("java-options",
  92                     appCfgLayout.appModsDirectory()));
  93         }
  94 
  95         if (!arguments.isEmpty()) {
  96             content.add(Map.entry("[ArgOptions]", SECTION_TAG));
  97             for (var value : arguments) {
  98                 content.add(Map.entry("arguments", value));
  99             }
 100         }
 101 
 102         Path cfgFile = appImagelayout.appDirectory().resolve(launcherName + ".cfg");
 103         Files.createDirectories(cfgFile.getParent());
 104 
 105         boolean[] addLineBreakAtSection = new boolean[1];
 106         Stream<String> lines = content.stream().map(entry -> {
 107             if (entry.getValue() == SECTION_TAG) {
 108                 if (!addLineBreakAtSection[0]) {
 109                     addLineBreakAtSection[0] = true;
 110                     return entry.getKey();
 111                 }
 112                 return "\n" + entry.getKey();
 113             }
 114             return entry.getKey() + "=" + entry.getValue();
 115         });
 116         Files.write(cfgFile, (Iterable<String>) lines::iterator);
 117     }
 118 
 119     private ApplicationLayout createAppCfgLayout() {
 120         ApplicationLayout appCfgLayout = appLayout.resolveAt(Path.of("$ROOTDIR"));
 121         appCfgLayout.pathGroup().setPath(ApplicationLayout.PathRole.APP,
 122                 Path.of("$APPDIR"));
 123         appCfgLayout.pathGroup().setPath(ApplicationLayout.PathRole.MODULES,
 124                 appCfgLayout.appDirectory().resolve(appCfgLayout.appModsDirectory().getFileName()));
 125         return appCfgLayout;
 126     }
 127 
 128     private String launcherName;
 129     private LauncherData launcherData;
 130     List<String> arguments;
 131     List<String> javaOptions;
 132     private final ApplicationLayout appLayout;
 133 
 134     private final static Object SECTION_TAG = new Object();
 135 }