1 19 20 package org.apache.geronimo.mavenplugins.geronimo.reporting; 21 22 import org.apache.geronimo.mavenplugins.geronimo.GeronimoMojoSupport; 23 24 import java.io.File ; 25 import java.util.Date ; 26 import java.util.Arrays ; 27 28 import org.apache.maven.plugin.MojoExecutionException; 29 import org.apache.maven.plugin.MojoFailureException; 30 31 36 public abstract class ReportingMojoSupport 37 extends GeronimoMojoSupport 38 { 39 44 protected boolean logOutput = false; 45 46 50 55 protected File logOutputDirectory; 56 57 63 protected File logFile = null; 64 65 70 protected Reporter[] reporters = null; 71 72 76 public void execute() throws MojoExecutionException, MojoFailureException { 77 init(); 78 79 if (log.isDebugEnabled()) { 80 if (reporters != null) { 81 log.debug("Reporters: " + Arrays.asList(reporters)); 82 } 83 else { 84 log.debug("No reporters configured"); 85 } 86 } 87 88 reportBegin(); 89 90 try { 91 doExecute(); 92 } 93 catch(Exception e) { 94 reportError(e); 95 96 if (e instanceof MojoExecutionException) { 97 throw new MojoExecutionException(e.getMessage(), e); 98 } 99 else if (e instanceof MojoFailureException) { 100 MojoFailureException x = new MojoFailureException(e.getMessage()); 101 x.initCause(e); 102 throw x; 103 } 104 else { 105 throw new MojoExecutionException(e.getMessage(), e); 106 } 107 } 108 finally { 109 reportEnd(); 110 } 111 } 112 113 protected File getLogFile() { 114 if (logFile == null) { 115 return new File (logOutputDirectory, getFullClassName() + ".log"); 116 } 117 118 return logFile; 119 } 120 121 126 protected abstract String getFullClassName(); 127 128 132 private void reportBegin() { 133 if (reporters == null) { 134 return; 135 } 136 137 final Reportable source = new Reportable() { 138 final Date start = new Date (); 139 140 public Date getStartTime() { 141 return start; 142 } 143 144 public String getName() { 145 return getFullClassName(); 146 } 147 148 public File getLogFile() { 149 return ReportingMojoSupport.this.getLogFile(); 150 } 151 }; 152 153 for (int i =0; i < reporters.length; i++) { 154 reporters[i].reportBegin(source); 155 } 156 } 157 158 private void reportError(final Throwable cause) { 159 assert cause != null; 160 161 if (reporters == null) { 162 return; 163 } 164 165 for (int i=0; i < reporters.length; i++) { 166 reporters[i].reportError(cause); 167 } 168 } 169 170 private void reportEnd() { 171 if (reporters == null) { 172 return; 173 } 174 175 for (int i=0; i < reporters.length; i++) { 176 reporters[i].reportEnd(); 177 } 178 } 179 } 180 | Popular Tags |