1 /*
   2  * Copyright (c) 2012, 2019, 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.incubator.jpackage.internal;
  27 
  28 import java.io.File;
  29 import java.nio.file.Path;
  30 import java.text.MessageFormat;
  31 import java.util.*;
  32 
  33 import static jdk.incubator.jpackage.internal.WindowsBundlerParam.*;
  34 
  35 public class WinAppBundler extends AbstractImageBundler {
  36 
  37     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  38             "jdk.incubator.jpackage.internal.resources.WinResources");
  39 
  40     @Override
  41     public boolean validate(Map<String, ? super Object> params)
  42             throws ConfigException {
  43         try {
  44             Objects.requireNonNull(params);
  45             return doValidate(params);
  46         } catch (RuntimeException re) {
  47             if (re.getCause() instanceof ConfigException) {
  48                 throw (ConfigException) re.getCause();
  49             } else {
  50                 throw new ConfigException(re);
  51             }
  52         }
  53     }
  54 
  55     // to be used by chained bundlers, e.g. by EXE bundler to avoid
  56     // skipping validation if p.type does not include "image"
  57     private boolean doValidate(Map<String, ? super Object> p)
  58             throws ConfigException {
  59 
  60         imageBundleValidation(p);
  61         return true;
  62     }
  63 
  64     public boolean bundle(Map<String, ? super Object> p, File outputDirectory)
  65             throws PackagerException {
  66         return doBundle(p, outputDirectory, false) != null;
  67     }
  68 
  69     File doBundle(Map<String, ? super Object> p, File outputDirectory,
  70             boolean dependentTask) throws PackagerException {
  71         if (StandardBundlerParam.isRuntimeInstaller(p)) {
  72             return StandardBundlerParam.getPredefinedRuntime(p);
  73         } else {
  74             return doAppBundle(p, outputDirectory, dependentTask);
  75         }
  76     }
  77 
  78     File doAppBundle(Map<String, ? super Object> p, File outputDirectory,
  79             boolean dependentTask) throws PackagerException {
  80         try {
  81             File rootDirectory = createRoot(p, outputDirectory, dependentTask,
  82                     APP_NAME.fetchFrom(p));
  83             AbstractAppImageBuilder appBuilder =
  84                     new WindowsAppImageBuilder(p, outputDirectory.toPath());
  85             if (PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null ) {
  86                 JLinkBundlerHelper.execute(p, appBuilder);
  87             } else {
  88                 StandardBundlerParam.copyPredefinedRuntimeImage(p, appBuilder);
  89             }
  90             if (!dependentTask) {
  91                 Log.verbose(MessageFormat.format(
  92                         I18N.getString("message.result-dir"),
  93                         outputDirectory.getAbsolutePath()));
  94             }
  95             return rootDirectory;
  96         } catch (PackagerException pe) {
  97             throw pe;
  98         } catch (Exception e) {
  99             Log.verbose(e);
 100             throw new PackagerException(e);
 101         }
 102     }
 103 
 104     @Override
 105     public String getName() {
 106         return I18N.getString("app.bundler.name");
 107     }
 108 
 109     @Override
 110     public String getID() {
 111         return "windows.app";
 112     }
 113 
 114     @Override
 115     public String getBundleType() {
 116         return "IMAGE";
 117     }
 118 
 119     @Override
 120     public File execute(Map<String, ? super Object> params,
 121             File outputParentDir) throws PackagerException {
 122         return doBundle(params, outputParentDir, false);
 123     }
 124 
 125     @Override
 126     public boolean supported(boolean platformInstaller) {
 127         return true;
 128     }
 129 
 130     @Override
 131     public boolean isDefault() {
 132         return false;
 133     }
 134 
 135 }