< prev index next >

src/jdk.incubator.jpackage/share/classes/jdk/incubator/jpackage/internal/CfgFile.java

Print this page




  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 }


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