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.text.MessageFormat;
  30 import java.util.*;
  31 
  32 import static jdk.incubator.jpackage.internal.StandardBundlerParam.*;
  33 
  34 public class LinuxAppBundler extends AbstractImageBundler {
  35 
  36     static final BundlerParamInfo<File> ICON_PNG =
  37             new StandardBundlerParam<>(
  38             "icon.png",
  39             File.class,
  40             params -> {
  41                 File f = ICON.fetchFrom(params);
  42                 if (f != null && !f.getName().toLowerCase().endsWith(".png")) {
  43                     Log.error(MessageFormat.format(
  44                             I18N.getString("message.icon-not-png"), f));
  45                     return null;
  46                 }
  47                 return f;
  48             },
  49             (s, p) -> new File(s));
  50 
  51     static final BundlerParamInfo<String> LINUX_INSTALL_DIR =
  52             new StandardBundlerParam<>(
  53             "linux-install-dir",
  54             String.class,
  55             params -> {
  56                  String dir = INSTALL_DIR.fetchFrom(params);
  57                  if (dir != null) {
  58                      if (dir.endsWith("/")) {
  59                          dir = dir.substring(0, dir.length()-1);
  60                      }
  61                      return dir;
  62                  }
  63                  return "/opt";
  64              },
  65             (s, p) -> s
  66     );
  67 
  68     static final BundlerParamInfo<String> LINUX_PACKAGE_DEPENDENCIES =
  69             new StandardBundlerParam<>(
  70             Arguments.CLIOptions.LINUX_PACKAGE_DEPENDENCIES.getId(),
  71             String.class,
  72             params -> {
  73                  return "";
  74              },
  75             (s, p) -> s
  76     );
  77 
  78     @Override
  79     public boolean validate(Map<String, ? super Object> params)
  80             throws ConfigException {
  81         try {
  82             Objects.requireNonNull(params);
  83             return doValidate(params);
  84         } catch (RuntimeException re) {
  85             if (re.getCause() instanceof ConfigException) {
  86                 throw (ConfigException) re.getCause();
  87             } else {
  88                 throw new ConfigException(re);
  89             }
  90         }
  91     }
  92 
  93     private boolean doValidate(Map<String, ? super Object> params)
  94             throws ConfigException {
  95 
  96         imageBundleValidation(params);
  97 
  98         return true;
  99     }
 100 
 101     File doBundle(Map<String, ? super Object> params, File outputDirectory,
 102             boolean dependentTask) throws PackagerException {
 103         if (StandardBundlerParam.isRuntimeInstaller(params)) {
 104             return PREDEFINED_RUNTIME_IMAGE.fetchFrom(params);
 105         } else {
 106             return doAppBundle(params, outputDirectory, dependentTask);
 107         }
 108     }
 109 
 110     private File doAppBundle(Map<String, ? super Object> params,
 111             File outputDirectory, boolean dependentTask)
 112             throws PackagerException {
 113         try {
 114             File rootDirectory = createRoot(params, outputDirectory,
 115                     dependentTask, APP_NAME.fetchFrom(params));
 116             AbstractAppImageBuilder appBuilder = new LinuxAppImageBuilder(
 117                     params, outputDirectory.toPath());
 118             if (PREDEFINED_RUNTIME_IMAGE.fetchFrom(params) == null ) {
 119                 JLinkBundlerHelper.execute(params, appBuilder);
 120             } else {
 121                 StandardBundlerParam.copyPredefinedRuntimeImage(
 122                         params, appBuilder);
 123             }
 124             return rootDirectory;
 125         } catch (PackagerException pe) {
 126             throw pe;
 127         } catch (Exception ex) {
 128             Log.verbose(ex);
 129             throw new PackagerException(ex);
 130         }
 131     }
 132 
 133     @Override
 134     public String getName() {
 135         return I18N.getString("app.bundler.name");
 136     }
 137 
 138     @Override
 139     public String getID() {
 140         return "linux.app";
 141     }
 142 
 143     @Override
 144     public String getBundleType() {
 145         return "IMAGE";
 146     }
 147 
 148     @Override
 149     public File execute(Map<String, ? super Object> params,
 150             File outputParentDir) throws PackagerException {
 151         return doBundle(params, outputParentDir, false);
 152     }
 153 
 154     @Override
 155     public boolean supported(boolean runtimeInstaller) {
 156         return true;
 157     }
 158 
 159     @Override
 160     public boolean isDefault() {
 161         return false;
 162     }
 163 
 164 }