View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 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: AbstractResourcesWriteMojo.java 4613 2012-09-22 10:07:08Z schulte $
29   *
30   */
31  package org.jomc.mojo;
32  
33  import java.io.File;
34  import java.util.Locale;
35  import java.util.logging.Level;
36  import javax.xml.bind.JAXBContext;
37  import javax.xml.bind.util.JAXBSource;
38  import javax.xml.transform.Source;
39  import org.apache.commons.io.FileUtils;
40  import org.apache.maven.model.Resource;
41  import org.apache.maven.plugin.MojoExecutionException;
42  import org.apache.maven.project.MavenProject;
43  import org.jomc.model.Module;
44  import org.jomc.modlet.ModelContext;
45  import org.jomc.modlet.ModelValidationReport;
46  import org.jomc.modlet.ObjectFactory;
47  import org.jomc.tools.ResourceFileProcessor;
48  
49  /**
50   * Base class for writing resource files.
51   *
52   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
53   * @version $JOMC: AbstractResourcesWriteMojo.java 4613 2012-09-22 10:07:08Z schulte $
54   */
55  public abstract class AbstractResourcesWriteMojo extends AbstractJomcMojo
56  {
57  
58      /** Constant for the name of the tool backing the class. */
59      private static final String TOOLNAME = "ResourceFileProcessor";
60  
61      /**
62       * The language of the default language properties file of generated resource bundle properties resources.
63       *
64       * @parameter expression="${jomc.resourceBundleDefaultLanguage}"
65       */
66      private String resourceBundleDefaultLanguage;
67  
68      /** Creates a new {@code AbstractResourcesWriteMojo} instance. */
69      public AbstractResourcesWriteMojo()
70      {
71          super();
72      }
73  
74      @Override
75      protected final void executeTool() throws Exception
76      {
77          this.logSeparator();
78  
79          if ( this.isResourceProcessingEnabled() )
80          {
81              this.logProcessingModule( TOOLNAME, this.getResourcesModuleName() );
82  
83              final ModelContext context = this.createModelContext( this.getResourcesClassLoader() );
84              final ResourceFileProcessor tool = this.createResourceFileProcessor( context );
85              final JAXBContext jaxbContext = context.createContext( this.getModel() );
86              final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
87              final ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
88  
89              if ( this.resourceBundleDefaultLanguage != null )
90              {
91                  tool.setResourceBundleDefaultLocale(
92                      new Locale( this.resourceBundleDefaultLanguage.toLowerCase( Locale.ENGLISH ) ) );
93  
94              }
95  
96              this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
97  
98              if ( validationReport.isModelValid() )
99              {
100                 final Module module =
101                     tool.getModules() != null ? tool.getModules().getModule( this.getResourcesModuleName() ) : null;
102 
103                 if ( module != null )
104                 {
105                     if ( !this.getResourcesDirectory().exists() && !this.getResourcesDirectory().mkdirs() )
106                     {
107                         throw new MojoExecutionException( Messages.getMessage(
108                             "failedCreatingDirectory", this.getResourcesDirectory().getAbsolutePath() ) );
109 
110                     }
111 
112                     tool.writeResourceBundleResourceFiles( module, this.getResourcesDirectory() );
113 
114                     if ( !this.getResourcesDirectory().equals( this.getResourcesOutputDirectory() ) )
115                     {
116                         FileUtils.copyDirectory( this.getResourcesDirectory(), this.getResourcesOutputDirectory() );
117                     }
118 
119                     final Resource resource = new Resource();
120                     resource.setDirectory( this.getResourcesDirectory().getAbsolutePath() );
121                     resource.setFiltering( false );
122 
123                     this.addMavenResource( this.getMavenProject(), resource );
124 
125                     this.logToolSuccess( TOOLNAME );
126                 }
127                 else
128                 {
129                     this.logMissingModule( this.getResourcesModuleName() );
130                 }
131             }
132             else
133             {
134                 throw new MojoExecutionException( Messages.getMessage( "resourceProcessingFailure" ) );
135             }
136         }
137         else if ( this.isLoggable( Level.INFO ) )
138         {
139             this.log( Level.INFO, Messages.getMessage( "resourceProcessingDisabled" ), null );
140         }
141     }
142 
143     /**
144      * Gets the name of the module to write resource files of.
145      *
146      * @return The name of the module to write resource files of.
147      *
148      * @throws MojoExecutionException if getting the name fails.
149      */
150     protected abstract String getResourcesModuleName() throws MojoExecutionException;
151 
152     /**
153      * Gets the class loader to use for writing resource files.
154      *
155      * @return The class loader to use for writing resource files.
156      *
157      * @throws MojoExecutionException if getting the class loader fails.
158      */
159     protected abstract ClassLoader getResourcesClassLoader() throws MojoExecutionException;
160 
161     /**
162      * Gets the directory to write the resource files to.
163      *
164      * @return The directory to write the resource files to.
165      *
166      * @throws MojoExecutionException if getting the directory fails.
167      */
168     protected abstract File getResourcesDirectory() throws MojoExecutionException;
169 
170     /**
171      * Gets the directory to copy resource files to.
172      *
173      * @return The directory to copy resource files to.
174      *
175      * @throws MojoExecutionException if getting the directory fails.
176      *
177      * @since 1.2
178      */
179     protected abstract File getResourcesOutputDirectory() throws MojoExecutionException;
180 
181     /**
182      * Adds a resource to a {@code MavenProjecŧ}.
183      *
184      * @param mavenProject The {@code MavenProject} to add a resource to.
185      * @param resource The {@code Resource} to add.
186      *
187      * @throws MojoExecutionException if adding the resource fails.
188      *
189      * @since 1.2
190      */
191     protected abstract void addMavenResource( MavenProject mavenProject, Resource resource )
192         throws MojoExecutionException;
193 
194 }