1 /*
   2  * Copyright (c) 2016, 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 package com.sun.javafx.tools.ant;
  26 
  27 import java.io.File;
  28 import java.util.LinkedList;
  29 import java.util.List;
  30 import org.apache.tools.ant.BuildException;
  31 import org.apache.tools.ant.types.DataType;
  32 import org.apache.tools.ant.DynamicElement;
  33 import org.apache.tools.ant.DynamicAttribute;
  34 
  35 
  36 public class Runtime extends DataType implements Cloneable, DynamicElement, DynamicAttribute {
  37     private List<Argument> addModules = new LinkedList<>();
  38     private List<Argument> limitModules = new LinkedList<>();
  39     private List<Argument> modulePath = new LinkedList<>();
  40     private Boolean stripNativeCommands;
  41     private Boolean detectModules;
  42 
  43     public class Argument {
  44         private String value;
  45 
  46         public void setValue(String value) {
  47             this.value = value;
  48         }
  49 
  50         public String getValue() {
  51             return this.value;
  52         }
  53     }
  54 
  55     private List<String> processArguments(List<Argument> args, String pattern) {
  56         List<String> result = new LinkedList();
  57 
  58         for (Argument a: args) {
  59             for (String s : a.getValue().split(pattern)) {
  60                 result.add(s);
  61             }
  62         }
  63 
  64         return result;
  65     }
  66 
  67     /**
  68      * "addMod" declaration for the application's runtime.
  69      *
  70      * Modules can be specified per-element, or comma/colon/semi-colon/space separated
  71      *
  72      * @ant.not-required Default is to bundle the whole platform
  73      */
  74     private Argument createAddModules() {
  75         Argument a = new Argument();
  76         this.addModules.add(a);
  77         return a;
  78     }
  79 
  80     /**
  81      * "addMod" declaration for the application's runtime
  82      *
  83      * @ant.not-required Default is to bundle the whole platform
  84      */
  85     List<String> getAddModules() {
  86         return processArguments(this.addModules, "[,;: ]+");
  87     }
  88 
  89     /**
  90      * "limitMod" declaration for the application's runtime.
  91      *
  92      * Modules can be specified per-element, or comma/colon/semi-colon/space separated
  93      *
  94      * @ant.not-required Default is to bundle the whole platform
  95      */
  96     private Argument createLimitModules() {
  97         Argument a = new Argument();
  98         this.limitModules.add(a);
  99         return a;
 100     }
 101 
 102     /**
 103      * "limitMod" declaration for the application's runtime
 104      *
 105      * @ant.not-required Default is to bundle the whole platform
 106      */
 107     List<String> getLimitModules() {
 108         return processArguments(this.limitModules, "[,;: ]+");
 109     }
 110 
 111     /**
 112      * "modulePath" declaration for the application's runtime.
 113      *
 114      * Modules can be specified per-element, or colon/semi-colon separated
 115      *
 116      * @ant.not-required Default is to bundle the whole platform
 117      */
 118     private Argument createModulePath() {
 119         Argument a = new Argument();
 120         this.modulePath.add(a);
 121         return a;
 122     }
 123 
 124     /**
 125      *
 126      */
 127     public String getModulePath() {
 128         String result = "";
 129         List<String> paths = processArguments(this.modulePath, "[;:]+");
 130 
 131         for (String s : paths) {
 132             if (!result.isEmpty()) {
 133                 result += File.pathSeparator;
 134             }
 135 
 136             result += s;
 137         }
 138 
 139         return result;
 140     }
 141 
 142     /**
 143      * Whether or not the bundler should remove native commands.
 144      */
 145     public Boolean getStripNativeCommands() {
 146         return this.stripNativeCommands;
 147     }
 148 
 149     /**
 150      * Whether or not the bundler should remove native commands.
 151      * @ant.not-required default is true
 152      */
 153     public void setStripNativeCommands(boolean value) {
 154         this.stripNativeCommands = value;
 155     }
 156 
 157     /**
 158      * Whether or not the bundler should attempt to detect and add used modules.
 159      */
 160     public Boolean getDetectModules() {
 161         return this.detectModules;
 162     }
 163 
 164     /**
 165      * Whether or not the bundler should attempt to detect and add used modules.
 166      * @ant.not-required default is false. This is experimental.
 167      */
 168     public void setDetectModules(boolean value) {
 169         this.detectModules = value;
 170     }
 171 
 172     public Object clone() {
 173         try {
 174             Application result = (Application) super.clone();
 175             return result;
 176         } catch (CloneNotSupportedException e) {
 177             throw new BuildException(e);
 178         }
 179     }
 180 
 181     public Runtime get() {
 182         return isReference() ?
 183                 (Runtime) getRefid().getReferencedObject() : this;
 184     }
 185 
 186     @Override
 187     public Object createDynamicElement(String name) {
 188         if (name.equals("add-modules")) {
 189             return createAddModules();
 190         }
 191         else if (name.equals("limit-modules")) {
 192             return createLimitModules();
 193         }
 194         else if (name.equals("module-path")) {
 195             return createModulePath();
 196         }
 197 
 198         return null;
 199     }
 200 
 201     public void setDynamicAttribute(String name, String value) {
 202         if (name.equals("strip-native-commands")) {
 203             this.stripNativeCommands = Boolean.valueOf(value);
 204         }
 205         else if (name.equals("detect-modules")) {
 206             this.detectModules = Boolean.valueOf(value);
 207         }
 208     }
 209 }