1 /*
   2  * Copyright (c) 2011, 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.sun.javafx.tools.ant;
  27 
  28 import java.util.LinkedList;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Properties;
  32 import com.sun.javafx.tools.packager.HtmlParam;
  33 import com.sun.javafx.tools.packager.Param;
  34 import org.apache.tools.ant.BuildException;
  35 import org.apache.tools.ant.types.DataType;
  36 import org.apache.tools.ant.types.Reference;
  37 
  38 /**
  39  * Basic application descriptor.
  40  * <p>
  41  * Defines main components of application and default set of parameters.
  42  *
  43  *
  44  * Examples:
  45  * <pre>
  46  *    &lt;info vendor="Uncle Joe" description="Test program"/&gt;
  47  * </pre>
  48  *
  49  * @ant.type name="application" category="javafx"
  50  */
  51 public class Application extends DataType implements Cloneable {
  52     String mainClass = null;
  53     String preloaderClass = null;
  54     String name = null;
  55     List<Param> parameters = new LinkedList<Param>();
  56     List<HtmlParam> htmlParameters = new LinkedList<HtmlParam>();
  57     public List<Argument> arguments = new LinkedList<Argument>();
  58     String fallbackApp = null;
  59     String id = null;
  60     boolean embeddedIntoSwing = false;
  61     String version = null;
  62     Boolean daemon = null;
  63 
  64     public List<Argument> addModule = new LinkedList<Argument>();
  65     public List<Argument> limitModule = new LinkedList<Argument>();
  66     String jdkModulePath;
  67     boolean detectModules;
  68 
  69     public void setVersion(String v) {
  70         version = v;
  71     }
  72 
  73     public void setToolkit(String v) {
  74         embeddedIntoSwing = "swing".equalsIgnoreCase(v);
  75     }
  76 
  77     /**
  78      * Main class of AWT-based applet to be used if application fail to launch
  79      * due to missing FX runtime and installation of JavaFX is not possible.
  80      *
  81      * @ant.not-required
  82      */
  83     public void setFallbackClass(String v) {
  84         fallbackApp = v;
  85     }
  86 
  87     public void setName(String v) {
  88         name = v;
  89     }
  90 
  91     public Param createParam() {
  92         Param p = new Param();
  93         parameters.add(p);
  94         return p;
  95     }
  96 
  97     public void setParams(Properties props) {
  98         if (props != null) {
  99             for (Map.Entry en : props.entrySet()) {
 100                 Param p = new Param();
 101                 p.setName((String)en.getKey());
 102                 p.setValue((String)en.getValue());
 103                 parameters.add(p);
 104             }
 105         }
 106     }
 107 
 108     public class Argument {
 109         String value;
 110 
 111         public void addText(String v) {
 112             value = getProject().replaceProperties(v);
 113         }
 114     }
 115 
 116     public Argument createArgument() {
 117         Argument a = new Argument();
 118         arguments.add(a);
 119         return a;
 120     }
 121 
 122     List<String> getArguments() {
 123         List<String> lst = new LinkedList();
 124         for(Argument a: arguments) {
 125             lst.add(a.value);
 126         }
 127         return lst;
 128     }
 129 
 130     public Object clone() {
 131         try {
 132             Application result = (Application) super.clone();
 133             return result;
 134         } catch (CloneNotSupportedException e) {
 135             throw new BuildException(e);
 136         }
 137     }
 138 
 139     public HtmlParam createHtmlParam() {
 140         HtmlParam p = new HtmlParam();
 141         htmlParameters.add(p);
 142         return p;
 143     }
 144 
 145     /**
 146      * Application id that can be used to obtain Javascript reference to the application in HTML.
 147      * Same id can be also used to refer to application object in the ant task (using refid).
 148      *
 149      * @ant.not-required
 150      */
 151     public void setId(String id) {
 152         this.id = id;
 153     }
 154 
 155     @Override
 156     public void setRefid(Reference id) {
 157         this.id = id.getRefId();
 158         super.setRefid(id);
 159     }
 160 
 161     /**
 162      * Main application class.
 163      *
 164      * @ant.required
 165      */
 166     public void setMainClass(String v) {
 167         mainClass = v;
 168     }
 169 
 170     /**
 171      * Preloader class to be used.
 172      *
 173      * @ant.not-required Default is preloader shipped in JavaFX Runtime.
 174      */
 175     public void setPreloaderClass(String v) {
 176         preloaderClass = v;
 177     }
 178 
 179     /**
 180      * Is this class a daemon/service?
 181      *
 182      * @ant.not-required Default is false, i.e. an interactive app
 183      */
 184     public void setDaemon(boolean b) {
 185         daemon = b;
 186     }
 187 
 188 
 189     /**
 190      * "addModule" declaration for the application's runtime.
 191      *
 192      * Modules can be specified per-element, or comma/colon/semi-colon/space separated
 193      *
 194      * @ant.not-required Default is to bundle the whole platform
 195      */
 196     public Argument createAddModule() {
 197         Argument a = new Argument();
 198         addModule.add(a);
 199         return a;
 200     }
 201 
 202     /**
 203      * "addModule" declaration for the application's runtime
 204      *
 205      * @ant.not-required Default is to bundle the whole platform
 206      */
 207     List<String> getAddModule() {
 208         List<String> lst = new LinkedList();
 209         for(Argument a: arguments) {
 210             for (String s : a.value.split("[:;,\\s]+")) {
 211                 lst.add(s);
 212             }
 213         }
 214         return lst;
 215     }
 216 
 217     /**
 218      * "limitModule" declaration for the application's runtime.
 219      *
 220      * Modules can be specified per-element, or comma/colon/semi-colon/space separated
 221      *
 222      * @ant.not-required Default is to bundle the whole platform
 223      */
 224     public Argument createLimitModule() {
 225         Argument a = new Argument();
 226         addModule.add(a);
 227         return a;
 228     }
 229 
 230     /**
 231      * "limitModule" declaration for the application's runtime
 232      *
 233      * @ant.not-required Default is to bundle the whole platform
 234      */
 235     List<String> getLimitModule() {
 236         List<String> lst = new LinkedList();
 237         for(Argument a: arguments) {
 238             for (String s : a.value.split("[:;,\\s]+")) {
 239                 lst.add(s);
 240             }
 241         }
 242         return lst;
 243     }
 244 
 245     /**
 246      * Whether or not the bundler should attempt to detect and add used modules
 247      */
 248     public boolean getDetectModules() {
 249         return detectModules;
 250     }
 251 
 252     /**
 253      * Whether or not the bundler should attempt to detect and add used modules
 254      * @ant.not-required default is false
 255      */
 256     public void setDetectModules(boolean Value) {
 257         this.detectModules = Value;
 258     }
 259 
 260     /**
 261      * Module path within the running applicaiton
 262      */
 263     public String getJdkModulePath() {
 264         return jdkModulePath;
 265     }
 266 
 267     /**
 268      * Module path within the running applicaiton
 269      *
 270      * @ant.not-required default is $PACKAGEPATH/modules
 271      */
 272     public void setJdkModulePath(String Value) {
 273         this.jdkModulePath = Value;
 274     }
 275 
 276     //return instance that actually has data. Could be referenced object ...
 277     public Application get() {
 278         return isReference() ?
 279                 (Application) getRefid().getReferencedObject() : this;
 280     }
 281 
 282     public void selfcheck() {
 283         if (get().mainClass == null) {
 284             throw new BuildException("Application main class is required.");
 285         }
 286     }
 287 
 288     @Override
 289     public String toString() {
 290         return "Application[id="+id+", mainClass="+mainClass+"]";
 291     }
 292 }