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 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.mojo;
032
033import java.util.logging.Level;
034import javax.xml.bind.JAXBContext;
035import javax.xml.bind.util.JAXBSource;
036import javax.xml.transform.Source;
037import org.apache.maven.plugin.MojoExecutionException;
038import org.jomc.modlet.ModelContext;
039import org.jomc.modlet.ModelValidationReport;
040import org.jomc.modlet.ObjectFactory;
041import org.jomc.tools.ClassFileProcessor;
042
043/**
044 * Base class for validating class path class file model objects.
045 *
046 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
047 * @version $JOMC: AbstractClasspathValidateMojo.java 4613 2012-09-22 10:07:08Z schulte $
048 * @since 1.1
049 */
050public 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
080            if ( validationReport != null )
081            {
082                this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
083
084                if ( !validationReport.isModelValid() )
085                {
086                    throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
087                }
088            }
089
090            this.logToolSuccess( TOOLNAME );
091        }
092        else
093        {
094            throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
095        }
096    }
097
098    /**
099     * Gets the class loader to validate class path model objects of.
100     *
101     * @return The class loader to validate class path model objects of.
102     *
103     * @throws MojoExecutionException if getting the class loader fails.
104     */
105    protected abstract ClassLoader getClasspathClassLoader() throws MojoExecutionException;
106
107}