1 41 42 package com.mullassery.act; 43 44 import java.awt.Toolkit ; 45 import java.io.File ; 46 import java.util.Enumeration ; 47 import java.util.HashMap ; 48 import java.util.Hashtable ; 49 import java.util.Iterator ; 50 51 import org.apache.tools.ant.BuildEvent; 52 import org.apache.tools.ant.BuildException; 53 import org.apache.tools.ant.BuildListener; 54 import org.apache.tools.ant.DefaultLogger; 55 import org.apache.tools.ant.IntrospectionHelper; 56 import org.apache.tools.ant.Project; 57 import org.apache.tools.ant.Target; 58 import org.apache.tools.ant.Task; 59 import org.w3c.dom.Document ; 60 import org.w3c.dom.Element ; 61 import org.w3c.dom.NamedNodeMap ; 62 import org.w3c.dom.Node ; 63 import org.w3c.dom.NodeList ; 64 65 import com.mullassery.act.util.DebugUtil; 66 import com.mullassery.act.util.ResourceUtil; 67 import com.mullassery.act.util.XMLUtil; 68 69 73 public class TaskMaster { 74 public final static String CHILDREN = "children"; 75 public final Project proj = new Project(); 76 78 79 public TaskMaster() throws ACTException { 80 this.projectInitialize(); 81 } 82 83 86 public static HashMap getElementInfo(Class element) throws ACTException { 87 HashMap info = new HashMap (); 88 try { 89 IntrospectionHelper ih = IntrospectionHelper.getHelper(element); 90 Enumeration en = ih.getAttributes(); 91 String attr = null; 92 while (en.hasMoreElements()) { 93 attr = en.nextElement().toString(); 94 info.put(attr, ih.getAttributeType(attr)); 95 } 96 97 en = ih.getNestedElements(); 98 HashMap childElements = null; 99 String nestedElement = null; 100 while (en.hasMoreElements()) { 101 nestedElement = en.nextElement().toString(); 102 if (childElements == null) 103 childElements = new HashMap (); 104 childElements.put( 105 nestedElement, 106 ih.getElementType(nestedElement)); 107 } 108 if (childElements != null) 109 info.put(CHILDREN, childElements); 110 } catch (Throwable be) { 111 throw new ACTException( 112 "Could not get all the details of the element : " + be); 113 } 114 return info; 115 } 116 117 public static void setAttributes(Project proj, Object t, Element vals) { 118 IntrospectionHelper ih = IntrospectionHelper.getHelper(t.getClass()); 119 120 NodeList nl = vals.getChildNodes(); 121 Node n = null; 122 for (int i = 0; i < nl.getLength(); i++) { 123 n = nl.item(i); 124 if (n.getNodeType() == Node.ELEMENT_NODE) { 125 Object nested = ih.createElement(proj, t, n.getNodeName()); 126 setAttributes(proj, nested, (Element ) n); 127 ih.storeElement(proj, t, nested, n.getNodeName()); 128 } 129 } 130 131 NamedNodeMap nm = vals.getAttributes(); 132 for (int i = 0; i < nm.getLength(); i++) { 133 n = nm.item(i); 134 if (n.getNodeType() == Node.ATTRIBUTE_NODE) { 135 ih.setAttribute(proj, t, n.getNodeName(), n.getNodeValue()); 136 } 137 } 138 } 139 140 public void addDataType(String typeName, String typeClass) 141 throws ACTException { 142 try { 143 proj.addDataTypeDefinition(typeName, Class.forName(typeClass)); 144 } catch (Exception e) { 145 throw new ACTException(e.getMessage()); 146 } 147 } 148 149 public void addTask(String taskName, String taskClass) 150 throws ACTException { 151 try { 152 proj.addTaskDefinition(taskName, Class.forName(taskClass)); 153 } catch (ClassNotFoundException e) { 154 throw new ACTException("Could not find " + taskClass); 155 } catch (Exception e) { 156 throw new ACTException(e.toString()); 157 } 158 } 159 public Task createMainTask(final String mainTask, final String dir) 161 throws ACTException { 162 Task t = null; 163 try { 164 t = (Task) (getTaskClass(mainTask).newInstance()); 165 } catch (Exception e) { 166 throw new ACTException( 167 "Could not create the Task Object (" 168 + mainTask 169 + ") : " 170 + e.getLocalizedMessage()); 171 } 172 this.proj.setBasedir(dir); 173 t.setProject(this.proj); 174 t.setTaskName(mainTask); 175 Target trg = new Target(); 176 trg.setName("ACT-Execution"); 177 trg.addTask(t); 178 t.setOwningTarget(trg); 179 proj.addOrReplaceTarget(trg); 180 return t; 181 } 182 183 public void execute(final Task tsk) throws ACTException { 184 try { 185 proj.executeTarget(tsk.getOwningTarget().getName()); 186 } catch (BuildException be) { 187 throw new ACTException( 188 "Task Execution Failed : " + be.getMessage()); 189 } 190 } 191 192 public void executeTask( 193 final String mainTask, 194 final String dir, 195 final Element values) 196 throws ACTException { 197 DefaultLogger dl = new DefaultLogger(); 198 dl.setOutputPrintStream(System.out); 199 dl.setErrorPrintStream(System.err); 200 dl.setMessageOutputLevel(Project.MSG_INFO); 201 executeTask(mainTask, dir, values, dl); 202 } 203 204 public void executeTask( 205 final String mainTask, 206 final String dir, 207 final Element values, 208 BuildListener bl) 209 throws ACTException { 210 synchronized (this.proj) { 211 Task tsk = createMainTask(mainTask, Project.translatePath(dir)); 212 if (bl != null) 213 tsk.getProject().addBuildListener(bl); 214 setAttributes(tsk.getProject(), tsk, values); 215 execute(tsk); 216 } 217 } 218 219 public Hashtable getAllTasks() throws ACTException { 220 return proj.getTaskDefinitions(); 221 } 222 223 public Hashtable getAllTypes() throws ACTException { 224 return this.proj.getDataTypeDefinitions(); 225 } 226 227 public Class getElementClass(String elem) throws ACTException { 228 Class clazz = getTypeClass(elem); 229 return clazz != null ? clazz : getTaskClass(elem); 230 } 231 232 public String getElementHelp(String elem) throws ACTException { 233 HashMap val = getElementInfo(getElementClass(elem)); 234 StringBuffer sb = 235 new StringBuffer ("\"" + elem + "\" properties listed below :"); 236 String prop = null; 237 Object obj = null; 238 Iterator en = val.keySet().iterator(); 239 while (en.hasNext()) { 240 obj = en.next(); 241 prop = obj.toString(); 242 obj = val.get(obj); 243 if (!prop.equals(CHILDREN)) { 244 sb.append("\n\t-" + prop); 245 } 246 } 247 if (val.containsKey(CHILDREN)) { 248 sb.append("\n\t\"Also supports the following nested elements "); 249 sb.append("\n\t Usage -[element].[property] [value]"); 250 HashMap nVal = (HashMap ) val.get(CHILDREN); 251 en = nVal.keySet().iterator(); 252 while (en.hasNext()) { 253 obj = en.next(); 254 prop = obj.toString(); 255 sb.append("\n\t\t" + prop); 256 } 257 } 258 return sb.toString(); 259 } 260 261 public Class getTaskClass(java.lang.String task) throws ACTException { 262 return (Class ) getAllTasks().get(task); 263 } 264 265 public Class getTypeClass(java.lang.String type) throws ACTException { 266 return (Class ) getAllTypes().get(type); 267 } 268 269 private void projectInitialize() throws ACTException { 270 try { 271 this.proj.init(); 272 } catch (BuildException be) { 273 throw new ACTException( 274 "Initialization Failed : " + be.getMessage()); 275 } 276 File tf = ResourceUtil.getSettingsFile(); 277 DebugUtil.debug("Using settings from " + tf.getAbsolutePath()); 278 if (!tf.exists()) 279 return; 280 addGroups(XMLUtil.getDocumentElement(tf)); 281 } 282 283 private void addGroups(Element group) { 284 NodeList nGroups = group.getChildNodes(); 285 DebugUtil.debug( 286 "\t" 287 + group.getNodeName() 288 + " has children : " 289 + nGroups.getLength()); 290 for (int i = 0; i < nGroups.getLength(); i++) { 292 try { 293 if (nGroups.item(i).getNodeType() != Node.ELEMENT_NODE 294 || !nGroups.item(i).getNodeName().equals("task-group")) { 295 continue; 296 } 297 Element node = (Element ) nGroups.item(i); 298 String name = node.getAttribute("display-name"); 299 if (name != null && name.length() > 0) { 300 DebugUtil.debug("Adding group " + name); 301 addGroups(node); 302 } 303 } catch (Throwable t) { 304 continue; 305 } 306 } 307 addTasks(group); 308 } 309 310 private void addTasks(Element group) { 311 NodeList tasks = group.getElementsByTagName("task"); 312 DebugUtil.debug("Found " + tasks.getLength() + " tasks"); 313 for (int j = 0; j < tasks.getLength(); j++) { 314 Element task = (Element ) tasks.item(j); 315 if (!task.hasAttribute("class") || !task.hasAttribute("name")) 316 continue; 317 String id = task.getAttribute("name"); 318 String tClass = task.getAttribute("class"); 319 if (id.length() == 0 && tClass.length() == 0) 320 continue; 321 try { 322 this.addTask(id, tClass); 323 DebugUtil.debug("Added " + id + " : " + tClass + " task"); 324 } catch (ACTException e) { DebugUtil.debug(e.toString()); 326 } 327 } 328 } 329 330 public void removeTask(String taskName) { 331 proj.getTaskDefinitions().remove(taskName); 332 } 333 334 public void run(String [] args) throws ACTException { 336 String [] params = handleInput(args); 337 if (params == null) 338 return; 339 String taskName = params[0]; 340 Document doc = XMLUtil.getDocumentBuilder().newDocument(); 341 Element e = doc.createElement(taskName); 342 for (int i = 2; i < params.length; i += 2) { 343 String name = params[i - 1].substring(1); 344 String value = params[i]; 345 346 HashMap cm = getElementInfo(getElementClass(e.getNodeName())); 347 XMLUtil.setProperty(cm, e, name, value); 348 } 349 this.executeTask(taskName, ".", e, new BuildListener() { 350 boolean logging = false; 351 private void addMessage(String evt) { 352 if (logging) 353 System.out.println(evt); 354 } 355 public void messageLogged(BuildEvent evt) { 356 addMessage(evt.getMessage()); 357 } 358 public void buildFinished(BuildEvent arg0) { 359 } 360 public void buildStarted(BuildEvent arg0) { 361 } 362 public void targetFinished(BuildEvent arg0) { 363 } 364 public void targetStarted(BuildEvent arg0) { 365 } 366 public void taskFinished(BuildEvent evt) { 367 Toolkit.getDefaultToolkit().beep(); 368 logging = false; 369 } 370 public void taskStarted(BuildEvent evt) { 371 logging = true; 372 } 373 }); 374 } 375 376 private String [] handleInput(String [] args) throws ACTException { 377 if (args.length == 0 || (args[0].equals("-dir") && args.length < 3)) { 378 System.err.println( 379 "Usage: ACT [-dir parent_dir_of_act-setting.xml] taskname attributes" 380 + "Try task name -help for help."); 381 return null; 382 } 383 if (args[0].equals("-dir")) { 384 File dir = new File (args[1]); 385 if (dir.exists() && dir.isDirectory()) { 386 ResourceUtil.actDir = args[1]; 387 } else { 388 System.err.println(args[1] + " is not a valid directory!"); 389 return null; 390 } 391 String [] temp = new String [args.length - 2]; 392 System.arraycopy(args, 2, temp, 0, args.length-2); 393 args = temp; 394 } 395 if ((args.length > 1 396 && (args[1].equals("?") || args[1].equalsIgnoreCase("-help")))) { 397 System.err.println(getElementHelp(args[0])); 398 return null; 399 } 400 return args; 401 } 402 } 403 | Popular Tags |