Home » geronimo-2.2-source-release » org.apache.geronimo.mavenplugins.geronimo.reporting » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *    http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   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: 547772 $ $Date: 2007-06-15 12:59:14 -0700 (Fri, 15 Jun 2007) $
   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       // TODO: Figure out how to inject the mojo's goal name
   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       /**
   74        * Whether to stop or proceed when errors and failures are encountered
   75        *
   76        * @parameter expression="${stopOnFailure}" default-value="true"
   77        */
   78       protected boolean stopOnFailure;
   79   
   80       /**
   81        * Provides hooks into the reporting interface to allow for customized reports to be generated
   82        * for goal executions.
   83        */
   84       public void execute() throws MojoExecutionException, MojoFailureException {
   85           init();
   86   
   87           if ( log.isDebugEnabled() ) {
   88               if ( reporters != null ) {
   89                   log.debug("Reporters: " + Arrays.asList(reporters));
   90               }
   91               else {
   92                   log.debug("No reporters configured");
   93               }
   94           }
   95   
   96           reportBegin();
   97   
   98           try {
   99               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   }

Home » geronimo-2.2-source-release » org.apache.geronimo.mavenplugins.geronimo.reporting » [javadoc | source]