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: ManageSourcesTask.java 5043 2015-05-27 07:03:39Z schulte $
29   *
30   */
31  package org.jomc.ant;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.logging.Level;
36  import javax.xml.bind.JAXBContext;
37  import javax.xml.bind.JAXBException;
38  import javax.xml.bind.util.JAXBSource;
39  import javax.xml.transform.Source;
40  import org.apache.tools.ant.BuildException;
41  import org.jomc.model.Implementation;
42  import org.jomc.model.Module;
43  import org.jomc.model.Specification;
44  import org.jomc.modlet.Model;
45  import org.jomc.modlet.ModelContext;
46  import org.jomc.modlet.ModelException;
47  import org.jomc.modlet.ModelValidationReport;
48  import org.jomc.modlet.ObjectFactory;
49  import org.jomc.tools.SourceFileProcessor;
50  
51  /**
52   * Task for managing source files.
53   *
54   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
55   * @version $JOMC: ManageSourcesTask.java 5043 2015-05-27 07:03:39Z schulte $
56   */
57  public final class ManageSourcesTask extends SourceFileProcessorTask
58  {
59  
60      /**
61       * The directory holding the source files to manage.
62       */
63      private File sourcesDirectory;
64  
65      /**
66       * Creates a new {@code ManageSourcesTask} instance.
67       */
68      public ManageSourcesTask()
69      {
70          super();
71      }
72  
73      /**
74       * Gets the directory holding the source files to manage.
75       *
76       * @return The directory holding the source files to manage or {@code null}.
77       *
78       * @see #setSourcesDirectory(java.io.File)
79       */
80      public File getSourcesDirectory()
81      {
82          return this.sourcesDirectory;
83      }
84  
85      /**
86       * Sets the directory holding the source files to manage.
87       *
88       * @param value The new directory holding the source files to manage or {@code null}.
89       *
90       * @see #getSourcesDirectory()
91       */
92      public void setSourcesDirectory( final File value )
93      {
94          this.sourcesDirectory = value;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public void preExecuteTask() throws BuildException
102     {
103         super.preExecuteTask();
104 
105         this.assertNotNull( "sourcesDirectory", this.getSourcesDirectory() );
106     }
107 
108     /**
109      * Manages source files.
110      *
111      * @throws BuildException if managing source files fails.
112      */
113     @Override
114     public void processSourceFiles() throws BuildException
115     {
116         ProjectClassLoader classLoader = null;
117         boolean suppressExceptionOnClose = true;
118 
119         try
120         {
121             this.log( Messages.getMessage( "managingSources", this.getModel() ) );
122 
123             classLoader = this.newProjectClassLoader();
124             final ModelContext context = this.newModelContext( classLoader );
125             final SourceFileProcessor tool = this.newSourceFileProcessor();
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.manageSourceFiles( s, this.getSourcesDirectory() );
143                 }
144 
145                 if ( i != null )
146                 {
147                     tool.manageSourceFiles( i, this.getSourcesDirectory() );
148                 }
149 
150                 if ( m != null )
151                 {
152                     tool.manageSourceFiles( m, this.getSourcesDirectory() );
153                 }
154 
155                 if ( this.isModulesProcessingRequested() )
156                 {
157                     tool.manageSourceFiles( this.getSourcesDirectory() );
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 SourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
170         }
171         catch ( final JAXBException e )
172         {
173             throw new SourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
174         }
175         catch ( final ModelException e )
176         {
177             throw new SourceProcessingException( 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 SourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
197                 }
198             }
199         }
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public ManageSourcesTask clone()
207     {
208         final ManageSourcesTask clone = (ManageSourcesTask) super.clone();
209         clone.sourcesDirectory =
210             this.sourcesDirectory != null ? new File( this.sourcesDirectory.getAbsolutePath() ) : null;
211 
212         return clone;
213     }
214 
215 }