| 1 28 29 package com.idaremedia.antx.starters; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Location; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Task; 38 import org.apache.tools.ant.UnknownElement; 39 import org.apache.tools.ant.taskdefs.Antlib; 40 import org.apache.tools.ant.taskdefs.AntlibDefinition; 41 42 import com.idaremedia.antx.AntX; 43 import com.idaremedia.antx.AntXFixture; 44 import com.idaremedia.antx.helpers.TaskHandle; 45 import com.idaremedia.antx.helpers.Tk; 46 import com.idaremedia.antx.ownhelpers.TaskExaminer; 47 48 60 61 public abstract class StrictAntLib extends Antlib implements StrictOuterTask 62 { 63 67 protected StrictAntLib() 68 { 69 super(); 70 Iam_ = AntX.fixture+"AntLib:"; 71 } 72 73 74 75 80 protected StrictAntLib(String iam) 81 { 82 super(); 83 Iam_= Tk.cvlabelFrom(iam); 84 } 85 86 87 88 93 public void setURI(String uri) 94 { 95 super.setURI(uri); 96 m_uriLocal = uri; 97 } 98 99 100 101 105 public final String getDefaultItemURI() 106 { 107 return m_uriLocal; 108 } 109 110 111 112 117 public void setClassLoader(ClassLoader cl) 118 { 119 super.setClassLoader(cl); 120 m_clLocal = cl; 121 } 122 123 124 125 129 public final ClassLoader getDefaultItemClassLoader() 130 { 131 if (m_clLocal==null) { 132 m_clLocal= Antlib.class.getClassLoader(); 133 } 134 return m_clLocal; 135 } 136 137 138 139 146 public void addTask(Task task) 147 { 148 TaskHandle taskH = new TaskHandle(task); 149 150 if (includeTask(taskH)) { 151 getTasksList().add(taskH.getTask()); 152 } else { 153 String ermsg = AntX.uistrs().get("taskset.nested.task.disallowed", 154 task.getTaskName(), this.getTaskName()); 155 log(ermsg, Project.MSG_ERR); 156 throw new BuildException(ermsg,getLocation()); 157 } 158 } 159 160 161 162 167 protected final List getTasksList() 168 { 169 return m_nestedTasks; 170 } 171 172 173 174 186 protected boolean includeTask(TaskHandle taskH) 187 { 188 boolean ok= checkTaskCount(taskH); 189 if (ok) { 190 Class c = TaskExaminer.trueClass(taskH.getTask()); 191 ok = checkTaskType(c,taskH); 192 } 193 return ok; 194 } 195 196 197 198 206 protected boolean checkTaskCount(TaskHandle taskH) 207 { 208 return true; 209 } 210 211 212 213 220 protected boolean checkTaskType(Class c, TaskHandle taskH) 221 { 222 return c!=null && AntlibDefinition.class.isAssignableFrom(c); 223 } 224 225 226 227 231 protected final void verifyInProject_(String calr) 232 { 233 if (getProject()==null) { 234 String ermsg = AntX.uistrs().get("cv.verifyInP",Iam_,calr); 235 log(ermsg, Project.MSG_ERR); 236 throw new IllegalStateException (ermsg); 237 } 238 } 239 240 241 242 249 protected void verifyCanExecute_(String calr) 250 { 251 verifyInProject_(calr); 252 } 253 254 255 256 262 protected void performLibraryTask(Task nestedTask) 263 { 264 AntlibDefinition libTask = (AntlibDefinition)nestedTask; 265 libTask.setURI(getDefaultItemURI()); 266 libTask.setAntlibClassLoader(getDefaultItemClassLoader()); 267 libTask.execute(); } 269 270 271 272 276 protected void aboutToInstall() 277 { 278 } 280 281 282 283 291 protected void installCompleted(RuntimeException uncaught) 292 { 293 } 295 296 297 298 307 public void execute() 308 { 309 verifyCanExecute_("exec"); 310 311 List tl = getTasksList(); 312 313 RuntimeException failure=null; 314 aboutToInstall(); 315 try { 316 if (!tl.isEmpty()) { 317 Location libLocation = getLocation(); 318 319 for (Iterator itr= tl.iterator();itr.hasNext();) { 320 Task libitem = (Task)itr.next(); 321 setLocation(libitem.getLocation()); 322 323 if (libitem instanceof UnknownElement) { 324 UnknownElement ue= (UnknownElement)libitem; 325 ue.maybeConfigure(); 326 libitem = ue.getTask(); 327 } 328 if (libitem!=null) { 329 performLibraryTask(libitem); 330 } 331 } 332 333 setLocation(libLocation); 334 } 335 } catch(RuntimeException rtX) { 336 failure = rtX; 337 throw rtX; 338 } finally { 339 installCompleted(failure); 340 } 341 } 342 343 344 protected final String Iam_; 345 private List m_nestedTasks= AntXFixture.newList(10); 346 private String m_uriLocal; 347 private ClassLoader m_clLocal; 348 } 349 350 351 | Popular Tags |