001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.geronimo.mavenplugins.geronimo.reporting;
021
022 import org.apache.geronimo.mavenplugins.geronimo.GeronimoMojoSupport;
023
024 import java.io.File;
025 import java.util.Date;
026 import java.util.Arrays;
027
028 import org.apache.maven.plugin.MojoExecutionException;
029 import org.apache.maven.plugin.MojoFailureException;
030
031 /**
032 * Support for Geronimo mojos which can be processed by a set of {@link Reporter}s.
033 *
034 * @version $Rev: 547772 $ $Date: 2007-06-15 15:59:14 -0400 (Fri, 15 Jun 2007) $
035 */
036 public abstract class ReportingMojoSupport
037 extends GeronimoMojoSupport
038 {
039 /**
040 * Enable logging mode.
041 *
042 * @parameter expression="${logOutput}" default-value="false"
043 */
044 protected boolean logOutput = false;
045
046 //
047 // TODO: Figure out how to inject the mojo's goal name
048 //
049
050 /**
051 * The directory where log files will be put under.
052 *
053 * @parameter expression="${logOutputDirectory}" default-value="${project.build.directory}/geronimo-logs"
054 */
055 protected File logOutputDirectory;
056
057 /**
058 * When logOutput is enabled, the output is logged to the file location specified here. If this
059 * value is not present, then "${logOutputDirectory}/<em>goal-name</em>" log will be used.
060 *
061 * @parameter
062 */
063 protected File logFile = null;
064
065 /**
066 * A set of reporters which will do something interesting with the execution results.
067 *
068 * @parameter
069 */
070 protected Reporter[] reporters = null;
071
072
073 /**
074 * Whether to stop or proceed when errors and failures are encountered
075 *
076 * @parameter expression="${stopOnFailure}" default-value="true"
077 */
078 protected boolean stopOnFailure;
079
080 /**
081 * Provides hooks into the reporting interface to allow for customized reports to be generated
082 * for goal executions.
083 */
084 public void execute() throws MojoExecutionException, MojoFailureException {
085 init();
086
087 if ( log.isDebugEnabled() ) {
088 if ( reporters != null ) {
089 log.debug("Reporters: " + Arrays.asList(reporters));
090 }
091 else {
092 log.debug("No reporters configured");
093 }
094 }
095
096 reportBegin();
097
098 try {
099 doExecute();
100 }
101 catch ( Exception e ) {
102 reportError(e);
103
104 if ( stopOnFailure ) {
105 if ( e instanceof MojoExecutionException ) {
106 throw new MojoExecutionException(e.getMessage(), e);
107 }
108 else if ( e instanceof MojoFailureException ) {
109 MojoFailureException x = new MojoFailureException(e.getMessage());
110 x.initCause(e);
111 throw x;
112 }
113 else {
114 throw new MojoExecutionException(e.getMessage(), e);
115 }
116 }
117 else {
118 log.warn("Ignoring failure !");
119 }
120 }
121 finally {
122 reportEnd();
123 }
124 }
125
126 protected File getLogFile() {
127 if (logFile == null) {
128 return new File(logOutputDirectory, getFullClassName() + ".log");
129 }
130
131 try {
132 return logFile.getCanonicalFile();
133 }
134 catch (java.io.IOException e) {
135 throw new RuntimeException(e);
136 }
137 }
138
139 /**
140 * Sub-class must override to provide the goal name.
141 *
142 * @return The name of the goal.
143 */
144 protected abstract String getFullClassName();
145
146 //
147 // Reporter
148 //
149
150 private void reportBegin() {
151 if ( reporters == null ) {
152 return;
153 }
154
155 final Reportable source = new Reportable() {
156 final Date start = new Date();
157
158 public Date getStartTime() {
159 return start;
160 }
161
162 public String getName() {
163 return getFullClassName();
164 }
165
166 public File getLogFile() {
167 return ReportingMojoSupport.this.getLogFile();
168 }
169 };
170
171 for ( int i =0; i < reporters.length; i++ ) {
172 reporters[i].reportBegin(source);
173 }
174 }
175
176 private void reportError(final Throwable cause) {
177 assert cause != null;
178
179 if ( reporters == null ) {
180 return;
181 }
182
183 for ( int i=0; i < reporters.length; i++ ) {
184 reporters[i].reportError(cause);
185 }
186 }
187
188 private void reportEnd() {
189 if ( reporters == null ) {
190 return;
191 }
192
193 for ( int i=0; i < reporters.length; i++ ) {
194 reporters[i].reportEnd();
195 }
196 }
197 }