ValidateClassesTask.java

  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: ValidateClassesTask.java 5043 2015-05-27 07:03:39Z schulte $
  29.  *
  30.  */
  31. package org.jomc.ant;

  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.util.logging.Level;
  35. import javax.xml.bind.JAXBContext;
  36. import javax.xml.bind.JAXBException;
  37. import javax.xml.bind.util.JAXBSource;
  38. import javax.xml.transform.Source;
  39. import org.apache.tools.ant.BuildException;
  40. import org.jomc.model.Implementation;
  41. import org.jomc.model.Module;
  42. import org.jomc.model.Specification;
  43. import org.jomc.modlet.Model;
  44. import org.jomc.modlet.ModelContext;
  45. import org.jomc.modlet.ModelException;
  46. import org.jomc.modlet.ModelValidationReport;
  47. import org.jomc.modlet.ObjectFactory;
  48. import org.jomc.tools.ClassFileProcessor;

  49. /**
  50.  * Task for validating class file model objects.
  51.  *
  52.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
  53.  * @version $JOMC: ValidateClassesTask.java 5043 2015-05-27 07:03:39Z schulte $
  54.  */
  55. public final class ValidateClassesTask extends ClassFileProcessorTask
  56. {

  57.     /**
  58.      * The directory holding the class files to validate model objects of.
  59.      */
  60.     private File classesDirectory;

  61.     /**
  62.      * Creates a new {@code ValidateClassesTask} instance.
  63.      */
  64.     public ValidateClassesTask()
  65.     {
  66.         super();
  67.     }

  68.     /**
  69.      * Gets the directory holding the class files to validate model objects of.
  70.      *
  71.      * @return The directory holding the class files to validate model objects of or {@code null}.
  72.      *
  73.      * @see #setClassesDirectory(java.io.File)
  74.      */
  75.     public File getClassesDirectory()
  76.     {
  77.         return this.classesDirectory;
  78.     }

  79.     /**
  80.      * Sets the directory holding the class files to validate model objects of.
  81.      *
  82.      * @param value The new directory holding the class files to validate model objects of or {@code null}.
  83.      *
  84.      * @see #getClassesDirectory()
  85.      */
  86.     public void setClassesDirectory( final File value )
  87.     {
  88.         this.classesDirectory = value;
  89.     }

  90.     /**
  91.      * {@inheritDoc}
  92.      */
  93.     @Override
  94.     public void preExecuteTask() throws BuildException
  95.     {
  96.         super.preExecuteTask();

  97.         this.assertNotNull( "classesDirectory", this.getClassesDirectory() );
  98.     }

  99.     /**
  100.      * Validates class file model objects.
  101.      *
  102.      * @throws BuildException if validating class file model objects fails.
  103.      */
  104.     @Override
  105.     public void processClassFiles() throws BuildException
  106.     {
  107.         ProjectClassLoader classLoader = null;
  108.         boolean suppressExceptionOnClose = true;

  109.         try
  110.         {
  111.             this.log( Messages.getMessage( "validatingModelObjects", this.getModel() ) );

  112.             classLoader = this.newProjectClassLoader();
  113.             final ModelContext context = this.newModelContext( classLoader );
  114.             final ClassFileProcessor tool = this.newClassFileProcessor();
  115.             final JAXBContext jaxbContext = context.createContext( this.getModel() );
  116.             final Model model = this.getModel( context );
  117.             final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
  118.             ModelValidationReport validationReport = context.validateModel( this.getModel(), source );

  119.             this.logValidationReport( context, validationReport );
  120.             tool.setModel( model );

  121.             if ( validationReport.isModelValid() )
  122.             {
  123.                 final Specification s = this.getSpecification( model );
  124.                 final Implementation i = this.getImplementation( model );
  125.                 final Module m = this.getModule( model );

  126.                 if ( s != null )
  127.                 {
  128.                     validationReport = tool.validateModelObjects( s, context, this.getClassesDirectory() );

  129.                     if ( validationReport != null )
  130.                     {
  131.                         this.logValidationReport( context, validationReport );

  132.                         if ( !validationReport.isModelValid() )
  133.                         {
  134.                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
  135.                         }
  136.                     }
  137.                 }

  138.                 if ( i != null )
  139.                 {
  140.                     validationReport = tool.validateModelObjects( i, context, this.getClassesDirectory() );

  141.                     if ( validationReport != null )
  142.                     {
  143.                         this.logValidationReport( context, validationReport );

  144.                         if ( !validationReport.isModelValid() )
  145.                         {
  146.                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
  147.                         }
  148.                     }
  149.                 }

  150.                 if ( m != null )
  151.                 {
  152.                     validationReport = tool.validateModelObjects( m, context, this.getClassesDirectory() );

  153.                     if ( validationReport != null )
  154.                     {
  155.                         this.logValidationReport( context, validationReport );

  156.                         if ( !validationReport.isModelValid() )
  157.                         {
  158.                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
  159.                         }
  160.                     }
  161.                 }

  162.                 if ( this.isModulesProcessingRequested() )
  163.                 {
  164.                     validationReport = tool.validateModelObjects( context, this.getClassesDirectory() );

  165.                     if ( validationReport != null )
  166.                     {
  167.                         this.logValidationReport( context, validationReport );

  168.                         if ( !validationReport.isModelValid() )
  169.                         {
  170.                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
  171.                         }
  172.                     }
  173.                 }

  174.                 suppressExceptionOnClose = false;
  175.             }
  176.             else
  177.             {
  178.                 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
  179.             }
  180.         }
  181.         catch ( final IOException e )
  182.         {
  183.             throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
  184.         }
  185.         catch ( final JAXBException e )
  186.         {
  187.             throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
  188.         }
  189.         catch ( final ModelException e )
  190.         {
  191.             throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
  192.         }
  193.         finally
  194.         {
  195.             try
  196.             {
  197.                 if ( classLoader != null )
  198.                 {
  199.                     classLoader.close();
  200.                 }
  201.             }
  202.             catch ( final IOException e )
  203.             {
  204.                 if ( suppressExceptionOnClose )
  205.                 {
  206.                     this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
  207.                 }
  208.                 else
  209.                 {
  210.                     throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
  211.                 }
  212.             }
  213.         }
  214.     }

  215.     /**
  216.      * {@inheritDoc}
  217.      */
  218.     @Override
  219.     public ValidateClassesTask clone()
  220.     {
  221.         final ValidateClassesTask clone = (ValidateClassesTask) super.clone();
  222.         clone.classesDirectory =
  223.             this.classesDirectory != null ? new File( this.classesDirectory.getAbsolutePath() ) : null;

  224.         return clone;
  225.     }

  226. }