1 /*
   2  * Copyright (c) 2014, 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 com.oracle.bundlers.mac;
  27 
  28 import com.oracle.bundlers.AbstractBundler;
  29 import com.oracle.bundlers.BundlerParamInfo;
  30 import com.oracle.bundlers.StandardBundlerParam;
  31 import com.sun.javafx.tools.packager.bundlers.BundleType;
  32 import com.sun.javafx.tools.packager.bundlers.ConfigException;
  33 import com.sun.javafx.tools.packager.bundlers.MacAppBundler;
  34 import com.sun.javafx.tools.packager.bundlers.UnsupportedPlatformException;
  35 
  36 import java.io.File;
  37 import java.text.MessageFormat;import java.util.Arrays;
  38 import java.util.Collection;
  39 import java.util.LinkedHashSet;
  40 import java.util.Map;
  41 import java.util.ResourceBundle;
  42 
  43 import static com.oracle.bundlers.StandardBundlerParam.*;
  44 
  45 public abstract class MacBaseInstallerBundler extends AbstractBundler {
  46 
  47     private static final ResourceBundle I18N =
  48             ResourceBundle.getBundle("com.oracle.bundlers.mac.MacBaseInstallerBundler");
  49 
  50     //This could be generalized more to be for any type of Image Bundler
  51     protected final BundlerParamInfo<MacAppBundler> APP_BUNDLER = new StandardBundlerParam<>(
  52             I18N.getString("param.app-bundler.name"),
  53             I18N.getString("param.app-bundle.description"),
  54             "mac.app.bundler",
  55             MacAppBundler.class,
  56             null,
  57             params -> new MacAppBundler(),
  58             false,
  59             (s, p) -> null);
  60 
  61     protected final BundlerParamInfo<File> APP_IMAGE_BUILD_ROOT = new StandardBundlerParam<>(
  62             I18N.getString("param.app-image-build-root.name"),
  63             I18N.getString("param.app-image-build-root.description"),
  64             "mac.app.imageRoot",
  65             File.class,
  66             null,
  67             params -> {
  68                 File imageDir = IMAGES_ROOT.fetchFrom(params);
  69                 if (!imageDir.exists()) imageDir.mkdirs();
  70                 return new File(imageDir, getID()+ ".image");
  71             },
  72             false,
  73             (s, p) -> new File(s));
  74 
  75     public static final StandardBundlerParam<File> MAC_APP_IMAGE = new StandardBundlerParam<>(
  76             I18N.getString("param.app-image.name"),
  77             I18N.getString("param.app-image.description"),
  78             "mac.app.image",
  79             File.class,
  80             null,
  81             params -> null,
  82             false,
  83             (s, p) -> new File(s));
  84 
  85 
  86     protected final BundlerParamInfo<MacDaemonBundler> DAEMON_BUNDLER = new StandardBundlerParam<>(
  87             I18N.getString("param.daemon-bundler.name"),
  88             I18N.getString("param.daemon-bundler.description"),
  89             "mac.daemon.bundler",
  90             MacDaemonBundler.class,
  91             null,
  92             params -> new MacDaemonBundler(),
  93             false,
  94             (s, p) -> null);
  95 
  96 
  97     protected final BundlerParamInfo<File> DAEMON_IMAGE_BUILD_ROOT = new StandardBundlerParam<>(
  98             I18N.getString("param.daemon-image-build-root.name"),
  99             I18N.getString("param.daemon-image-build-root.description"),
 100             "mac.daemon.image",
 101             File.class,
 102             null,
 103             params -> {
 104                 File imageDir = IMAGES_ROOT.fetchFrom(params);
 105                 if (!imageDir.exists()) imageDir.mkdirs();
 106                 return new File(imageDir, getID()+ ".daemon");
 107             },
 108             false,
 109             (s, p) -> new File(s));
 110 
 111 
 112     protected final BundlerParamInfo<File> CONFIG_ROOT = new StandardBundlerParam<>(
 113             I18N.getString("param.config-root.name"),
 114             I18N.getString("param.config-root.description"),
 115             "configRoot",
 116             File.class,
 117             null,
 118             params -> {
 119                 File imagesRoot = new File(BUILD_ROOT.fetchFrom(params), "macosx");
 120                 imagesRoot.mkdirs();
 121                 return imagesRoot;
 122             },
 123             false, (s, p) -> null);
 124 
 125     public static File getPredefinedImage(Map<String, ? super Object> p) {
 126         File applicationImage = null;
 127         if (MAC_APP_IMAGE.fetchFrom(p) != null) {
 128             applicationImage = MAC_APP_IMAGE.fetchFrom(p);
 129             if (!applicationImage.exists()) {
 130                 throw new RuntimeException(
 131                         MessageFormat.format(I18N.getString("message.app-image-dir-does-not-exist"), MAC_APP_IMAGE.getID(), applicationImage.toString()));
 132             }
 133         }
 134         return applicationImage;
 135     }
 136 
 137     protected void validateAppImageAndBundeler(Map<String, ? super Object> params) throws ConfigException, UnsupportedPlatformException {
 138         if (MAC_APP_IMAGE.fetchFrom(params) != null) {
 139             File applicationImage = MAC_APP_IMAGE.fetchFrom(params);
 140             if (!applicationImage.exists()) {
 141                 throw new ConfigException(
 142                         MessageFormat.format(I18N.getString("message.app-image-dir-does-not-exist"), MAC_APP_IMAGE.getID(), applicationImage.toString()),
 143                         MessageFormat.format(I18N.getString("message.app-image-dir-does-not-exist.advice"), MAC_APP_IMAGE.getID()));
 144             }
 145         } else {
 146             APP_BUNDLER.fetchFrom(params).doValidate(params);
 147         }
 148     }
 149 
 150     protected File prepareAppBundle(Map<String, ? super Object> p) {
 151         if (getPredefinedImage(p) != null) {
 152             return null;
 153         }
 154 
 155         File appImageRoot = APP_IMAGE_BUILD_ROOT.fetchFrom(p);
 156         return APP_BUNDLER.fetchFrom(p).doBundle(p, appImageRoot, true);
 157     }
 158 
 159     protected File prepareDaemonBundle(Map<String, ? super Object> p) throws ConfigException {
 160         File daemonImageRoot = DAEMON_IMAGE_BUILD_ROOT.fetchFrom(p);
 161         return DAEMON_BUNDLER.fetchFrom(p).doBundle(p, daemonImageRoot, true);        
 162     }
 163 
 164 
 165     @Override
 166     public Collection<BundlerParamInfo<?>> getBundleParameters() {
 167         Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>();
 168 
 169         results.addAll(MacAppBundler.getAppBundleParameters());
 170         results.addAll(Arrays.asList(
 171                 APP_BUNDLER,
 172                 CONFIG_ROOT,
 173                 APP_IMAGE_BUILD_ROOT,
 174                 MAC_APP_IMAGE
 175         ));
 176 
 177         return results;
 178     }
 179 
 180     @Override
 181     public BundleType getBundleType() {
 182         return BundleType.INSTALLER;
 183     }
 184 }