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: 451383 $ $Date: 2006-09-29 11:54:33 -0700 (Fri, 29 Sep 2006) $
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         * Provides hooks into the reporting interface to allow for customized reports to be generated
074         * for goal executions.
075         */
076        public void execute() throws MojoExecutionException, MojoFailureException {
077            init();
078    
079            if (log.isDebugEnabled()) {
080                if (reporters != null) {
081                    log.debug("Reporters: " + Arrays.asList(reporters));
082                }
083                else {
084                    log.debug("No reporters configured");
085                }
086            }
087            
088            reportBegin();
089    
090            try {
091                doExecute();
092            }
093            catch(Exception e) {
094                reportError(e);
095    
096                if (e instanceof MojoExecutionException) {
097                    throw new MojoExecutionException(e.getMessage(), e);
098                }
099                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        // Reporter
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    }