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