1 28 29 package com.idaremedia.antx.flowcontrol.call; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Target; 38 import org.apache.tools.ant.Task; 39 import org.apache.tools.ant.taskdefs.Ant; 40 import org.apache.tools.ant.taskdefs.Property; 41 import org.apache.tools.ant.types.PropertySet; 42 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.AntXFixture; 45 import com.idaremedia.antx.ownhelpers.ProjectDependentSkeleton; 46 import com.idaremedia.antx.ownhelpers.ScopedProperties; 47 48 66 67 public final class LocalTargetCaller extends ProjectDependentSkeleton 68 implements TargetCaller 69 { 70 private static final String IAM_= AntX.flow+"LocalTargetCaller"; 71 72 73 79 public LocalTargetCaller() 80 { 81 } 82 83 84 90 public LocalTargetCaller(CallerTask task) 91 { 92 setCaller(task); 93 } 94 95 96 97 104 public void setCaller(Task task) 105 { 106 AntX.require_(task!=null,IAM_,"setCalr- nonzro task"); 107 Target parent = task.getOwningTarget(); 108 if (parent!=null) { 109 setTarget(parent.getName()); 110 } 111 setProject(task.getProject()); 112 m_owningTask = task; 113 } 114 115 116 117 120 public void setTarget(String targetName) 121 { 122 m_targetName = targetName; 123 } 124 125 126 129 public String getTargetName() 130 { 131 return m_targetName; 132 } 133 134 135 138 public final void setStepName(String stepName) 139 { 140 m_stepName = stepName; 141 } 142 143 144 147 public final String getStepName() 148 { 149 return m_stepName; 150 } 151 152 153 156 public Property createProperty() 157 { 158 Property p = new Property(); 159 p.setProject(getProject()); 160 if (m_plist==null) { 161 m_plist = AntXFixture.newList(); 162 } 163 m_plist.add(p); 164 return p; 165 } 166 167 168 171 public PropertySet createPropertySet() 172 { 173 PropertySet p = new PropertySet(); p.setProject(getProject()); 175 if (m_plist==null) { 176 m_plist = AntXFixture.newList(); 177 } 178 m_plist.add(p); 179 return p; 180 } 181 182 183 184 188 public Ant.Reference createReference() 189 { 190 return null; 191 } 192 193 194 195 199 private void installOverlay() 200 { 201 Project P= getProjectNoNull(); 202 if (m_plist!=null) { 203 m_overlay = new ScopedProperties(P,true); 204 Iterator itr= m_plist.iterator(); 205 while (itr.hasNext()) { 206 Object o = itr.next(); 207 if (o instanceof Property) { 208 m_overlay.put((Property)o); 209 } else { 210 m_overlay.put((PropertySet)o); 211 } 212 } 213 m_overlay.install(); 214 } 215 } 216 217 218 221 private void uninstallOverlay() 222 { 223 if (m_overlay!=null) { 224 m_overlay.uninstall(null); 225 m_overlay=null; 226 } 227 } 228 229 230 private Task[] getFreshTargetTasks(String targetName) 231 { 232 Target t = (Target)getProject().getTargets().get(getTargetName()); 233 return t.getTasks(); 235 } 236 237 238 243 public void run() 244 throws BuildException 245 { 246 installOverlay(); 247 try { 248 if (getStepName()==null) { 249 getProject().executeTarget(getTargetName()); 250 } 251 else { 252 StepLauncher steplaunch = new StepLauncher(); 253 steplaunch.setProject(getProject()); 254 255 Task[] fromTasks = getFreshTargetTasks(getTargetName()); 256 257 InlineStep step = (InlineStep)steplaunch.getInlinedTarget 258 (getStepName(),InlineStep.class,fromTasks,1,1); 259 260 steplaunch = null; 262 if (step!=null) { 263 step.maybeConfigure(); 264 step.run(); 265 } else { 266 String error = AntX.uistrs(). 267 get("flow.steplaunch.missing.step", getStepName(), 268 getTargetName()); 269 m_owningTask.log(error,Project.MSG_ERR); 270 throw new BuildException 271 (error, m_owningTask.getLocation()); 272 } 273 } 274 } finally { 275 uninstallOverlay(); 276 } 277 } 278 279 280 281 289 public final void run(String property, String value) 290 throws BuildException 291 { 292 Property p= this.createProperty(); 293 p.setName(property); 294 p.setValue(value); 295 run(); 296 } 297 298 299 300 306 public final void run(Map properties) 307 throws BuildException 308 { 309 if (!properties.isEmpty()) { 310 Iterator itr = properties.entrySet().iterator(); 311 while (itr.hasNext()) { 312 Map.Entry item = (Map.Entry )itr.next(); 313 Property p= this.createProperty(); 314 p.setName(item.getKey().toString()); 315 p.setValue(item.getValue().toString()); 316 } 317 } 318 run(); 319 } 320 321 322 323 329 public final void run(TargetCaller.Prep prep) 330 throws BuildException 331 { 332 prep.prepare(this); 333 run(); 334 } 335 336 337 private String m_targetName, m_stepName; 338 private List m_plist; private ScopedProperties m_overlay; private Task m_owningTask; 341 } 342 343 344 | Popular Tags |