KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > tools > ant > foreach > ForeachTask


1 /*
2  * Copyright 1999,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 package org.apache.axis.tools.ant.foreach;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Task;
20 import org.apache.tools.ant.taskdefs.Ant;
21 import org.apache.tools.ant.taskdefs.Java;
22 import org.apache.tools.ant.taskdefs.Property;
23 import org.apache.tools.ant.types.Commandline;
24 import org.apache.tools.ant.types.Path;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 /**
31  * Call a target foreach entry in a set of parameters based on a fileset.
32  * <p>
33  * <i>For Axis development; there is no support or stability associated
34  * with this task</i>
35  * <pre>
36  * &lt;target name=&quot;target1&quot;&gt;
37  * &lt;foreach target=&quot;target2&quot;&gt;
38  * &lt;param name=&quot;param1&quot;&gt;
39  * &lt;fileset refid=&quot;fset1&quot;/&gt;
40  * &lt;/param&gt;
41  * &lt;param name=&quot;param2&quot;&gt;
42  * &lt;item value=&quot;jar&quot; /&gt;
43  * &lt;item value=&quot;zip&quot; /&gt;
44  * &lt;/param&gt;
45  * &lt;/foreach&gt;
46  * &lt;/target&gt;
47  *
48  * &lt;target name=&quot;target2&quot;&gt;
49  * &lt;echo message=&quot;prop is ${param1}.${param2}&quot; /&gt;
50  * &lt;/target&gt;
51  * </pre>
52  * <br>
53  * Really this just a wrapper around "AntCall"
54  * <br>
55  * Added a "type" attribute that works precisely like its equivalent
56  * in <code>ExecuteOn</code>. It allows the user
57  * to specify whether directories, files, or both directories and files
58  * from the filesets are included as entries in the parameter set.
59  * @ant.task category="axis"
60  * @author <a HREF="mailto:tpv@spamcop.net">Tim Vernum</a>
61  * @author Davanum Srinivas
62  */

63 public class ForeachTask extends Task {
64     private Ant callee;
65     private Java callee2;
66     private String JavaDoc subTarget;
67     private Vector JavaDoc params;
68     private Hashtable JavaDoc properties;
69     // must match the default value of Ant#inheritAll
70
private boolean inheritAll = true;
71     // must match the default value of Ant#inheritRefs
72
private boolean inheritRefs = false;
73     private boolean fork = false;
74     private boolean verbose = false;
75
76     public ForeachTask() {
77         params = new Vector JavaDoc();
78         properties = new Hashtable JavaDoc();
79     }
80
81     public void init() {
82     }
83
84     /**
85      * If true, pass all properties to the new Ant project.
86      * Defaults to true.
87      */

88     public void setInheritAll(boolean inherit) {
89        inheritAll = inherit;
90     }
91
92     /**
93      * If true, pass all references to the new Ant project.
94      * Defaults to false
95      * @param inheritRefs new value
96      */

97     public void setInheritRefs(boolean inheritRefs) {
98         this.inheritRefs = inheritRefs;
99     }
100
101     /**
102      * Target to execute, required.
103      */

104     public void setTarget(String JavaDoc target) {
105         subTarget = target;
106     }
107
108     /**
109      * If true, forks the ant invocation.
110      *
111      * @param f "true|false|on|off|yes|no"
112      */

113     public void setFork(boolean f) {
114         fork = f;
115     }
116
117     /**
118      * Enable verbose output when signing
119      * ; optional: default false
120      */

121     public void setVerbose(final boolean verbose) {
122         this.verbose = verbose;
123     }
124
125     public ParamSet createParam() {
126         ParamSet param = new ParamSet();
127         params.addElement(param);
128         return param;
129     }
130
131     private void buildProperty(String JavaDoc propName, String JavaDoc propValue) {
132         properties.put(propName, propValue);
133     }
134
135     private void executeTarget() {
136         if (subTarget == null) {
137             throw new BuildException("Attribute target is required.",
138                                      getLocation());
139         }
140         if(fork) {
141             executeForkedAntTask();
142         } else {
143             executeAntTask();
144         }
145     }
146
147     private void executeForkedAntTask() {
148         /* if (callee2 == null) { */
149             callee2 = (Java) getProject().createTask("java");
150             callee2.setOwningTarget(getOwningTarget());
151             callee2.setTaskName(getTaskName());
152             callee2.setLocation(getLocation());
153             callee2.setClassname("org.apache.tools.ant.Main");
154             callee2.setAppend(true);
155             callee2.setFork(true);
156             callee2.createJvmarg().setValue("-Xbootclasspath/p:" + System.getProperty("sun.boot.class.path"));
157         /* } */
158         String JavaDoc systemClassPath = System.getProperty("java.class.path");
159         callee2.setClasspath(new Path(getProject(), systemClassPath));
160         String JavaDoc args = "-buildfile " + properties.get("file");
161         Commandline.Argument arguments = callee2.createArg();
162         arguments.setLine(args);
163         if (verbose) {
164             callee2.createArg().setValue("-verbose");
165         }
166         callee2.createArg().setValue(subTarget);
167         if (callee2.executeJava() != 0) {
168             throw new BuildException("Execution of ANT Task failed");
169         }
170     }
171
172     private void executeAntTask() {
173         /* if (callee == null) { */
174             callee = (Ant) getProject().createTask("ant");
175             callee.setOwningTarget(getOwningTarget());
176             callee.setTaskName(getTaskName());
177             callee.init();
178         /* } */
179
180         callee.setAntfile(getProject().getProperty("ant.file"));
181         callee.setTarget(subTarget);
182         callee.setInheritAll(inheritAll);
183         callee.setInheritRefs(inheritRefs);
184         Enumeration JavaDoc keys = properties.keys();
185         while (keys.hasMoreElements()) {
186             String JavaDoc key = (String JavaDoc) keys.nextElement();
187             String JavaDoc val = (String JavaDoc) properties.get(key);
188             Property prop = callee.createProperty();
189             prop.setName(key);
190             prop.setValue(val);
191         }
192         callee.execute();
193         System.gc();
194         System.gc();
195         System.gc();
196     }
197
198     /**
199      * This method is used to recursively iterate through
200      * each parameter set.
201      * It ends up being something like:
202      * <pre>
203      * for( i=0; i< params[0].size ; i++ )
204      * for( j=0; j < params[1].size ; j++ )
205      * for( k=0; k < params[2].size ; k++ )
206      * executeTarget( params[0][i], params[1][j] , params[2][k] ) ;
207      * </pre>
208      */

209     private void executeParameters(int paramNumber) {
210         if (paramNumber == params.size()) {
211             executeTarget();
212         } else {
213             ParamSet paramSet = (ParamSet) params.elementAt(paramNumber);
214             Enumeration JavaDoc values = paramSet.getValues(getProject());
215             while (values.hasMoreElements()) {
216                 String JavaDoc val = (String JavaDoc) values.nextElement();
217                 buildProperty(paramSet.getName(), val);
218                 executeParameters(paramNumber + 1);
219             }
220         }
221     }
222
223     public void execute() {
224         if (subTarget == null) {
225             throw new BuildException("Attribute target is required.", getLocation());
226         }
227         executeParameters(0);
228     }
229 }
230
Popular Tags