1 /*
   2  * Copyright (c) 2011, 2013, 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.sun.javafx.tools.ant;
  27 
  28 import org.apache.tools.ant.BuildException;
  29 
  30 /**
  31  * Extension of standard ant FileSet type that provide means to
  32  * specify optional meta information on selected set of files.
  33  * This includes:
  34  *   <ul>
  35  *     <li> type or resource (JNLP, jar, etc)
  36  *     <li> OS and Architecture for which this resource is applicable
  37  *     <li> hint on when this resource is needed
  38  *          (helps to optimize loading order)
  39  *    </ul>
  40  *
  41  * Examples:
  42  * <pre>
  43  *        &lt;fx:fileset dir="dist" includes="app.jar"/&gt;
  44  * </pre>
  45  * Defines set consisting of single jar file (type will be detected based on extension)
  46  * that is applicable to all OS/arch combinations and needed for application startup.
  47  * <p>
  48  * <pre>
  49  *        &lt;fx:fileset dir="dist" neededFor="preloader" os="windows"&gt;
  50  *            &lt;include name="*.jar"/&gt;
  51  *        &lt;/fx:fileset&gt;
  52  * </pre>
  53  * All the jars in the "dist" folder for Windows platfrom only. These jars needed
  54  * to be available to launch preloader.
  55  *
  56  * @ant.type name="fileset" category="javafx"
  57  */
  58 public class FileSet extends org.apache.tools.ant.types.FileSet {
  59     //TODO: add support for locale & platform (see JNLP spec)
  60 
  61     //autoguess based on extension
  62     public final static int TYPE_AUTO = 0;
  63     public final static int TYPE_JAR = 1;
  64     public final static int TYPE_NATIVELIB = 2;
  65     public final static int TYPE_ICON = 3;
  66     public final static int TYPE_JNLP = 4;
  67 
  68     //these 2 types are only applicable to native bundles
  69     public final static int TYPE_DATA = 5;
  70     public final static int TYPE_LICENSE = 6;
  71 
  72     private int type = TYPE_AUTO;
  73     private String mode = "eager";
  74     private String os = null;
  75     private String arch = null;
  76 
  77     private FileSet get() {
  78         if (isReference()) {
  79             return (FileSet) getRefid().getReferencedObject();
  80         }
  81         return this;
  82     }
  83     //better use enum but then it need to be refactored ...
  84     final String[] types = {null, "jar", "nativelib", "icon", "jnlp", "data", "license"};
  85 
  86     public String getTypeAsString() {
  87         return types[get().type];
  88     }
  89 
  90     public String getOs() {
  91         return get().os;
  92     }
  93 
  94     public String getMode() {
  95         return get().mode;
  96     }
  97 
  98     public String getArch() {
  99         return get().arch;
 100     }
 101 
 102     /**
 103      * Type of the resources in the set. Supported types are "auto" for autodetect,
 104      * "jar", "jnlp", "native" for jar containing native libraries and "icon".
 105      *
 106      * @ant.not-required Default is to guess based on extension.
 107      */
 108     public void setType(String v) {
 109         if ("jar".equals(v)) {
 110             type = TYPE_JAR;
 111         } else if ("native".equals(v)) {
 112             type = TYPE_NATIVELIB;
 113         } else if ("icon".equals(v)) {
 114             type = TYPE_ICON;
 115         } else if ("jnlp".equals(v)) {
 116             type = TYPE_JNLP;
 117         } else if ("auto".equals(v)) {
 118             type = TYPE_AUTO;
 119         } else if ("data".equals(v)) {
 120             type = TYPE_DATA;
 121         } else if ("license".equals(v)) {
 122             type = TYPE_LICENSE;
 123         } else {
 124             throw new BuildException("Unsupported resource type [" + v + "].");
 125         }
 126     }
 127 
 128     /**
 129      * Defines when resources are needed (impacts loading priority).
 130      * Supported levels are:
 131      * <ul>
 132      *    <li> <em>preloader</em> - resources are needed to launch preloader
 133      *         (first thing to be executed)
 134      *    <li> <em>startup</em> - resources are needed for application startup.
 135      *    <li> <em>runtime</em> - resources are not required before application
 136      *       starts but may be needed at runtime.
 137      * </ul>
 138      *
 139      * @ant.not-required Default is "startup".
 140      */
 141     public void setRequiredFor(String v) {
 142         if ("preloader".equals(v)) {
 143             mode = "progress";
 144         } else if ("startup".equals(v)) {
 145             mode = "eager";
 146         } else if ("runtime".equals(v)) {
 147             mode = "lazy";
 148         } else {
 149             throw new BuildException("Unknown requiredFor value [" + v + "]");
 150         }
 151     }
 152 
 153     /**
 154      * Specifies the operating systems for which these resources should be considered.
 155      *
 156      * @ant.not-required Default is any.
 157      */
 158     public void setOs(String v) {
 159         os = v;
 160     }
 161 
 162     /**
 163      * Specifies the architecture for which these resources should be considered.
 164      *
 165      * @ant.not-required Default is any.
 166      */
 167     public void setArch(String v) {
 168         arch = v;
 169     }
 170 }