1 package net.firstpartners.nounit.ant; 2 3 import net.firstpartners.nounit.ui.common.CommandPackage; 4 import net.firstpartners.nounit.ui.common.IProcessor; 5 import net.firstpartners.nounit.ui.common.Processor; 6 import net.firstpartners.nounit.ui.common.XMLProcessor; 7 import net.firstpartners.nounit.utility.NoUnitException; 8 9 import org.apache.log4j.BasicConfigurator; 10 import org.apache.log4j.PropertyConfigurator; 11 import org.apache.log4j.Logger; 12 13 import org.apache.tools.ant.BuildException; 14 import org.apache.tools.ant.Task; 15 16 import java.io.File ; 17 18 27 public class NoUnitTask extends Task { 28 29 private String startDirectory; 30 private String outputDirectory; 31 private String reportClass; 32 private String userMessage; 33 private String reportName; 34 private String outputFile; 35 private String xmlFileName; 36 private String log4jConfig; 37 38 static Logger log = Logger.getLogger(NoUnitTask.class); 40 41 44 public NoUnitTask() { 45 super(); 46 } 47 48 51 public void init() { 52 53 super.init(); 55 56 if(this.log4jConfig==null){ 59 BasicConfigurator.configure(); 61 System.out.println("using Default log4j file"); 63 } else { 64 PropertyConfigurator.configure(log4jConfig); 66 File logFile=new File (log4jConfig); 67 System.out.println("Using Log4j config file:"+logFile.getAbsolutePath()); } 69 70 71 } 72 73 74 78 public void execute() throws BuildException{ 79 80 81 try{ 82 this.validateAttributes(); 84 85 86 CommandPackage pckage = this.getCommandPackage(); 89 90 IProcessor mainProcessor = this.getProcessor(); 92 93 CommandPackage results = mainProcessor.transform(pckage); 95 96 super.handleOutput(results.getString(CommandPackage.USER_MESSAGE)); 98 99 100 }catch(NoUnitException ex){ 101 102 logExceptionTrace(ex); 104 throw new BuildException("NoUnitException occurred in Ant Task", ex); 105 106 }catch(Exception ex){ 107 108 logExceptionTrace(ex); 110 throw new BuildException("Exception occurred in Ant Task", ex); 111 } 112 } 113 114 118 private void logExceptionTrace(Exception exceptionToPrint){ 119 120 StackTraceElement [] stackElements = exceptionToPrint.getStackTrace(); 122 123 for (int a=0;a<stackElements.length;a++){ 125 126 super.handleErrorOutput(stackElements[a].toString()); 127 } 128 129 } 130 131 135 private IProcessor getProcessor(){ 136 IProcessor processr = null; 137 138 if( this.isPropertyValid(this.reportClass) && this.isPropertyValid(this.reportName) ){ 141 processr = new Processor(); 142 this.log("using the default processor"); 143 }else{ 144 processr = new XMLProcessor(); 145 this.log("using the XML Only processor"); 146 } 147 return processr; 148 } 149 154 private CommandPackage getCommandPackage(){ 155 156 CommandPackage pckage = new CommandPackage(); 157 pckage.addValue(CommandPackage.START_DIR, this.startDirectory); 159 pckage.addValue(CommandPackage.OUTPUT_DIR, this.outputDirectory); 160 161 if(this.isPropertyValid(this.reportClass)){ 162 pckage.addValue(CommandPackage.REPORT_CLASS, this.reportClass); 163 } 164 if(this.isPropertyValid(this.reportName)){ 165 pckage.addValue("report_name", this.reportName); 166 } 167 if(this.isPropertyValid(this.outputFile)){ 168 pckage.addValue("output_file", this.outputFile); 169 } 170 if(this.isPropertyValid(this.xmlFileName)){ 171 pckage.addValue(CommandPackage.XML_OUTPUT_NAME, this.xmlFileName); 172 } 173 174 return pckage; 175 } 176 181 private boolean isPropertyValid(String property){ 182 return (property != null && !property.equals("")); 183 } 184 185 190 public void validateAttributes() throws BuildException{ 191 if((this.startDirectory == null) || (this.startDirectory.equals("")) || 192 (this.outputDirectory == null) || (this.outputDirectory.equals(""))) { 193 throw new BuildException("Missing required attribute!"); 194 } 195 } 196 200 public void setOutputFile(String outputFile) { 201 this.outputFile = outputFile; 202 } 203 204 208 public void setReportClass(String reportClass) { 209 this.reportClass = reportClass; 210 } 211 212 216 public void setReportName(String reportName) { 217 this.reportName = reportName; 218 } 219 220 224 public void setStartDirectory(String startDirectory) { 225 this.startDirectory = startDirectory; 226 this.log("set start directory to:"+startDirectory); 227 } 228 229 233 public void setUserMessage(String userMessage) { 234 this.userMessage = userMessage; 235 } 236 237 241 public void setOutputDirectory(String outputDirectory) { 242 this.outputDirectory = outputDirectory; 243 log.debug("Setting output directory to:"+outputDirectory); 244 } 245 246 250 public void setXmlFileName(String string) { 251 xmlFileName = string; 252 log.debug("Setting xml file name to:"+string); 253 } 254 255 259 public void setLog4jConfig(String string) { 260 log4jConfig = string; 261 log.debug("Setting log4j config to:"+string); 262 } 263 264 } 265 | Popular Tags |