1 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 ; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 29 30 63 public class ForeachTask extends Task { 64 private Ant callee; 65 private Java callee2; 66 private String subTarget; 67 private Vector params; 68 private Hashtable properties; 69 private boolean inheritAll = true; 71 private boolean inheritRefs = false; 73 private boolean fork = false; 74 private boolean verbose = false; 75 76 public ForeachTask() { 77 params = new Vector (); 78 properties = new Hashtable (); 79 } 80 81 public void init() { 82 } 83 84 88 public void setInheritAll(boolean inherit) { 89 inheritAll = inherit; 90 } 91 92 97 public void setInheritRefs(boolean inheritRefs) { 98 this.inheritRefs = inheritRefs; 99 } 100 101 104 public void setTarget(String target) { 105 subTarget = target; 106 } 107 108 113 public void setFork(boolean f) { 114 fork = f; 115 } 116 117 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 propName, String 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 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 systemClassPath = System.getProperty("java.class.path"); 159 callee2.setClasspath(new Path(getProject(), systemClassPath)); 160 String 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 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 keys = properties.keys(); 185 while (keys.hasMoreElements()) { 186 String key = (String ) keys.nextElement(); 187 String val = (String ) 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 209 private void executeParameters(int paramNumber) { 210 if (paramNumber == params.size()) { 211 executeTarget(); 212 } else { 213 ParamSet paramSet = (ParamSet) params.elementAt(paramNumber); 214 Enumeration values = paramSet.getValues(getProject()); 215 while (values.hasMoreElements()) { 216 String val = (String ) 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 |