001/*
002 *   Copyright (C) Christian Schulte <cs@schulte.it>, 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: ValidateClasspathTask.java 5043 2015-05-27 07:03:39Z schulte $
029 *
030 */
031package org.jomc.ant;
032
033import java.io.IOException;
034import java.util.logging.Level;
035import javax.xml.bind.JAXBContext;
036import javax.xml.bind.JAXBException;
037import javax.xml.bind.util.JAXBSource;
038import javax.xml.transform.Source;
039import org.apache.tools.ant.BuildException;
040import org.jomc.model.Implementation;
041import org.jomc.model.Module;
042import org.jomc.model.Specification;
043import org.jomc.modlet.Model;
044import org.jomc.modlet.ModelContext;
045import org.jomc.modlet.ModelException;
046import org.jomc.modlet.ModelValidationReport;
047import org.jomc.modlet.ObjectFactory;
048import org.jomc.tools.ClassFileProcessor;
049
050/**
051 * Task for validating class path model objects.
052 *
053 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
054 * @version $JOMC: ValidateClasspathTask.java 5043 2015-05-27 07:03:39Z schulte $
055 */
056public final class ValidateClasspathTask extends ClassFileProcessorTask
057{
058
059    /**
060     * Creates a new {@code ValidateClasspathTask} instance.
061     */
062    public ValidateClasspathTask()
063    {
064        super();
065    }
066
067    /**
068     * Validates class file model objects.
069     *
070     * @throws BuildException if validating class file model objects fails.
071     */
072    @Override
073    public void processClassFiles() throws BuildException
074    {
075        ProjectClassLoader classLoader = null;
076        boolean suppressExceptionOnClose = true;
077
078        try
079        {
080            this.log( Messages.getMessage( "validatingClasspath", this.getModel() ) );
081
082            classLoader = this.newProjectClassLoader();
083            final ModelContext context = this.newModelContext( classLoader );
084            final ClassFileProcessor tool = this.newClassFileProcessor();
085            final JAXBContext jaxbContext = context.createContext( this.getModel() );
086            final Model model = this.getModel( context );
087            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
088            ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
089
090            this.logValidationReport( context, validationReport );
091            tool.setModel( model );
092
093            if ( validationReport.isModelValid() )
094            {
095                final Specification s = this.getSpecification( model );
096                final Implementation i = this.getImplementation( model );
097                final Module m = this.getModule( model );
098
099                if ( s != null )
100                {
101                    validationReport = tool.validateModelObjects( s, context );
102
103                    if ( validationReport != null )
104                    {
105                        this.logValidationReport( context, validationReport );
106
107                        if ( !validationReport.isModelValid() )
108                        {
109                            throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
110                        }
111                    }
112                }
113
114                if ( i != null )
115                {
116                    validationReport = tool.validateModelObjects( i, context );
117
118                    if ( validationReport != null )
119                    {
120                        this.logValidationReport( context, validationReport );
121
122                        if ( !validationReport.isModelValid() )
123                        {
124                            throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
125                        }
126                    }
127                }
128
129                if ( m != null )
130                {
131                    validationReport = tool.validateModelObjects( m, context );
132
133                    if ( validationReport != null )
134                    {
135                        this.logValidationReport( context, validationReport );
136
137                        if ( !validationReport.isModelValid() )
138                        {
139                            throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
140                        }
141                    }
142                }
143
144                if ( this.isModulesProcessingRequested() )
145                {
146                    validationReport = tool.validateModelObjects( context );
147
148                    if ( validationReport != null )
149                    {
150                        this.logValidationReport( context, validationReport );
151
152                        if ( !validationReport.isModelValid() )
153                        {
154                            throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
155                        }
156                    }
157                }
158
159                suppressExceptionOnClose = false;
160            }
161            else
162            {
163                throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
164            }
165        }
166        catch ( final IOException e )
167        {
168            throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
169        }
170        catch ( final JAXBException e )
171        {
172            throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
173        }
174        catch ( final ModelException e )
175        {
176            throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
177        }
178        finally
179        {
180            try
181            {
182                if ( classLoader != null )
183                {
184                    classLoader.close();
185                }
186            }
187            catch ( final IOException e )
188            {
189                if ( suppressExceptionOnClose )
190                {
191                    this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
192                }
193                else
194                {
195                    throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
196                }
197            }
198        }
199    }
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    public ValidateClasspathTask clone()
206    {
207        return (ValidateClasspathTask) super.clone();
208    }
209
210}