src/jdk.packager/windows/classes/jdk/packager/internal/windows/WinAppBundler.java
branchJDK-8200758-branch
changeset 57017 1b08af362a30
parent 57016 f63f13da91c0
child 57018 9d782e357916
equal deleted inserted replaced
57016:f63f13da91c0 57017:1b08af362a30
     1 /*
       
     2  * Copyright (c) 2012, 2018, 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.packager.internal.windows;
       
    27 
       
    28 import jdk.packager.internal.AbstractImageBundler;
       
    29 import jdk.packager.internal.BundlerParamInfo;
       
    30 import jdk.packager.internal.ConfigException;
       
    31 import jdk.packager.internal.IOUtils;
       
    32 import jdk.packager.internal.Log;
       
    33 import jdk.packager.internal.Platform;
       
    34 import jdk.packager.internal.StandardBundlerParam;
       
    35 import jdk.packager.internal.UnsupportedPlatformException;
       
    36 import jdk.packager.internal.builders.windows.WindowsAppImageBuilder;
       
    37 import jdk.packager.internal.resources.windows.WinResources;
       
    38 import jdk.packager.internal.JLinkBundlerHelper;
       
    39 import jdk.packager.internal.Arguments;
       
    40 import jdk.packager.internal.builders.AbstractAppImageBuilder;
       
    41 
       
    42 import java.io.ByteArrayOutputStream;
       
    43 import java.io.File;
       
    44 import java.io.IOException;
       
    45 import java.io.PrintStream;
       
    46 import java.text.MessageFormat;
       
    47 import java.util.Arrays;
       
    48 import java.util.Collection;
       
    49 import java.util.Map;
       
    50 import java.util.ResourceBundle;
       
    51 
       
    52 import static jdk.packager.internal.windows.WindowsBundlerParam.*;
       
    53 import static jdk.packager.internal.windows.WinMsiBundler.WIN_APP_IMAGE;
       
    54 
       
    55 public class WinAppBundler extends AbstractImageBundler {
       
    56 
       
    57     private static final ResourceBundle I18N =
       
    58             ResourceBundle.getBundle(
       
    59             "jdk.packager.internal.resources.windows.WinAppBundler");
       
    60 
       
    61     public static final BundlerParamInfo<File> ICON_ICO =
       
    62             new StandardBundlerParam<>(
       
    63             I18N.getString("param.icon-ico.name"),
       
    64             I18N.getString("param.icon-ico.description"),
       
    65             "icon.ico",
       
    66             File.class,
       
    67             params -> {
       
    68                 File f = ICON.fetchFrom(params);
       
    69                 if (f != null && !f.getName().toLowerCase().endsWith(".ico")) {
       
    70                     Log.info(MessageFormat.format(
       
    71                             I18N.getString("message.icon-not-ico"), f));
       
    72                     return null;
       
    73                 }
       
    74                 return f;
       
    75             },
       
    76             (s, p) -> new File(s));
       
    77 
       
    78     public WinAppBundler() {
       
    79         super();
       
    80         baseResourceLoader = WinResources.class;
       
    81     }
       
    82 
       
    83     public final static String WIN_BUNDLER_PREFIX =
       
    84             BUNDLER_PREFIX + "windows/";
       
    85 
       
    86     @Override
       
    87     public boolean validate(Map<String, ? super Object> params)
       
    88             throws UnsupportedPlatformException, ConfigException {
       
    89         try {
       
    90             if (params == null) throw new ConfigException(
       
    91                     I18N.getString("error.parameters-null"),
       
    92                     I18N.getString("error.parameters-null.advice"));
       
    93 
       
    94             return doValidate(params);
       
    95         } catch (RuntimeException re) {
       
    96             if (re.getCause() instanceof ConfigException) {
       
    97                 throw (ConfigException) re.getCause();
       
    98             } else {
       
    99                 throw new ConfigException(re);
       
   100             }
       
   101         }
       
   102     }
       
   103 
       
   104     // to be used by chained bundlers, e.g. by EXE bundler to avoid
       
   105     // skipping validation if p.type does not include "image"
       
   106     boolean doValidate(Map<String, ? super Object> p)
       
   107             throws UnsupportedPlatformException, ConfigException {
       
   108         if (Platform.getPlatform() != Platform.WINDOWS) {
       
   109             throw new UnsupportedPlatformException();
       
   110         }
       
   111 
       
   112         imageBundleValidation(p);
       
   113 
       
   114         if (StandardBundlerParam.getPredefinedAppImage(p) != null) {
       
   115             return true;
       
   116         }
       
   117 
       
   118         // Make sure that jpackager.exe exists.
       
   119         File tool = new File(
       
   120                 System.getProperty("java.home") + "\\bin\\jpackager.exe");
       
   121 
       
   122         if (!tool.exists()) {
       
   123             throw new ConfigException(
       
   124                     I18N.getString("error.no-windows-resources"),
       
   125                     I18N.getString("error.no-windows-resources.advice"));
       
   126         }
       
   127 
       
   128         // validate runtime bit-architectire
       
   129         testRuntimeBitArchitecture(p);
       
   130 
       
   131         return true;
       
   132     }
       
   133 
       
   134     private static void testRuntimeBitArchitecture(
       
   135             Map<String, ? super Object> params) throws ConfigException {
       
   136         if ("true".equalsIgnoreCase(System.getProperty(
       
   137                 "fxpackager.disableBitArchitectureMismatchCheck"))) {
       
   138             Log.debug(I18N.getString("message.disable-bit-architecture-check"));
       
   139             return;
       
   140         }
       
   141 
       
   142         if ((BIT_ARCH_64.fetchFrom(params) !=
       
   143                 BIT_ARCH_64_RUNTIME.fetchFrom(params))) {
       
   144             throw new ConfigException(
       
   145                     I18N.getString("error.bit-architecture-mismatch"),
       
   146                     I18N.getString("error.bit-architecture-mismatch.advice"));
       
   147         }
       
   148     }
       
   149 
       
   150     // it is static for the sake of sharing with "Exe" bundles
       
   151     // that may skip calls to validate/bundle in this class!
       
   152     private static File getRootDir(File outDir, Map<String, ? super Object> p) {
       
   153         return new File(outDir, APP_NAME.fetchFrom(p));
       
   154     }
       
   155 
       
   156     private static boolean usePredefineAppName(Map<String, ? super Object> p) {
       
   157         return (PREDEFINED_APP_IMAGE.fetchFrom(p) != null);
       
   158     }
       
   159 
       
   160     private static String appName;
       
   161     synchronized static String getAppName(
       
   162             Map<String, ? super Object> p) {
       
   163         // If we building from predefined app image, then we should use names
       
   164         // from image and not from CLI.
       
   165         if (usePredefineAppName(p)) {
       
   166             if (appName == null) {
       
   167                 // Use WIN_APP_IMAGE here, since we already copy pre-defined
       
   168                 // image to WIN_APP_IMAGE
       
   169                 File appImageDir = new File(
       
   170                         WIN_APP_IMAGE.fetchFrom(p).toString() + "\\app");
       
   171                 File [] files = appImageDir.listFiles(
       
   172                         (File dir, String name) -> name.endsWith(".cfg"));
       
   173                 if (files == null || files.length == 0) {
       
   174                     throw new RuntimeException(MessageFormat.format(
       
   175                         I18N.getString("error.cannot-find-launcher"),
       
   176                         appImageDir));
       
   177                 } else {
       
   178                     appName = files[0].getName();
       
   179                     int index = appName.indexOf(".");
       
   180                     if (index != -1) {
       
   181                         appName = appName.substring(0, index);
       
   182                     }
       
   183                     if (files.length > 1) {
       
   184                         Log.info(MessageFormat.format(I18N.getString(
       
   185                                 "message.multiple-launchers"), appName));
       
   186                     }
       
   187                 }
       
   188                 return appName;
       
   189             } else {
       
   190                 return appName;
       
   191             }
       
   192         }
       
   193 
       
   194         return APP_NAME.fetchFrom(p);
       
   195     }
       
   196 
       
   197     public static String getLauncherName(Map<String, ? super Object> p) {
       
   198         return getAppName(p) + ".exe";
       
   199     }
       
   200 
       
   201     public static String getLauncherCfgName(Map<String, ? super Object> p) {
       
   202         return "app\\" + getAppName(p) +".cfg";
       
   203     }
       
   204 
       
   205     public boolean bundle(Map<String, ? super Object> p, File outputDirectory) {
       
   206         return doBundle(p, outputDirectory, false) != null;
       
   207     }
       
   208 
       
   209     private File createRoot(Map<String, ? super Object> p,
       
   210             File outputDirectory, boolean dependentTask) throws IOException {
       
   211         if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) {
       
   212             throw new RuntimeException(MessageFormat.format(
       
   213                     I18N.getString("error.cannot-create-output-dir"),
       
   214                     outputDirectory.getAbsolutePath()));
       
   215         }
       
   216         if (!outputDirectory.canWrite()) {
       
   217             throw new RuntimeException(MessageFormat.format(
       
   218                     I18N.getString("error.cannot-write-to-output-dir"),
       
   219                     outputDirectory.getAbsolutePath()));
       
   220         }
       
   221         if (!dependentTask) {
       
   222             Log.info(MessageFormat.format(
       
   223                     I18N.getString("message.creating-app-bundle"),
       
   224                     APP_NAME.fetchFrom(p), outputDirectory.getAbsolutePath()));
       
   225         }
       
   226 
       
   227         // Create directory structure
       
   228         File rootDirectory = getRootDir(outputDirectory, p);
       
   229         IOUtils.deleteRecursive(rootDirectory);
       
   230         rootDirectory.mkdirs();
       
   231 
       
   232         if (!p.containsKey(JLinkBundlerHelper.JLINK_BUILDER.getID())) {
       
   233             p.put(JLinkBundlerHelper.JLINK_BUILDER.getID(),
       
   234                     "windowsapp-image-builder");
       
   235         }
       
   236 
       
   237         return rootDirectory;
       
   238     }
       
   239 
       
   240     File doBundle(Map<String, ? super Object> p,
       
   241                 File outputDirectory, boolean dependentTask) {
       
   242         if (Arguments.CREATE_JRE_INSTALLER.fetchFrom(p)) {
       
   243             return doJreBundle(p, outputDirectory, dependentTask);
       
   244         } else {
       
   245             return doAppBundle(p, outputDirectory, dependentTask);
       
   246         }
       
   247     }
       
   248 
       
   249     File doJreBundle(Map<String, ? super Object> p,
       
   250             File outputDirectory, boolean dependentTask) {
       
   251         try {
       
   252             File rootDirectory = createRoot(p, outputDirectory, dependentTask);
       
   253             AbstractAppImageBuilder appBuilder = new WindowsAppImageBuilder(
       
   254                     APP_NAME.fetchFrom(p),
       
   255                     outputDirectory.toPath());
       
   256             File predefined = PREDEFINED_RUNTIME_IMAGE.fetchFrom(p);
       
   257             if (predefined == null ) {
       
   258                 JLinkBundlerHelper.generateServerJre(p, appBuilder);
       
   259             } else {
       
   260                 return predefined;
       
   261             }
       
   262             return rootDirectory;
       
   263         } catch (IOException ex) {
       
   264             Log.info("Exception: "+ex);
       
   265             Log.debug(ex);
       
   266             return null;
       
   267         } catch (Exception ex) {
       
   268             Log.info("Exception: "+ex);
       
   269             Log.debug(ex);
       
   270             return null;
       
   271         }
       
   272     }
       
   273 
       
   274     File doAppBundle(Map<String, ? super Object> p,
       
   275             File outputDirectory, boolean dependentTask) {
       
   276         try {
       
   277             File rootDirectory =
       
   278                     createRoot(p, outputDirectory, dependentTask);
       
   279             AbstractAppImageBuilder appBuilder =
       
   280                     new WindowsAppImageBuilder(p, outputDirectory.toPath());
       
   281             if (PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null ) {
       
   282                 JLinkBundlerHelper.execute(p, appBuilder);
       
   283             } else {
       
   284                 StandardBundlerParam.copyPredefinedRuntimeImage(p, appBuilder);
       
   285             }
       
   286             if (!dependentTask) {
       
   287                 Log.info(MessageFormat.format(
       
   288                         I18N.getString("message.result-dir"),
       
   289                         outputDirectory.getAbsolutePath()));
       
   290             }
       
   291             return rootDirectory;
       
   292         } catch (IOException ex) {
       
   293             Log.info(ex.toString());
       
   294             Log.verbose(ex);
       
   295             return null;
       
   296         } catch (Exception ex) {
       
   297             Log.info("Exception: "+ex);
       
   298             Log.debug(ex);
       
   299             return null;
       
   300         }
       
   301     }
       
   302 
       
   303     private static final String RUNTIME_AUTO_DETECT = ".runtime.autodetect";
       
   304 
       
   305     public static void extractFlagsFromRuntime(
       
   306             Map<String, ? super Object> params) {
       
   307         if (params.containsKey(".runtime.autodetect")) return;
       
   308 
       
   309         params.put(RUNTIME_AUTO_DETECT, "attempted");
       
   310 
       
   311         String commandline;
       
   312         File runtimePath = JLinkBundlerHelper.getJDKHome(params).toFile();
       
   313         File launcherPath = new File(runtimePath, "bin\\java.exe");
       
   314 
       
   315         ProcessBuilder pb =
       
   316                  new ProcessBuilder(launcherPath.getAbsolutePath(), "-version");
       
   317         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
       
   318             try (PrintStream pout = new PrintStream(baos)) {
       
   319                 IOUtils.exec(pb, Log.isDebug(), true, pout);
       
   320             }
       
   321 
       
   322             commandline = baos.toString();
       
   323         } catch (IOException e) {
       
   324             e.printStackTrace();
       
   325             params.put(RUNTIME_AUTO_DETECT, "failed");
       
   326             return;
       
   327         }
       
   328 
       
   329         AbstractImageBundler.extractFlagsFromVersion(params, commandline);
       
   330         params.put(RUNTIME_AUTO_DETECT, "succeeded");
       
   331     }
       
   332 
       
   333     @Override
       
   334     public String getName() {
       
   335         return I18N.getString("bundler.name");
       
   336     }
       
   337 
       
   338     @Override
       
   339     public String getDescription() {
       
   340         return I18N.getString("bundler.description");
       
   341     }
       
   342 
       
   343     @Override
       
   344     public String getID() {
       
   345         return "windows.app";
       
   346     }
       
   347 
       
   348     @Override
       
   349     public String getBundleType() {
       
   350         return "IMAGE";
       
   351     }
       
   352 
       
   353     @Override
       
   354     public Collection<BundlerParamInfo<?>> getBundleParameters() {
       
   355         return getAppBundleParameters();
       
   356     }
       
   357 
       
   358     public static Collection<BundlerParamInfo<?>> getAppBundleParameters() {
       
   359         return Arrays.asList(
       
   360                 APP_NAME,
       
   361                 APP_RESOURCES,
       
   362                 ARGUMENTS,
       
   363                 CLASSPATH,
       
   364                 ICON_ICO,
       
   365                 JVM_OPTIONS,
       
   366                 JVM_PROPERTIES,
       
   367                 MAIN_CLASS,
       
   368                 MAIN_JAR,
       
   369                 PREFERENCES_ID,
       
   370                 VERSION,
       
   371                 VERBOSE
       
   372             );
       
   373     }
       
   374 
       
   375     @Override
       
   376     public File execute(
       
   377             Map<String, ? super Object> params, File outputParentDir) {
       
   378         return doBundle(params, outputParentDir, false);
       
   379     }
       
   380 
       
   381     @Override
       
   382     public boolean supported() {
       
   383         return (Platform.getPlatform() == Platform.WINDOWS);
       
   384     }
       
   385 
       
   386 }