001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.ListIterator;
028:
029: /**
030: * The assertion datatype. This type describes
031: * assertion settings for the <java> task and others.
032: * One can set the system assertions, and enable/disable those in
033: * packages and classes.
034: * Assertions can only be enabled or disabled when forking Java.
035: *
036: * Example: set system assertions and all org.apache packages except
037: * for ant, and the class org.apache.tools.ant.Main.
038: * <pre>
039: * <assertions enableSystemAssertions="true" >
040: * <enable package="org.apache" />
041: * <disable package="org.apache.ant" />
042: * <enable class="org.apache.tools.ant.Main"/>
043: * </assertions>
044: *</pre>
045: * Disable system assertions; enable those in the anonymous package
046: * <pre>
047: * <assertions enableSystemAssertions="false" >
048: * <enable package="..." />
049: * </assertions>
050: * </pre>
051: * enable assertions in a class called Test
052: * <pre>
053: * <assertions >
054: * <enable class="Test" />
055: * </assertions>
056: * </pre>
057: * This type is a datatype, so you can declare assertions and use them later
058: *
059: * <pre>
060: * <assertions id="project.assertions" >
061: * <enable project="org.apache.test" />
062: * </assertions>
063: *
064: * <assertions refid="project.assertions" />
065: *
066: * </pre>
067: * @since Ant 1.6
068: */
069: public class Assertions extends DataType implements Cloneable {
070:
071: /**
072: * enable/disable sys assertions; null means undefined
073: */
074: private Boolean enableSystemAssertions;
075:
076: /**
077: * list of type BaseAssertion
078: */
079: private ArrayList assertionList = new ArrayList();
080:
081: /**
082: * enable assertions
083: * @param assertion an enable assertion nested element
084: */
085: public void addEnable(EnabledAssertion assertion) {
086: checkChildrenAllowed();
087: assertionList.add(assertion);
088: }
089:
090: /**
091: * disable assertions
092: * @param assertion a disable assertion nested element
093: */
094: public void addDisable(DisabledAssertion assertion) {
095: checkChildrenAllowed();
096: assertionList.add(assertion);
097: }
098:
099: /**
100: * enable or disable system assertions.
101: * Default is not set (neither -enablesystemassersions or -disablesytemassertions
102: * are used on the command line).
103: * @param enableSystemAssertions if true enable system assertions
104: */
105: public void setEnableSystemAssertions(Boolean enableSystemAssertions) {
106: checkAttributesAllowed();
107: this .enableSystemAssertions = enableSystemAssertions;
108: }
109:
110: /**
111: * Set the value of the refid attribute.
112: *
113: * <p>Subclasses may need to check whether any other attributes
114: * have been set as well or child elements have been created and
115: * thus override this method. if they do the must call
116: * <code>super.setRefid</code>.</p>
117: * @param ref the reference to use
118: */
119: public void setRefid(Reference ref) {
120: if (assertionList.size() > 0 || enableSystemAssertions != null) {
121: throw tooManyAttributes();
122: }
123: super .setRefid(ref);
124: }
125:
126: /**
127: * get whatever we are referencing to. This could be ourself.
128: * @return the object that contains the assertion info
129: */
130: private Assertions getFinalReference() {
131: if (getRefid() == null) {
132: return this ;
133: } else {
134: Object o = getRefid().getReferencedObject(getProject());
135: if (!(o instanceof Assertions)) {
136: throw new BuildException("reference is of wrong type");
137: }
138: return (Assertions) o;
139: }
140: }
141:
142: /**
143: * how many assertions are made...will resolve references before returning
144: * @return total # of commands to make
145: */
146: public int size() {
147: Assertions clause = getFinalReference();
148: return clause.getFinalSize();
149: }
150:
151: /**
152: * what is the final size of this object
153: * @return
154: */
155: private int getFinalSize() {
156: return assertionList.size()
157: + (enableSystemAssertions != null ? 1 : 0);
158: }
159:
160: /**
161: * add the assertions to a list in a format suitable
162: * for adding to a command line
163: * @param commandList the command line to format
164: */
165: public void applyAssertions(List commandList) {
166: getProject().log("Applying assertions", Project.MSG_DEBUG);
167: Assertions clause = getFinalReference();
168: //do the system assertions
169: if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
170: getProject().log("Enabling system assertions",
171: Project.MSG_DEBUG);
172: commandList.add("-enablesystemassertions");
173: } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
174: getProject().log("disabling system assertions",
175: Project.MSG_DEBUG);
176: commandList.add("-disablesystemassertions");
177: }
178:
179: //now any inner assertions
180: Iterator it = clause.assertionList.iterator();
181: while (it.hasNext()) {
182: BaseAssertion assertion = (BaseAssertion) it.next();
183: String arg = assertion.toCommand();
184: getProject().log("adding assertion " + arg,
185: Project.MSG_DEBUG);
186: commandList.add(arg);
187: }
188: }
189:
190: /**
191: * apply all the assertions to the command.
192: * @param command the command line to format
193: */
194: public void applyAssertions(CommandlineJava command) {
195: Assertions clause = getFinalReference();
196: //do the system assertions
197: if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
198: addVmArgument(command, "-enablesystemassertions");
199: } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
200: addVmArgument(command, "-disablesystemassertions");
201: }
202:
203: //now any inner assertions
204: Iterator it = clause.assertionList.iterator();
205: while (it.hasNext()) {
206: BaseAssertion assertion = (BaseAssertion) it.next();
207: String arg = assertion.toCommand();
208: addVmArgument(command, arg);
209: }
210: }
211:
212: /**
213: * add the assertions to a list in a format suitable
214: * for adding to a command line
215: * @param commandIterator list of commands
216: */
217: public void applyAssertions(final ListIterator commandIterator) {
218: getProject().log("Applying assertions", Project.MSG_DEBUG);
219: Assertions clause = getFinalReference();
220: //do the system assertions
221: if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
222: getProject().log("Enabling system assertions",
223: Project.MSG_DEBUG);
224: commandIterator.add("-enablesystemassertions");
225: } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
226: getProject().log("disabling system assertions",
227: Project.MSG_DEBUG);
228: commandIterator.add("-disablesystemassertions");
229: }
230:
231: //now any inner assertions
232: Iterator it = clause.assertionList.iterator();
233: while (it.hasNext()) {
234: BaseAssertion assertion = (BaseAssertion) it.next();
235: String arg = assertion.toCommand();
236: getProject().log("adding assertion " + arg,
237: Project.MSG_DEBUG);
238: commandIterator.add(arg);
239: }
240: }
241:
242: /**
243: * helper method to add a string JVM argument to a command
244: * @param command
245: * @param arg
246: */
247: private static void addVmArgument(CommandlineJava command,
248: String arg) {
249: Commandline.Argument argument;
250: argument = command.createVmArgument();
251: argument.setValue(arg);
252: }
253:
254: /**
255: * clone the objects.
256: * This is not a full depth clone; the list of assertions is cloned,
257: * but it does not clone the underlying assertions.
258: * @return a cli
259: * @throws CloneNotSupportedException if the super class does not support cloning
260: */
261: public Object clone() throws CloneNotSupportedException {
262: Assertions that = (Assertions) super .clone();
263: that.assertionList = (ArrayList) assertionList.clone();
264: return that;
265: }
266:
267: /**
268: * base class for our assertion elements.
269: */
270:
271: public abstract static class BaseAssertion {
272: private String packageName;
273: private String className;
274:
275: /**
276: * name a class
277: * @param className a class name
278: */
279: public void setClass(String className) {
280: this .className = className;
281: }
282:
283: /**
284: * name a package
285: * @param packageName a package name
286: */
287: public void setPackage(String packageName) {
288: this .packageName = packageName;
289: }
290:
291: /**
292: * what is the class name?
293: * @return classname or null
294: * @see #setClass
295: */
296: protected String getClassName() {
297: return className;
298: }
299:
300: /**
301: * what is the package name?
302: * @return package name or null
303: * @see #setPackage
304: */
305: protected String getPackageName() {
306: return packageName;
307: }
308:
309: /**
310: * get the prefix used to begin the command; -ea or -da.
311: * @return prefix
312: */
313: public abstract String getCommandPrefix();
314:
315: /**
316: * create a full command string from this class
317: * @throws BuildException in case of trouble
318: * @return The command string
319: */
320: public String toCommand() {
321: //catch invalidness
322: if (getPackageName() != null && getClassName() != null) {
323: throw new BuildException(
324: "Both package and class have been set");
325: }
326: StringBuffer command = new StringBuffer(getCommandPrefix());
327: //see if it is a package or a class
328: if (getPackageName() != null) {
329: //packages get a ... prefix
330: command.append(':');
331: command.append(getPackageName());
332: if (!command.toString().endsWith("...")) {
333: //append the ... suffix if not there already
334: command.append("...");
335: }
336: } else if (getClassName() != null) {
337: //classes just get the classname
338: command.append(':');
339: command.append(getClassName());
340: }
341: return command.toString();
342: }
343: }
344:
345: /**
346: * an enabled assertion enables things
347: */
348: public static class EnabledAssertion extends BaseAssertion {
349: /**
350: * get the prefix used to begin the command; -ea or -da.
351: * @return prefix
352: */
353: public String getCommandPrefix() {
354: return "-ea";
355: }
356:
357: }
358:
359: /**
360: * A disabled assertion disables things
361: */
362: public static class DisabledAssertion extends BaseAssertion {
363: /**
364: * get the prefix used to begin the command; -ea or -da.
365: * @return prefix
366: */
367: public String getCommandPrefix() {
368: return "-da";
369: }
370:
371: }
372: }
|