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: AbstractClassesValidateMojo.java 5135 2016-04-08 13:53:07Z schulte $
029 *
030 */
031package org.jomc.mojo;
032
033import java.io.File;
034import java.util.logging.Level;
035import javax.xml.bind.JAXBContext;
036import javax.xml.bind.util.JAXBSource;
037import javax.xml.transform.Source;
038import org.apache.maven.plugin.MojoExecutionException;
039import org.jomc.model.Module;
040import org.jomc.modlet.ModelContext;
041import org.jomc.modlet.ModelValidationReport;
042import org.jomc.modlet.ObjectFactory;
043import org.jomc.tools.ClassFileProcessor;
044
045/**
046 * Base class for validating class file model objects.
047 *
048 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
049 * @version $JOMC: AbstractClassesValidateMojo.java 5135 2016-04-08 13:53:07Z schulte $
050 */
051public abstract class AbstractClassesValidateMojo extends AbstractJomcMojo
052{
053
054    /**
055     * Constant for the name of the tool backing the mojo.
056     */
057    private static final String TOOLNAME = "ClassFileProcessor";
058
059    /**
060     * Creates a new {@code AbstractClassesValidateMojo} instance.
061     */
062    public AbstractClassesValidateMojo()
063    {
064        super();
065    }
066
067    @Override
068    protected final void executeTool() throws Exception
069    {
070        this.logSeparator();
071
072        if ( this.isClassProcessingEnabled() )
073        {
074            this.logProcessingModule( TOOLNAME, this.getClassesModuleName() );
075
076            final ModelContext context = this.createModelContext( this.getClassesClassLoader() );
077            final ClassFileProcessor tool = this.createClassFileProcessor( context );
078            final JAXBContext jaxbContext = context.createContext( this.getModel() );
079            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
080            ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
081            this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
082
083            if ( validationReport.isModelValid() )
084            {
085                final Module module =
086                    tool.getModules() != null ? tool.getModules().getModule( this.getClassesModuleName() ) : null;
087
088                if ( module != null )
089                {
090                    validationReport = tool.validateModelObjects( module, context, this.getClassesDirectory() );
091
092                    if ( validationReport != null )
093                    {
094                        this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE,
095                                  validationReport );
096
097                        if ( !validationReport.isModelValid() )
098                        {
099                            throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
100                        }
101                    }
102
103                    this.logToolSuccess( TOOLNAME );
104                }
105                else
106                {
107                    this.logMissingModule( this.getClassesModuleName() );
108                }
109            }
110            else
111            {
112                throw new MojoExecutionException( Messages.getMessage( "classFileValidationFailure" ) );
113            }
114        }
115        else if ( this.isLoggable( Level.INFO ) )
116        {
117            this.log( Level.INFO, Messages.getMessage( "classFileValidationDisabled" ), null );
118        }
119    }
120
121    /**
122     * Gets the name of the module to validate class file model objects of.
123     *
124     * @return The name of the module to validate class file model objects of.
125     *
126     * @throws MojoExecutionException if getting the name fails.
127     */
128    protected abstract String getClassesModuleName() throws MojoExecutionException;
129
130    /**
131     * Gets the class loader to use for validating class file model objects.
132     *
133     * @return The class loader to use for validating class file model objects.
134     *
135     * @throws MojoExecutionException if getting the class loader fails.
136     */
137    protected abstract ClassLoader getClassesClassLoader() throws MojoExecutionException;
138
139    /**
140     * Gets the directory holding the class files to validate model objects of.
141     *
142     * @return The directory holding the class files to validate model objects of.
143     *
144     * @throws MojoExecutionException if getting the directory fails.
145     */
146    protected abstract File getClassesDirectory() throws MojoExecutionException;
147
148}