KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Exit


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.Task;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.ExitStatusException;
25 import org.apache.tools.ant.taskdefs.condition.Condition;
26 import org.apache.tools.ant.taskdefs.condition.ConditionBase;
27
28 /**
29  * Exits the active build, giving an additional message
30  * if available.
31  *
32  * The <code>if</code> and <code>unless</code> attributes make the
33  * failure conditional -both probe for the named property being defined.
34  * The <code>if</code> tests for the property being defined, the
35  * <code>unless</code> for a property being undefined.
36  *
37  * If both attributes are set, then the test fails only if both tests
38  * are true. i.e.
39  * <pre>fail := defined(ifProperty) && !defined(unlessProperty)</pre>
40  *
41  * A single nested<code>&lt;condition&gt;</code> element can be specified
42  * instead of using <code>if</code>/<code>unless</code> (a combined
43  * effect can be achieved using <code>isset</code> conditions).
44  *
45  * @since Ant 1.2
46  *
47  * @ant.task name="fail" category="control"
48  */

49 public class Exit extends Task {
50
51     private static class NestedCondition extends ConditionBase implements Condition {
52         public boolean eval() {
53             if (countConditions() != 1) {
54                 throw new BuildException(
55                     "A single nested condition is required.");
56             }
57             return ((Condition) (getConditions().nextElement())).eval();
58         }
59     }
60
61     private String JavaDoc message;
62     private String JavaDoc ifCondition, unlessCondition;
63     private NestedCondition nestedCondition;
64     private Integer JavaDoc status;
65
66     /**
67      * A message giving further information on why the build exited.
68      *
69      * @param value message to output
70      */

71     public void setMessage(String JavaDoc value) {
72         this.message = value;
73     }
74
75     /**
76      * Only fail if a property of the given name exists in the current project.
77      * @param c property name
78      */

79     public void setIf(String JavaDoc c) {
80         ifCondition = c;
81     }
82
83     /**
84      * Only fail if a property of the given name does not
85      * exist in the current project.
86      * @param c property name
87      */

88     public void setUnless(String JavaDoc c) {
89         unlessCondition = c;
90     }
91
92     /**
93      * Set the status code to associate with the thrown Exception.
94      * @param i the <code>int</code> status
95      */

96     public void setStatus(int i) {
97         status = new Integer JavaDoc(i);
98     }
99
100     /**
101      * Throw a <code>BuildException</code> to exit (fail) the build.
102      * If specified, evaluate conditions:
103      * A single nested condition is accepted, but requires that the
104      * <code>if</code>/<code>unless</code> attributes be omitted.
105      * If the nested condition evaluates to true, or the
106      * ifCondition is true or unlessCondition is false, the build will exit.
107      * The error message is constructed from the text fields, from
108      * the nested condition (if specified), or finally from
109      * the if and unless parameters (if present).
110      * @throws BuildException on error
111      */

112     public void execute() throws BuildException {
113         boolean fail = (nestedConditionPresent()) ? testNestedCondition()
114                      : (testIfCondition() && testUnlessCondition());
115         if (fail) {
116             String JavaDoc text = null;
117             if (message != null && message.trim().length() > 0) {
118                 text = message.trim();
119             } else {
120                 if (ifCondition != null && ifCondition.length() > 0
121                     && getProject().getProperty(ifCondition) != null) {
122                     text = "if=" + ifCondition;
123                 }
124                 if (unlessCondition != null && unlessCondition.length() > 0
125                     && getProject().getProperty(unlessCondition) == null) {
126                     if (text == null) {
127                         text = "";
128                     } else {
129                         text += " and ";
130                     }
131                     text += "unless=" + unlessCondition;
132                 }
133                 if (nestedConditionPresent()) {
134                     text = "condition satisfied";
135                 } else {
136                     if (text == null) {
137                         text = "No message";
138                     }
139                 }
140             }
141             log("failing due to " + text, Project.MSG_DEBUG);
142             throw ((status == null) ? new BuildException(text)
143              : new ExitStatusException(text, status.intValue()));
144         }
145     }
146
147     /**
148      * Set a multiline message.
149      * @param msg the message to display
150      */

151     public void addText(String JavaDoc msg) {
152         if (message == null) {
153             message = "";
154         }
155         message += getProject().replaceProperties(msg);
156     }
157
158     /**
159      * Add a condition element.
160      * @return <code>ConditionBase</code>.
161      * @since Ant 1.6.2
162      */

163     public ConditionBase createCondition() {
164         if (nestedCondition != null) {
165             throw new BuildException("Only one nested condition is allowed.");
166         }
167         nestedCondition = new NestedCondition();
168         return nestedCondition;
169     }
170
171     /**
172      * test the if condition
173      * @return true if there is no if condition, or the named property exists
174      */

175     private boolean testIfCondition() {
176         if (ifCondition == null || "".equals(ifCondition)) {
177             return true;
178         }
179         return getProject().getProperty(ifCondition) != null;
180     }
181
182     /**
183      * test the unless condition
184      * @return true if there is no unless condition,
185      * or there is a named property but it doesn't exist
186      */

187     private boolean testUnlessCondition() {
188         if (unlessCondition == null || "".equals(unlessCondition)) {
189             return true;
190         }
191         return getProject().getProperty(unlessCondition) == null;
192     }
193
194     /**
195      * test the nested condition
196      * @return true if there is none, or it evaluates to true
197      */

198     private boolean testNestedCondition() {
199         boolean result = nestedConditionPresent();
200
201         if (result && ifCondition != null || unlessCondition != null) {
202             throw new BuildException("Nested conditions "
203                 + "not permitted in conjunction with if/unless attributes");
204         }
205
206         return result && nestedCondition.eval();
207     }
208
209     /**
210      * test whether there is a nested condition.
211      * @return <code>boolean</code>.
212      */

213     private boolean nestedConditionPresent() {
214         return (nestedCondition != null);
215     }
216
217 }
218
Popular Tags