src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WinExeBundler.java
author herrick
Wed, 16 Oct 2019 09:57:23 -0400
branchJDK-8200758-branch
changeset 58647 2c43b89b1679
parent 58114 42df7de58e39
child 58696 61c44899b4eb
permissions -rw-r--r--
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes Submitted-by: asemenyuk Reviewed-by: aherrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     1
/*
57106
ea870b9ce89a 8216492: Update copyright of all new jpackage fils to 2019
kcr
parents: 57103
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     4
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    10
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    15
 * accompanied this code).
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    16
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    20
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    23
 * questions.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    24
 */
57039
98d3963b0b7b 8214051: rename jpackager tool to jpackage
herrick
parents: 57038
diff changeset
    25
package jdk.jpackage.internal;
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    26
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    27
import java.io.*;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    28
import java.nio.file.Files;
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    29
import java.nio.file.Path;
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    30
import java.nio.file.Paths;
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    31
import java.text.MessageFormat;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    32
import java.util.*;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    33
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    34
public class WinExeBundler extends AbstractBundler {
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    35
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    36
    static {
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    37
        System.loadLibrary("jpackage");
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    38
    }
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    39
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    40
    private static final ResourceBundle I18N = ResourceBundle.getBundle(
57059
9bb2a4dc3af7 8214143: Reduce Resource files
herrick
parents: 57039
diff changeset
    41
            "jdk.jpackage.internal.resources.WinResources");
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    42
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    43
    public static final BundlerParamInfo<WinAppBundler> APP_BUNDLER
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    44
            = new WindowsBundlerParam<>(
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    45
                    "win.app.bundler",
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    46
                    WinAppBundler.class,
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    47
                    params -> new WinAppBundler(),
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    48
                    null);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    49
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    50
    public static final BundlerParamInfo<File> EXE_IMAGE_DIR
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    51
            = new WindowsBundlerParam<>(
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    52
                    "win.exe.imageDir",
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    53
                    File.class,
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    54
                    params -> {
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    55
                        File imagesRoot = IMAGES_ROOT.fetchFrom(params);
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    56
                        if (!imagesRoot.exists()) {
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    57
                            imagesRoot.mkdirs();
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    58
                        }
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    59
                        return new File(imagesRoot, "win-exe.image");
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    60
                    },
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    61
                    (s, p) -> null);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    62
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    63
    private final static String EXE_WRAPPER_NAME = "msiwrapper.exe";
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    64
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    65
    @Override
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    66
    public String getName() {
57059
9bb2a4dc3af7 8214143: Reduce Resource files
herrick
parents: 57039
diff changeset
    67
        return getString("exe.bundler.name");
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    68
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    69
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    70
    @Override
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    71
    public String getID() {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    72
        return "exe";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    73
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    74
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    75
    @Override
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    76
    public String getBundleType() {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    77
        return "INSTALLER";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    78
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    79
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    80
    @Override
57396
3944e4c2f779 8223586: remove jpackage dead code and other cleanup
herrick
parents: 57391
diff changeset
    81
    public File execute(Map<String, ? super Object> params,
57140
3dcb33ce7ced 8217751: jpackage messages and failures
herrick
parents: 57106
diff changeset
    82
            File outputParentDir) throws PackagerException {
57396
3944e4c2f779 8223586: remove jpackage dead code and other cleanup
herrick
parents: 57391
diff changeset
    83
        return bundle(params, outputParentDir);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    84
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    85
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    86
    @Override
57150
fa68c2ab636d 8217894: jpackage CLI syntax changes
herrick
parents: 57140
diff changeset
    87
    public boolean supported(boolean platformInstaller) {
57438
4a31db8d42bd 8226599: use code coverage results to remove dead code
herrick
parents: 57414
diff changeset
    88
        return WinMsiBundler.isSupported();
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    89
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    90
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    91
    @Override
58114
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57909
diff changeset
    92
    public boolean isDefault() {
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57909
diff changeset
    93
        return true;
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57909
diff changeset
    94
    }
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57909
diff changeset
    95
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57909
diff changeset
    96
    @Override
57396
3944e4c2f779 8223586: remove jpackage dead code and other cleanup
herrick
parents: 57391
diff changeset
    97
    public boolean validate(Map<String, ? super Object> params)
57438
4a31db8d42bd 8226599: use code coverage results to remove dead code
herrick
parents: 57414
diff changeset
    98
            throws ConfigException {
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
    99
        return new WinMsiBundler().validate(params);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   100
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   101
57396
3944e4c2f779 8223586: remove jpackage dead code and other cleanup
herrick
parents: 57391
diff changeset
   102
    public File bundle(Map<String, ? super Object> params, File outdir)
57140
3dcb33ce7ced 8217751: jpackage messages and failures
herrick
parents: 57106
diff changeset
   103
            throws PackagerException {
57407
2c14fbeff1dc 8225569: jpackage app-image layout
herrick
parents: 57397
diff changeset
   104
57414
6eda749d3117 8225428: CLI change to remove "mode", rename to "package", and build only one target
herrick
parents: 57411
diff changeset
   105
        IOUtils.writableOutputDir(outdir.toPath());
6eda749d3117 8225428: CLI change to remove "mode", rename to "package", and build only one target
herrick
parents: 57411
diff changeset
   106
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   107
        File exeImageDir = EXE_IMAGE_DIR.fetchFrom(params);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   108
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   109
        // Write msi to temporary directory.
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   110
        File msi = new WinMsiBundler().bundle(params, exeImageDir);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   111
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   112
        try {
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   113
            return buildEXE(msi, outdir);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   114
        } catch (IOException ex) {
57140
3dcb33ce7ced 8217751: jpackage messages and failures
herrick
parents: 57106
diff changeset
   115
            Log.verbose(ex);
3dcb33ce7ced 8217751: jpackage messages and failures
herrick
parents: 57106
diff changeset
   116
            throw new PackagerException(ex);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   117
        }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   118
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   119
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   120
    private File buildEXE(File msi, File outdir)
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   121
            throws IOException {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   122
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   123
        Log.verbose(MessageFormat.format(
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   124
                getString("message.outputting-to-location"),
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   125
                outdir.getAbsolutePath()));
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   126
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   127
        // Copy template msi wrapper next to msi file
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   128
        String exePath = msi.getAbsolutePath();
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   129
        exePath = exePath.substring(0, exePath.lastIndexOf('.')) + ".exe";
58647
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents: 58114
diff changeset
   130
        try (InputStream is = OverridableResource.readDefault(EXE_WRAPPER_NAME)) {
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   131
            Files.copy(is, Path.of(exePath));
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   132
        }
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   133
        // Embed msi in msi wrapper exe.
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   134
        embedMSI(exePath, msi.getAbsolutePath());
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   135
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57438
diff changeset
   136
        Path dstExePath = Paths.get(outdir.getAbsolutePath(),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57438
diff changeset
   137
                Path.of(exePath).getFileName().toString());
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   138
        Files.deleteIfExists(dstExePath);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   139
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   140
        Files.copy(Path.of(exePath), dstExePath);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   141
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   142
        Log.verbose(MessageFormat.format(
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   143
                getString("message.output-location"),
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   144
                outdir.getAbsolutePath()));
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   145
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   146
        return dstExePath.toFile();
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   147
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   148
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   149
    private static String getString(String key)
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   150
            throws MissingResourceException {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   151
        return I18N.getString(key);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   152
    }
57411
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   153
4a80a72a891d 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler
herrick
parents: 57407
diff changeset
   154
    private static native int embedMSI(String exePath, String msiPath);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   155
}