1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 /**
32 * Support for Geronimo mojos which can be processed by a set of {@link Reporter}s.
33 *
34 * @version $Rev: 451383 $ $Date: 2006-09-29 11:54:33 -0700 (Fri, 29 Sep 2006) $
35 */
36 public abstract class ReportingMojoSupport
37 extends GeronimoMojoSupport
38 {
39 /**
40 * Enable logging mode.
41 *
42 * @parameter expression="${logOutput}" default-value="false"
43 */
44 protected boolean logOutput = false;
45
46
47
48
49
50 /**
51 * The directory where log files will be put under.
52 *
53 * @parameter expression="${logOutputDirectory}" default-value="${project.build.directory}/geronimo-logs"
54 */
55 protected File logOutputDirectory;
56
57 /**
58 * When logOutput is enabled, the output is logged to the file location specified here. If this
59 * value is not present, then "${logOutputDirectory}/<em>goal-name</em>" log will be used.
60 *
61 * @parameter
62 */
63 protected File logFile = null;
64
65 /**
66 * A set of reporters which will do something interesting with the execution results.
67 *
68 * @parameter
69 */
70 protected Reporter[] reporters = null;
71
72 /**
73 * Provides hooks into the reporting interface to allow for customized reports to be generated
74 * for goal executions.
75 */
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, getGoalName() + ".log");
116 }
117
118 return logFile;
119 }
120
121 /**
122 * Sub-class must override to provide the goal name.
123 *
124 * @return The name of the goal.
125 */
126 protected abstract String getGoalName();
127
128
129
130
131
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 getGoalName();
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 }