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.tools.packager;
  27 
  28 import com.oracle.tools.packager.jnlp.JNLPBundler;
  29 import com.oracle.tools.packager.linux.LinuxRpmBundler;
  30 import com.oracle.tools.packager.mac.MacAppStoreBundler;
  31 import com.oracle.tools.packager.mac.MacDmgBundler;
  32 import com.oracle.tools.packager.mac.MacPkgBundler;
  33 import com.oracle.tools.packager.linux.LinuxAppBundler;
  34 import com.oracle.tools.packager.linux.LinuxDebBundler;
  35 import com.oracle.tools.packager.mac.MacAppBundler;
  36 import com.oracle.tools.packager.windows.WinAppBundler;
  37 import com.oracle.tools.packager.windows.WinExeBundler;
  38 import com.oracle.tools.packager.windows.WinMsiBundler;
  39 
  40 import java.util.Arrays;
  41 import java.util.Collection;
  42 import java.util.Collections;
  43 import java.util.ServiceLoader;
  44 import java.util.concurrent.CopyOnWriteArrayList;
  45 
  46 /**
  47  * A basic bundlers collection that loads the OpenJFX default bundlers.
  48  * Loads the bundlers common to OpenJFX.
  49  * <UL>
  50  *     <LI>Windows file image</LI>
  51  *     <LI>Mac .app</LI>
  52  *     <LI>Linux file image</LI>
  53  *     <LI>Windows MSI</LI>
  54  *     <LI>Windows EXE</LI>
  55  *     <LI>Mac DMG</LI>
  56  *     <LI>Mac PKG</LI>
  57  *     <LI>Linux DEB</LI>
  58  *     <LI>Linux RPM</LI>
  59  *
  60  * </UL>
  61  */
  62 public class BasicBundlers implements Bundlers {
  63 
  64     boolean defaultsLoaded = false;
  65 
  66     private Collection<Bundler> bundlers = new CopyOnWriteArrayList<>();
  67 
  68     public Collection<Bundler> getBundlers() {
  69         return Collections.unmodifiableCollection(bundlers);
  70     }
  71 
  72     public Collection<Bundler> getBundlers(String type) {
  73         if (type == null) return Collections.emptySet();
  74         switch (type) {
  75             case "NONE":
  76                 return Collections.emptySet();
  77             case "ALL":
  78                 return getBundlers();
  79             default:
  80                 return Arrays.asList(getBundlers().stream()
  81                         .filter(b -> type.equalsIgnoreCase(b.getBundleType()))
  82                         .toArray(Bundler[]::new));
  83         }
  84     }
  85 
  86     /**
  87      * A list of the "standard" parameters that bundlers should support
  88      * or fall back to when their specific parameters are not used.
  89      * @return an unmodifieable collection of the standard parameters.
  90      */
  91     public Collection<BundlerParamInfo> getStandardParameters() {
  92         //TODO enumerate the stuff in BundleParams
  93         return null;
  94     }
  95 
  96     /**
  97      * Loads the bundlers common to OpenJFX.
  98      * <UL>
  99      *     <LI>Windows file image</LI>
 100      *     <LI>Mac .app</LI>
 101      *     <LI>Linux file image</LI>
 102      *     <LI>Windows MSI</LI>
 103      *     <LI>Windows EXE</LI>
 104      *     <LI>Mac DMG</LI>
 105      *     <LI>Mac PKG</LI>
 106      *     <LI>Linux DEB</LI>
 107      *     <LI>Linux RPM</LI>
 108      *
 109      * </UL>
 110      */
 111     public void loadDefaultBundlers() {
 112         if (defaultsLoaded) return;
 113 
 114         bundlers.add(new WinAppBundler());
 115         bundlers.add(new WinExeBundler());
 116         bundlers.add(new WinMsiBundler());
 117 
 118         bundlers.add(new LinuxAppBundler());
 119         bundlers.add(new LinuxDebBundler());
 120         bundlers.add(new LinuxRpmBundler());
 121 
 122         bundlers.add(new MacAppBundler());
 123         bundlers.add(new MacDmgBundler());
 124         bundlers.add(new MacPkgBundler());
 125         bundlers.add(new MacAppStoreBundler());
 126 
 127         bundlers.add(new JNLPBundler());
 128 
 129         defaultsLoaded = true;
 130     }
 131 
 132     /**
 133      * Loads bundlers from the META-INF/services direct
 134      */
 135     public void loadBundlersFromServices(ClassLoader cl) {
 136         ServiceLoader<Bundler> loader = ServiceLoader.load(Bundler.class, cl);
 137         for (Bundler aLoader : loader) {
 138             bundlers.add(aLoader);
 139         }
 140     }
 141 
 142     public void loadBundler(Bundler bundler) {
 143         bundlers.add(bundler);
 144     }
 145 }