001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: AbstractClasspathValidateMojo.java 3838 2011-10-08 20:15:41Z schulte2005 $
029     *
030     */
031    package org.jomc.mojo;
032    
033    import java.util.logging.Level;
034    import javax.xml.bind.JAXBContext;
035    import javax.xml.bind.util.JAXBSource;
036    import javax.xml.transform.Source;
037    import org.apache.maven.plugin.MojoExecutionException;
038    import org.jomc.modlet.ModelContext;
039    import org.jomc.modlet.ModelValidationReport;
040    import org.jomc.modlet.ObjectFactory;
041    import org.jomc.tools.ClassFileProcessor;
042    
043    /**
044     * Base class for validating class path class file model objects.
045     *
046     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
047     * @version $JOMC: AbstractClasspathValidateMojo.java 3838 2011-10-08 20:15:41Z schulte2005 $
048     * @since 1.1
049     */
050    public abstract class AbstractClasspathValidateMojo extends AbstractJomcMojo
051    {
052    
053        /** Constant for the name of the tool backing the mojo. */
054        private static final String TOOLNAME = "ClassFileProcessor";
055    
056        /** Creates a new {@code AbstractClasspathValidateMojo} instance. */
057        public AbstractClasspathValidateMojo()
058        {
059            super();
060        }
061    
062        @Override
063        protected final void executeTool() throws Exception
064        {
065            this.logSeparator();
066            this.logProcessingModel( TOOLNAME, this.getModel() );
067    
068            final ModelContext context = this.createModelContext( this.getClasspathClassLoader() );
069            final ClassFileProcessor tool = this.createClassFileProcessor( context );
070            final JAXBContext jaxbContext = context.createContext( this.getModel() );
071            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
072            ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
073    
074            this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
075    
076            if ( validationReport.isModelValid() )
077            {
078                validationReport = tool.validateModelObjects( context );
079                this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
080    
081                if ( !validationReport.isModelValid() )
082                {
083                    throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
084                }
085    
086                this.logToolSuccess( TOOLNAME );
087            }
088            else
089            {
090                throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
091            }
092        }
093    
094        /**
095         * Gets the class loader to validate class path model objects of.
096         *
097         * @return The class loader to validate class path model objects of.
098         *
099         * @throws MojoExecutionException if getting the class loader fails.
100         */
101        protected abstract ClassLoader getClasspathClassLoader() throws MojoExecutionException;
102    
103    }