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 | import org.apache.tools.ant.taskdefs.*;
|
---|
19 | import java.io.IOException;
|
---|
20 |
|
---|
21 | import org.apache.tools.ant.Task;
|
---|
22 | import org.apache.tools.ant.Target;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.types.PropertySet;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Call another target in the same project.
|
---|
28 | *
|
---|
29 | * <pre>
|
---|
30 | * <target name="foo">
|
---|
31 | * <antcall target="bar">
|
---|
32 | * <param name="property1" value="aaaaa" />
|
---|
33 | * <param name="foo" value="baz" />
|
---|
34 | * </antcall>
|
---|
35 | * </target>
|
---|
36 | *
|
---|
37 | * <target name="bar" depends="init">
|
---|
38 | * <echo message="prop is ${property1} ${foo}" />
|
---|
39 | * </target>
|
---|
40 | * </pre>
|
---|
41 | *
|
---|
42 | * <p>This only works as expected if neither property1 nor foo are
|
---|
43 | * defined in the project itself.
|
---|
44 | *
|
---|
45 | *
|
---|
46 | * @since Ant 1.2
|
---|
47 | *
|
---|
48 | * @ant.task name="antcall" category="control"
|
---|
49 | */
|
---|
50 | public class TreeCallTarget extends Task {
|
---|
51 |
|
---|
52 | private Ant callee;
|
---|
53 | // must match the default value of Ant#inheritAll
|
---|
54 | private boolean inheritAll = true;
|
---|
55 | // must match the default value of Ant#inheritRefs
|
---|
56 | private boolean inheritRefs = false;
|
---|
57 |
|
---|
58 | private boolean targetSet = false;
|
---|
59 | private String theTarget = null;
|
---|
60 | private String address = null;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * If true, pass all properties to the new Ant project.
|
---|
64 | * Defaults to true.
|
---|
65 | * @param inherit <code>boolean</code> flag.
|
---|
66 | */
|
---|
67 | public void setInheritAll(boolean inherit) {
|
---|
68 | inheritAll = inherit;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * If true, pass all references to the new Ant project.
|
---|
73 | * Defaults to false.
|
---|
74 | * @param inheritRefs <code>boolean</code> flag.
|
---|
75 | */
|
---|
76 | public void setInheritRefs(boolean inheritRefs) {
|
---|
77 | this.inheritRefs = inheritRefs;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Initialize this task by creating new instance of the ant task and
|
---|
82 | * configuring it by calling its own init method.
|
---|
83 | */
|
---|
84 | public void init() {
|
---|
85 | callee = (Ant) getProject().createTask("ant");
|
---|
86 | callee.setOwningTarget(getOwningTarget());
|
---|
87 | callee.setTaskName(getTaskName());
|
---|
88 | callee.setLocation(getLocation());
|
---|
89 | callee.init();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Delegate the work to the ant task instance, after setting it up.
|
---|
94 | * @throws BuildEximport org.apache.tools.ant.Task;ception on validation failure or if the target didn't
|
---|
95 | * execute.
|
---|
96 | */
|
---|
97 | public void execute() throws BuildException {
|
---|
98 | if (callee == null) {
|
---|
99 | init();
|
---|
100 | }
|
---|
101 | if (!targetSet) {
|
---|
102 | throw new BuildException(
|
---|
103 | "Attribute target or at least one nested target is required.",
|
---|
104 | getLocation());
|
---|
105 | }
|
---|
106 |
|
---|
107 | //figure out what the address of this instance should be
|
---|
108 | String callingAddress = this.getOwningTarget().getAddress();
|
---|
109 | int subAddress = this.getOwningTarget().getNextSubAddress();
|
---|
110 | this.getOwningTarget().incrementNextSubAddress();
|
---|
111 |
|
---|
112 | if ( callingAddress == null ) {
|
---|
113 | address = "" + subAddress;
|
---|
114 | } else {
|
---|
115 | address = callingAddress + "." + subAddress;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | //check that the target should be run given address,resume.mode, rusume.from and resume.to
|
---|
120 | String resumeFrom = this.getProject().getProperty("resume.from");
|
---|
121 | String resumeTo = this.getProject().getProperty("resume.to");
|
---|
122 | String resumeMode = this.getProject().getProperty("resume.mode");
|
---|
123 |
|
---|
124 | boolean shouldExecOnFrom = false, shouldExecOnTo = false;
|
---|
125 |
|
---|
126 | //given resumeFrom, might we execute?
|
---|
127 | if ( resumeFrom == null ) {
|
---|
128 | shouldExecOnFrom = true;
|
---|
129 |
|
---|
130 | } else {
|
---|
131 |
|
---|
132 | //if the resumeFrom is a descendent of this, we must execute it no matter the Mode
|
---|
133 | if ( isDescendantOf(resumeFrom,address) ) {
|
---|
134 | shouldExecOnFrom = true;
|
---|
135 |
|
---|
136 | } else {
|
---|
137 |
|
---|
138 | if ( resumeMode == null ) {
|
---|
139 | //fallthrough mode
|
---|
140 | if ( isAfter(address,resumeFrom) ) {
|
---|
141 | shouldExecOnFrom = true;
|
---|
142 | } else {
|
---|
143 | shouldExecOnFrom = false;
|
---|
144 | }
|
---|
145 |
|
---|
146 | } else if ( resumeMode.equals("descend") ) {
|
---|
147 | //descend mode
|
---|
148 | if ( isDescendantOf(address,resumeFrom) ) {
|
---|
149 | shouldExecOnFrom = true;
|
---|
150 | } else {
|
---|
151 | shouldExecOnFrom = false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | } else {
|
---|
155 | throw new BuildException(
|
---|
156 | "Only 'fallthrough' and 'descend' resume-modes are supported.",
|
---|
157 | getLocation()
|
---|
158 | );
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | //given resumeTo, might we execute?
|
---|
166 | if ( resumeTo == null ) {
|
---|
167 | shouldExecOnTo = true;
|
---|
168 | } else {
|
---|
169 | if ( !isAfter(address,resumeTo) || isEqualTo(address,resumeTo) ) {
|
---|
170 | shouldExecOnTo = true;
|
---|
171 | } else {
|
---|
172 | shouldExecOnTo = false;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | //check that both are true
|
---|
177 | boolean execute = ( shouldExecOnFrom && shouldExecOnTo );
|
---|
178 |
|
---|
179 | if ( execute ) {
|
---|
180 | callee.setAntfile(getProject().getProperty("ant.file"));
|
---|
181 | callee.setInheritAll(inheritAll);
|
---|
182 | callee.setInheritRefs(inheritRefs);
|
---|
183 | Target calleeTarget = (Target)callee.getProject().getTargets().get(theTarget);
|
---|
184 | calleeTarget.setAddress(address);
|
---|
185 |
|
---|
186 | if( false ) {
|
---|
187 | throw new BuildException(
|
---|
188 | "\n theTarget " + theTarget + "\n calleeTarget: " + calleeTarget + "\n address: " + calleeTarget.getAddress(),
|
---|
189 | getLocation()
|
---|
190 | );
|
---|
191 | }
|
---|
192 | callee.execute(theTarget,address);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Create a new Property to pass to the invoked target(s).
|
---|
198 | * @return a <code>Property</code> object.
|
---|
199 | */
|
---|
200 | public Property createParam() {
|
---|
201 | if (callee == null) {
|
---|
202 | init();
|
---|
203 | }
|
---|
204 | return callee.createProperty();
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Reference element identifying a data type to carry
|
---|
209 | * over to the invoked target.
|
---|
210 | * @param r the specified <code>Ant.Reference</code>.
|
---|
211 | * @since Ant 1.5
|
---|
212 | */
|
---|
213 | public void addReference(Ant.Reference r) {
|
---|
214 | if (callee == null) {
|
---|
215 | init();
|
---|
216 | }
|
---|
217 | callee.addReference(r);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Set of properties to pass to the new project.
|
---|
222 | * @param ps the <code>PropertySet</code> to pass.
|
---|
223 | * @since Ant 1.6
|
---|
224 | */
|
---|
225 | public void addPropertyset(PropertySet ps) {
|
---|
226 | if (callee == null) {
|
---|
227 | init();
|
---|
228 | }
|
---|
229 | callee.addPropertyset(ps);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Set target to execute.
|
---|
235 | * @param target the name of the target to execute.
|
---|
236 | */
|
---|
237 | public void setTarget(String target) {
|
---|
238 | if (callee == null) {
|
---|
239 | init();
|
---|
240 | }
|
---|
241 | callee.setTarget(target);
|
---|
242 | theTarget = target;
|
---|
243 | targetSet = true;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Add a target to the list of targets to invoke.
|
---|
248 | * @param t <code>Ant.TargetElement</code> representing the target.
|
---|
249 | * @since Ant 1.6.3
|
---|
250 | */
|
---|
251 | public void addConfiguredTarget(Ant.TargetElement t) {
|
---|
252 | if (callee == null) {
|
---|
253 | init();
|
---|
254 | }
|
---|
255 | callee.addConfiguredTarget(t);
|
---|
256 | targetSet = true;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * @see Task#handleOutput(String)
|
---|
261 | *
|
---|
262 | * @since Ant 1.5
|
---|
263 | */
|
---|
264 | public void handleOutput(String output) {
|
---|
265 | if (callee != null) {
|
---|
266 | callee.handleOutput(output);
|
---|
267 | } else {
|
---|
268 | super.handleOutput(output);
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * @see Task#handleInput(byte[], int, int)
|
---|
274 | *
|
---|
275 | * @since Ant 1.6
|
---|
276 | */
|
---|
277 | public int handleInput(byte[] buffer, int offset, int length)
|
---|
278 | throws IOException {
|
---|
279 | if (callee != null) {
|
---|
280 | return callee.handleInput(buffer, offset, length);
|
---|
281 | } else {
|
---|
282 | return super.handleInput(buffer, offset, length);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * @see Task#handleFlush(String)
|
---|
288 | *
|
---|
289 | * @since Ant 1.5.2
|
---|
290 | */
|
---|
291 | public void handleFlush(String output) {
|
---|
292 | if (callee != null) {
|
---|
293 | callee.handleFlush(output);
|
---|
294 | } else {
|
---|
295 | super.handleFlush(output);
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * @see Task#handleErrorOutput(String)
|
---|
301 | *
|
---|
302 | * @since Ant 1.5
|
---|
303 | */
|
---|
304 | public void handleErrorOutput(String output) {
|
---|
305 | if (callee != null) {
|
---|
306 | callee.handleErrorOutput(output);
|
---|
307 | } else {
|
---|
308 | super.handleErrorOutput(output);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * @see Task#handleErrorFlush(String)
|
---|
314 | *
|
---|
315 | * @since Ant 1.5.2
|
---|
316 | */
|
---|
317 | public void handleErrorFlush(String output) {
|
---|
318 | if (callee != null) {
|
---|
319 | callee.handleErrorFlush(output);
|
---|
320 | } else {
|
---|
321 | super.handleErrorFlush(output);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 |
|
---|
327 |
|
---|
328 |
|
---|
329 |
|
---|
330 | /* --- node address relations --- */
|
---|
331 | public static boolean isAfter(String x, String y) {
|
---|
332 |
|
---|
333 | System.out.println( "x: " + x );
|
---|
334 | System.out.println( "y: " + y );
|
---|
335 |
|
---|
336 | if ( x.equals("") || y.equals("") ) {
|
---|
337 | throw new BuildException("Error - x or y not specified !!");
|
---|
338 | }
|
---|
339 |
|
---|
340 | //check that both are in the form int.int.int
|
---|
341 |
|
---|
342 | //break into chambers
|
---|
343 | String[] xs = x.split("\\.");
|
---|
344 | String[] ys = y.split("\\.");
|
---|
345 |
|
---|
346 | //figure out n
|
---|
347 | int n = ys.length;
|
---|
348 |
|
---|
349 | //step through, making sure x values are higher than or equal to y values
|
---|
350 | for ( int i=0; i<n; i++ ) {
|
---|
351 | int xValue = i<xs.length ? Integer.parseInt( xs[i] ) : 0;
|
---|
352 | int yValue = Integer.parseInt( ys[i] );
|
---|
353 |
|
---|
354 | System.out.println( xValue + " " + yValue );
|
---|
355 | if( xValue < yValue ) {
|
---|
356 | System.out.println("x not after y");
|
---|
357 | return false;
|
---|
358 | }
|
---|
359 |
|
---|
360 | if( xValue > yValue ) {
|
---|
361 | System.out.println("x is after y");
|
---|
362 | return true;
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | // if execution reaches here, first n places are equal
|
---|
367 | System.out.println("x is after y");
|
---|
368 | return true;
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | public static boolean isDescendantOf(String x, String y) {
|
---|
373 |
|
---|
374 | System.out.println( "x: " + x );
|
---|
375 | System.out.println( "y: " + y );
|
---|
376 |
|
---|
377 | if ( x.equals("") || y.equals("") ) {
|
---|
378 | throw new BuildException("Error - x or y not specified !!");
|
---|
379 | }
|
---|
380 |
|
---|
381 | //check that both are in the form int.int.int
|
---|
382 |
|
---|
383 | //break into chambers
|
---|
384 | String[] xs = x.split("\\.");
|
---|
385 | String[] ys = y.split("\\.");
|
---|
386 |
|
---|
387 | //make sure x is at least as deep as y
|
---|
388 | if ( xs.length < ys.length ) {
|
---|
389 | System.out.println("x not descendant of y");
|
---|
390 | return false;
|
---|
391 | }
|
---|
392 |
|
---|
393 | //figure out n
|
---|
394 | int n = ys.length;
|
---|
395 |
|
---|
396 | //step through, making sure x values are equal to y values
|
---|
397 | for ( int i=0; i<n; i++ ) {
|
---|
398 | int xValue = Integer.parseInt( xs[i] );
|
---|
399 | int yValue = Integer.parseInt( ys[i] );
|
---|
400 |
|
---|
401 | System.out.println( xValue + " " + yValue );
|
---|
402 | if( xValue != yValue ) {
|
---|
403 | System.out.println("x not descendant of y");
|
---|
404 | return false;
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | // if execution reaches here, first n places are equal
|
---|
409 | System.out.println("x is descendant of y");
|
---|
410 | return true;
|
---|
411 | }
|
---|
412 |
|
---|
413 | public static boolean isEqualTo(String x, String y) {
|
---|
414 |
|
---|
415 | System.out.println( "x: " + x );
|
---|
416 | System.out.println( "y: " + y );
|
---|
417 |
|
---|
418 | if ( x.equals("") || y.equals("") ) {
|
---|
419 | throw new BuildException("Error - x or y not specified !!");
|
---|
420 | }
|
---|
421 |
|
---|
422 | //check that both are in the form int.int.int
|
---|
423 |
|
---|
424 | //break into chambers
|
---|
425 | String[] xs = x.split("\\.");
|
---|
426 | String[] ys = y.split("\\.");
|
---|
427 |
|
---|
428 | //make sure x is exactly as deep as y
|
---|
429 | if ( xs.length != ys.length ) {
|
---|
430 | System.out.println("x not equal to y");
|
---|
431 | return false;
|
---|
432 | }
|
---|
433 |
|
---|
434 | //figure out n
|
---|
435 | int n = xs.length;
|
---|
436 |
|
---|
437 | //step through, making sure x values are equal to y values
|
---|
438 | for ( int i=0; i<n; i++ ) {
|
---|
439 | int xValue = Integer.parseInt( xs[i] );
|
---|
440 | int yValue = Integer.parseInt( ys[i] );
|
---|
441 |
|
---|
442 | System.out.println( xValue + " " + yValue );
|
---|
443 | if( xValue != yValue ) {
|
---|
444 | System.out.println("x not equal to y");
|
---|
445 | return false;
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | // if execution reaches here, first all places
|
---|
450 | System.out.println("x is equal to y");
|
---|
451 | return true;
|
---|
452 | }
|
---|
453 |
|
---|
454 | }
|
---|