View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: AbstractSourcesManageMojo.java 3838 2011-10-08 20:15:41Z schulte2005 $
29   *
30   */
31  package org.jomc.mojo;
32  
33  import java.io.File;
34  import java.util.logging.Level;
35  import javax.xml.bind.JAXBContext;
36  import javax.xml.bind.util.JAXBSource;
37  import javax.xml.transform.Source;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.jomc.model.Module;
40  import org.jomc.modlet.ModelContext;
41  import org.jomc.modlet.ModelValidationReport;
42  import org.jomc.modlet.ObjectFactory;
43  import org.jomc.tools.SourceFileProcessor;
44  
45  /**
46   * Base class for managing source code files.
47   *
48   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
49   * @version $JOMC: AbstractSourcesManageMojo.java 3838 2011-10-08 20:15:41Z schulte2005 $
50   */
51  public abstract class AbstractSourcesManageMojo extends AbstractJomcMojo
52  {
53  
54      /** Constant for the name of the tool backing the class. */
55      private static final String TOOLNAME = "SourceFileProcessor";
56  
57      /** Creates a new {@code AbstractSourcesManageMojo} instance. */
58      public AbstractSourcesManageMojo()
59      {
60          super();
61      }
62  
63      @Override
64      protected final void executeTool() throws Exception
65      {
66          this.logSeparator();
67  
68          if ( this.isSourceProcessingEnabled() )
69          {
70              this.logProcessingModule( TOOLNAME, this.getSourcesModuleName() );
71  
72              final ModelContext context = this.createModelContext( this.getSourcesClassLoader() );
73              final SourceFileProcessor tool = this.createSourceFileProcessor( context );
74              final JAXBContext jaxbContext = context.createContext( this.getModel() );
75              final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
76              final ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
77  
78              this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
79  
80              if ( validationReport.isModelValid() )
81              {
82                  final Module module = tool.getModules().getModule( this.getSourcesModuleName() );
83  
84                  if ( module != null )
85                  {
86                      tool.manageSourceFiles( module, this.getSourcesDirectory() );
87                      this.logToolSuccess( TOOLNAME );
88                  }
89                  else
90                  {
91                      this.logMissingModule( this.getSourcesModuleName() );
92                  }
93              }
94              else
95              {
96                  throw new MojoExecutionException( Messages.getMessage( "sourceProcessingFailure" ) );
97              }
98          }
99          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 }