View Javadoc
1   /*
2    *   Copyright (C) Christian Schulte <cs@schulte.it>, 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: AbstractClassesValidateMojo.java 5043 2015-05-27 07:03:39Z schulte $
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.ClassFileProcessor;
44  
45  /**
46   * Base class for validating class file model objects.
47   *
48   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
49   * @version $JOMC: AbstractClassesValidateMojo.java 5043 2015-05-27 07:03:39Z schulte $
50   */
51  public abstract class AbstractClassesValidateMojo extends AbstractJomcMojo
52  {
53  
54      /**
55       * Constant for the name of the tool backing the mojo.
56       */
57      private static final String TOOLNAME = "ClassFileProcessor";
58  
59      /**
60       * Creates a new {@code AbstractClassesValidateMojo} instance.
61       */
62      public AbstractClassesValidateMojo()
63      {
64          super();
65      }
66  
67      @Override
68      protected final void executeTool() throws Exception
69      {
70          this.logSeparator();
71  
72          if ( this.isClassProcessingEnabled() )
73          {
74              this.logProcessingModule( TOOLNAME, this.getClassesModuleName() );
75  
76              final ModelContext context = this.createModelContext( this.getClassesClassLoader() );
77              final ClassFileProcessor tool = this.createClassFileProcessor( context );
78              final JAXBContext jaxbContext = context.createContext( this.getModel() );
79              final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
80              ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
81              this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
82  
83              if ( validationReport.isModelValid() )
84              {
85                  final Module module =
86                      tool.getModules() != null ? tool.getModules().getModule( this.getClassesModuleName() ) : null;
87  
88                  if ( module != null )
89                  {
90                      validationReport = tool.validateModelObjects( module, context, this.getClassesDirectory() );
91  
92                      if ( validationReport != null )
93                      {
94                          this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE,
95                                    validationReport );
96  
97                          if ( !validationReport.isModelValid() )
98                          {
99                              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 }