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