001/*
002 *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
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 5135 2016-04-08 13:53:07Z 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 5135 2016-04-08 13:53:07Z schulte $
048 * @since 1.1
049 */
050public abstract class AbstractClasspathValidateMojo extends AbstractJomcMojo
051{
052
053    /**
054     * Constant for the name of the tool backing the mojo.
055     */
056    private static final String TOOLNAME = "ClassFileProcessor";
057
058    /**
059     * Creates a new {@code AbstractClasspathValidateMojo} instance.
060     */
061    public AbstractClasspathValidateMojo()
062    {
063        super();
064    }
065
066    @Override
067    protected final void executeTool() throws Exception
068    {
069        this.logSeparator();
070        this.logProcessingModel( TOOLNAME, this.getModel() );
071
072        final ModelContext context = this.createModelContext( this.getClasspathClassLoader() );
073        final ClassFileProcessor tool = this.createClassFileProcessor( context );
074        final JAXBContext jaxbContext = context.createContext( this.getModel() );
075        final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
076        ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
077
078        this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
079
080        if ( validationReport.isModelValid() )
081        {
082            validationReport = tool.validateModelObjects( context );
083
084            if ( validationReport != null )
085            {
086                this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
087
088                if ( !validationReport.isModelValid() )
089                {
090                    throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
091                }
092            }
093
094            this.logToolSuccess( TOOLNAME );
095        }
096        else
097        {
098            throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
099        }
100    }
101
102    /**
103     * Gets the class loader to validate class path model objects of.
104     *
105     * @return The class loader to validate class path model objects of.
106     *
107     * @throws MojoExecutionException if getting the class loader fails.
108     */
109    protected abstract ClassLoader getClasspathClassLoader() throws MojoExecutionException;
110
111}