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: AbstractSourcesManageMojo.java 3838 2011-10-08 20:15:41Z schulte2005 $
029     *
030     */
031    package org.jomc.mojo;
032    
033    import java.io.File;
034    import java.util.logging.Level;
035    import javax.xml.bind.JAXBContext;
036    import javax.xml.bind.util.JAXBSource;
037    import javax.xml.transform.Source;
038    import org.apache.maven.plugin.MojoExecutionException;
039    import org.jomc.model.Module;
040    import org.jomc.modlet.ModelContext;
041    import org.jomc.modlet.ModelValidationReport;
042    import org.jomc.modlet.ObjectFactory;
043    import org.jomc.tools.SourceFileProcessor;
044    
045    /**
046     * Base class for managing source code files.
047     *
048     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
049     * @version $JOMC: AbstractSourcesManageMojo.java 3838 2011-10-08 20:15:41Z schulte2005 $
050     */
051    public abstract class AbstractSourcesManageMojo extends AbstractJomcMojo
052    {
053    
054        /** Constant for the name of the tool backing the class. */
055        private static final String TOOLNAME = "SourceFileProcessor";
056    
057        /** Creates a new {@code AbstractSourcesManageMojo} instance. */
058        public AbstractSourcesManageMojo()
059        {
060            super();
061        }
062    
063        @Override
064        protected final void executeTool() throws Exception
065        {
066            this.logSeparator();
067    
068            if ( this.isSourceProcessingEnabled() )
069            {
070                this.logProcessingModule( TOOLNAME, this.getSourcesModuleName() );
071    
072                final ModelContext context = this.createModelContext( this.getSourcesClassLoader() );
073                final SourceFileProcessor tool = this.createSourceFileProcessor( context );
074                final JAXBContext jaxbContext = context.createContext( this.getModel() );
075                final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
076                final 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                    final Module module = tool.getModules().getModule( this.getSourcesModuleName() );
083    
084                    if ( module != null )
085                    {
086                        tool.manageSourceFiles( module, this.getSourcesDirectory() );
087                        this.logToolSuccess( TOOLNAME );
088                    }
089                    else
090                    {
091                        this.logMissingModule( this.getSourcesModuleName() );
092                    }
093                }
094                else
095                {
096                    throw new MojoExecutionException( Messages.getMessage( "sourceProcessingFailure" ) );
097                }
098            }
099            else if ( this.isLoggable( Level.INFO ) )
100            {
101                this.log( Level.INFO, Messages.getMessage( "sourceProcessingDisabled" ), null );
102            }
103        }
104    
105        /**
106         * Gets the name of the module to manage source code files of.
107         *
108         * @return The name of the module to manage source code files of.
109         *
110         * @throws MojoExecutionException if getting the name fails.
111         */
112        protected abstract String getSourcesModuleName() throws MojoExecutionException;
113    
114        /**
115         * Gets the class loader to use for managing source code files.
116         *
117         * @return The class loader to use for managing source code files.
118         *
119         * @throws MojoExecutionException if getting the class loader fails.
120         */
121        protected abstract ClassLoader getSourcesClassLoader() throws MojoExecutionException;
122    
123        /**
124         * Gets the directory holding the source code files to manage.
125         *
126         * @return The directory holding the source code files to manage.
127         *
128         * @throws MojoExecutionException if getting the directory fails.
129         */
130        protected abstract File getSourcesDirectory() throws MojoExecutionException;
131    
132    }