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     static final BundlerParamInfo<File> ICON_ICO =
  41             new StandardBundlerParam<>(
  42             "icon.ico",
  43             File.class,
  44             params -> {
  45                 File f = ICON.fetchFrom(params);
  46                 if (f != null && !f.getName().toLowerCase().endsWith(".ico")) {
  47                     Log.error(MessageFormat.format(
  48                             I18N.getString("message.icon-not-ico"), f));
  49                     return null;
  50                 }
  51                 return f;
  52             },
  53             (s, p) -> new File(s));
  54 
  55     @Override
  56     public boolean validate(Map<String, ? super Object> params)
  57             throws ConfigException {
  58         try {
  59             Objects.requireNonNull(params);
  60             return doValidate(params);
  61         } catch (RuntimeException re) {
  62             if (re.getCause() instanceof ConfigException) {
  63                 throw (ConfigException) re.getCause();
  64             } else {
  65                 throw new ConfigException(re);
  66             }
  67         }
  68     }
  69 
  70     // to be used by chained bundlers, e.g. by EXE bundler to avoid
  71     // skipping validation if p.type does not include "image"
  72     private boolean doValidate(Map<String, ? super Object> p)
  73             throws ConfigException {
  74 
  75         imageBundleValidation(p);
  76         return true;
  77     }
  78 
  79     public boolean bundle(Map<String, ? super Object> p, File outputDirectory)
  80             throws PackagerException {
  81         return doBundle(p, outputDirectory, false) != null;
  82     }
  83 
  84     File doBundle(Map<String, ? super Object> p, File outputDirectory,
  85             boolean dependentTask) throws PackagerException {
  86         if (StandardBundlerParam.isRuntimeInstaller(p)) {
  87             return PREDEFINED_RUNTIME_IMAGE.fetchFrom(p);
  88         } else {
  89             return doAppBundle(p, outputDirectory, dependentTask);
  90         }
  91     }
  92 
  93     File doAppBundle(Map<String, ? super Object> p, File outputDirectory,
  94             boolean dependentTask) throws PackagerException {
  95         try {
  96             File rootDirectory = createRoot(p, outputDirectory, dependentTask,
  97                     APP_NAME.fetchFrom(p));
  98             AbstractAppImageBuilder appBuilder =
  99                     new WindowsAppImageBuilder(p, outputDirectory.toPath());
 100             if (PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null ) {
 101                 JLinkBundlerHelper.execute(p, appBuilder);
 102             } else {
 103                 StandardBundlerParam.copyPredefinedRuntimeImage(p, appBuilder);
 104             }
 105             if (!dependentTask) {
 106                 Log.verbose(MessageFormat.format(
 107                         I18N.getString("message.result-dir"),
 108                         outputDirectory.getAbsolutePath()));
 109             }
 110             return rootDirectory;
 111         } catch (PackagerException pe) {
 112             throw pe;
 113         } catch (Exception e) {
 114             Log.verbose(e);
 115             throw new PackagerException(e);
 116         }
 117     }
 118 
 119     @Override
 120     public String getName() {
 121         return I18N.getString("app.bundler.name");
 122     }
 123 
 124     @Override
 125     public String getID() {
 126         return "windows.app";
 127     }
 128 
 129     @Override
 130     public String getBundleType() {
 131         return "IMAGE";
 132     }
 133 
 134     @Override
 135     public File execute(Map<String, ? super Object> params,
 136             File outputParentDir) throws PackagerException {
 137         return doBundle(params, outputParentDir, false);
 138     }
 139 
 140     @Override
 141     public boolean supported(boolean platformInstaller) {
 142         return true;
 143     }
 144 
 145     @Override
 146     public boolean isDefault() {
 147         return false;
 148     }
 149 
 150 }