1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.Task;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.ExitStatusException;
|
---|
23 | import org.apache.tools.ant.taskdefs.condition.Condition;
|
---|
24 | import org.apache.tools.ant.taskdefs.condition.ConditionBase;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Exits the active build, giving an additional message
|
---|
28 | * if available.
|
---|
29 | *
|
---|
30 | * The <code>if</code> and <code>unless</code> attributes make the
|
---|
31 | * failure conditional -both probe for the named property being defined.
|
---|
32 | * The <code>if</code> tests for the property being defined, the
|
---|
33 | * <code>unless</code> for a property being undefined.
|
---|
34 | *
|
---|
35 | * If both attributes are set, then the test fails only if both tests
|
---|
36 | * are true. i.e.
|
---|
37 | * <pre>fail := defined(ifProperty) && !defined(unlessProperty)</pre>
|
---|
38 | *
|
---|
39 | * A single nested<CODE><condition></CODE> element can be specified
|
---|
40 | * instead of using <CODE>if</CODE>/<CODE>unless</CODE> (a combined
|
---|
41 | * effect can be achieved using <CODE>isset</CODE> conditions).
|
---|
42 | *
|
---|
43 | * @since Ant 1.2
|
---|
44 | *
|
---|
45 | * @ant.task name="fail" category="control"
|
---|
46 | */
|
---|
47 | public class Exit extends Task {
|
---|
48 |
|
---|
49 | private class NestedCondition extends ConditionBase implements Condition {
|
---|
50 | public boolean eval() {
|
---|
51 | if (countConditions() != 1) {
|
---|
52 | throw new BuildException(
|
---|
53 | "A single nested condition is required.");
|
---|
54 | }
|
---|
55 | return ((Condition)(getConditions().nextElement())).eval();
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private String message;
|
---|
60 | private String ifCondition, unlessCondition;
|
---|
61 | private NestedCondition nestedCondition;
|
---|
62 | private Integer status;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * A message giving further information on why the build exited.
|
---|
66 | *
|
---|
67 | * @param value message to output
|
---|
68 | */
|
---|
69 | public void setMessage(String value) {
|
---|
70 | this.message = value;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Only fail if a property of the given name exists in the current project.
|
---|
75 | * @param c property name
|
---|
76 | */
|
---|
77 | public void setIf(String c) {
|
---|
78 | ifCondition = c;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Only fail if a property of the given name does not
|
---|
83 | * exist in the current project.
|
---|
84 | * @param c property name
|
---|
85 | */
|
---|
86 | public void setUnless(String c) {
|
---|
87 | unlessCondition = c;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Set the status code to associate with the thrown Exception.
|
---|
92 | * @param i the <CODE>int</CODE> status
|
---|
93 | */
|
---|
94 | public void setStatus(int i) {
|
---|
95 | status = new Integer(i);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Throw a <CODE>BuildException</CODE> to exit (fail) the build.
|
---|
100 | * If specified, evaluate conditions:
|
---|
101 | * A single nested condition is accepted, but requires that the
|
---|
102 | * <CODE>if</CODE>/<code>unless</code> attributes be omitted.
|
---|
103 | * If the nested condition evaluates to true, or the
|
---|
104 | * ifCondition is true or unlessCondition is false, the build will exit.
|
---|
105 | * The error message is constructed from the text fields, from
|
---|
106 | * the nested condition (if specified), or finally from
|
---|
107 | * the if and unless parameters (if present).
|
---|
108 | * @throws BuildException on error
|
---|
109 | */
|
---|
110 | public void execute() throws BuildException {
|
---|
111 | boolean fail = (nestedConditionPresent()) ? testNestedCondition()
|
---|
112 | : (testIfCondition() && testUnlessCondition());
|
---|
113 | if (fail) {
|
---|
114 | String text = null;
|
---|
115 | if (message != null && message.trim().length() > 0) {
|
---|
116 | text = message.trim();
|
---|
117 | } else {
|
---|
118 | if (ifCondition != null && ifCondition.length() > 0
|
---|
119 | && getProject().getProperty(ifCondition) != null) {
|
---|
120 | text = "if=" + ifCondition;
|
---|
121 | }
|
---|
122 | if (unlessCondition != null && unlessCondition.length() > 0
|
---|
123 | && getProject().getProperty(unlessCondition) == null) {
|
---|
124 | if (text == null) {
|
---|
125 | text = "";
|
---|
126 | } else {
|
---|
127 | text += " and ";
|
---|
128 | }
|
---|
129 | text += "unless=" + unlessCondition;
|
---|
130 | }
|
---|
131 | if (nestedConditionPresent()) {
|
---|
132 | text = "condition satisfied";
|
---|
133 | } else {
|
---|
134 | if (text == null) {
|
---|
135 | text = "No message";
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | throw ((status == null) ? new BuildException(text)
|
---|
140 | : new ExitStatusException(text, status.intValue()));
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Set a multiline message.
|
---|
146 | * @param msg the message to display
|
---|
147 | */
|
---|
148 | public void addText(String msg) {
|
---|
149 | if (message == null) {
|
---|
150 | message = "";
|
---|
151 | }
|
---|
152 | message += getProject().replaceProperties(msg);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Add a condition element.
|
---|
157 | * @return <CODE>ConditionBase</CODE>.
|
---|
158 | * @since Ant 1.6.2
|
---|
159 | */
|
---|
160 | public ConditionBase createCondition() {
|
---|
161 | if (nestedCondition != null) {
|
---|
162 | throw new BuildException("Only one nested condition is allowed.");
|
---|
163 | }
|
---|
164 | nestedCondition = new NestedCondition();
|
---|
165 | return nestedCondition;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * test the if condition
|
---|
170 | * @return true if there is no if condition, or the named property exists
|
---|
171 | */
|
---|
172 | private boolean testIfCondition() {
|
---|
173 | if (ifCondition == null || "".equals(ifCondition)) {
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 | return getProject().getProperty(ifCondition) != null;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * test the unless condition
|
---|
181 | * @return true if there is no unless condition,
|
---|
182 | * or there is a named property but it doesn't exist
|
---|
183 | */
|
---|
184 | private boolean testUnlessCondition() {
|
---|
185 | if (unlessCondition == null || "".equals(unlessCondition)) {
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 | return getProject().getProperty(unlessCondition) == null;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * test the nested condition
|
---|
193 | * @return true if there is none, or it evaluates to true
|
---|
194 | */
|
---|
195 | private boolean testNestedCondition() {
|
---|
196 | boolean result = nestedConditionPresent();
|
---|
197 |
|
---|
198 | if (result && ifCondition != null || unlessCondition != null) {
|
---|
199 | throw new BuildException("Nested conditions "
|
---|
200 | + "not permitted in conjunction with if/unless attributes");
|
---|
201 | }
|
---|
202 |
|
---|
203 | return result && nestedCondition.eval();
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * test whether there is a nested condition.
|
---|
208 | * @return <CODE>boolean</CODE>.
|
---|
209 | */
|
---|
210 | private boolean nestedConditionPresent() {
|
---|
211 | return (nestedCondition != null);
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|