View Javadoc

1   /*
2    *  jDTAUS Core Container Mojo
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.mojo;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.FileWriter;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.io.OutputStreamWriter;
29  import java.io.Writer;
30  import java.util.Locale;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.apache.velocity.VelocityContext;
34  import org.jdtaus.core.container.ContainerError;
35  import org.jdtaus.core.container.ContextError;
36  import org.jdtaus.core.container.ModelError;
37  import org.jdtaus.core.container.ModelFactory;
38  import org.jdtaus.core.container.mojo.model.JavaArtifact;
39  import org.jdtaus.core.container.mojo.model.spring.BeansElement;
40  import org.springframework.beans.factory.BeanFactory;
41  import org.springframework.beans.factory.xml.XmlBeanFactory;
42  import org.springframework.core.io.FileSystemResource;
43  
44  /**
45   * Mojo to generate a spring beans descriptor with corresponding support
46   * classes from a project.
47   *
48   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
49   * @version $JDTAUS: SpringDescriptorMojo.java 8853 2014-01-10 13:50:00Z schulte $
50   * @goal spring-descriptor
51   * @phase process-resources
52   * @requiresDependencyResolution test
53   */
54  public class SpringDescriptorMojo extends AbstractContainerMojo
55  {
56  
57      /** Location of the implementation template. */
58      private static final String FACTORY_BEAN_TEMPLATE_LOCATION =
59                                  "META-INF/templates/FactoryBean.java.vm";
60  
61      /**
62       * Name of the spring beans descriptor to generate.
63       *
64       * @parameter expression="${springDescriptor}"
65       *            default-value="${project.build.directory}/classes/META-INF/jdtaus/spring-beans.xml"
66       */
67      private File springDescriptor;
68  
69      /**
70       * Class name of the {@code FactoryBean} implementation to generate.
71       * @parameter expression="${factoryBean}"
72       * @required
73       */
74      private String factoryBean;
75  
76      /**
77       * Source root to create the factory bean class in.
78       * @parameter expression="${sourceRoot}"
79       *            default-value="${project.build.directory}/generated-sources/container"
80       */
81      protected File sourceRoot;
82  
83      /** Creates a new {@code SpringDescriptorMojo} instance. */
84      public SpringDescriptorMojo()
85      {
86          super();
87      }
88  
89      /**
90       * Gets the spring descriptor file to write.
91       *
92       * @return the spring descriptor file to write.
93       */
94      protected File getSpringDescriptorFile()
95      {
96          return this.springDescriptor;
97      }
98  
99      /**
100      * Gets the source root to create new source files in.
101      *
102      * @return the source root to create new source files in.
103      */
104     protected File getSourceRoot()
105     {
106         return this.sourceRoot;
107     }
108 
109     /**
110      * Gets the class name of the {@code FactoryBean} implementation to
111      * generate.
112      *
113      * @return the class name of the {@code FactoryBean} implementation
114      * to generate.
115      */
116     protected String getFactoryBean()
117     {
118         return this.factoryBean;
119     }
120 
121     public void execute() throws MojoExecutionException, MojoFailureException
122     {
123         final ClassLoader mavenLoader =
124                           Thread.currentThread().getContextClassLoader();
125 
126         Writer writer = null;
127         OutputStream out = null;
128 
129         try
130         {
131             final ClassLoader runtimeLoader =
132                               this.getRuntimeClassLoader( mavenLoader );
133 
134             Thread.currentThread().setContextClassLoader( runtimeLoader );
135             enableThreadContextClassLoader();
136 
137             final BeansElement springModel = this.getModelManager().
138                 getSpringModel( this.getFactoryBean(),
139                                 ModelFactory.newModel().getModules() );
140 
141             if ( springModel.getImportOrAliasOrBean().size() > 0 )
142             {
143                 if ( !this.getSpringDescriptorFile().getParentFile().exists() )
144                 {
145                     this.getSpringDescriptorFile().getParentFile().mkdirs();
146                 }
147 
148                 out = new FileOutputStream(
149                     this.getSpringDescriptorFile() );
150 
151                 this.getModelManager().getSpringMarshaller().
152                     marshal( springModel, out );
153 
154                 out.close();
155                 out = null;
156 
157                 this.getLog().info(
158                     SpringDescriptorMojoBundle.getInstance().
159                     getGeneratedDescriptorMessage(
160                     Locale.getDefault(), this.getSpringDescriptorFile().
161                     getCanonicalPath() ) );
162 
163                 final BeanFactory beanFactory = new XmlBeanFactory(
164                     new FileSystemResource( this.getSpringDescriptorFile() ) );
165 
166                 beanFactory.containsBean( "TEST" );
167 
168                 final JavaArtifact artifact =
169                                    new JavaArtifact( this.getFactoryBean() );
170 
171                 final File source =
172                            new File( this.getSourceRoot(),
173                                      artifact.getPackagePath() +
174                                      File.separator + artifact.getName() +
175                                      ".java" );
176 
177                 if ( !source.getParentFile().exists()
178                      && !source.getParentFile().mkdirs() )
179                 {
180                     throw new MojoExecutionException(
181                         SpringDescriptorMojoBundle.getInstance().
182                         getCannotCreateDirectoryMessage(
183                         Locale.getDefault(), source.getParentFile().
184                         getAbsolutePath() ) );
185 
186                 }
187 
188                 if ( this.getEncoding() == null )
189                 {
190                     writer = new FileWriter( source );
191                 }
192                 else
193                 {
194                     writer = new OutputStreamWriter(
195                         new FileOutputStream( source ), this.getEncoding() );
196 
197                 }
198 
199                 final VelocityContext ctx = new VelocityContext();
200                 ctx.put( "artifact", artifact );
201                 ctx.put( "project", this.getMavenProject() );
202 
203                 this.getVelocity().mergeTemplate(
204                     FACTORY_BEAN_TEMPLATE_LOCATION, "UTF-8", ctx, writer );
205 
206                 writer.close();
207                 writer = null;
208 
209                 this.getMavenProject().addCompileSourceRoot(
210                     this.getSourceRoot().getAbsolutePath() );
211 
212                 this.getLog().info(
213                     SpringDescriptorMojoBundle.getInstance().
214                     getGeneratedFactoryBeanMessage(
215                     Locale.getDefault(), source.getCanonicalPath() ) );
216 
217             }
218             else
219             {
220                 this.getLog().info(
221                     SpringDescriptorMojoBundle.getInstance().
222                     getEmptyDescriptorMessage(
223                     Locale.getDefault(),
224                     this.springDescriptor.getCanonicalPath() ) );
225 
226             }
227         }
228         catch ( final ContextError e )
229         {
230             throw new MojoExecutionException( e.getMessage(), e );
231         }
232         catch ( final ContainerError e )
233         {
234             throw new MojoExecutionException( e.getMessage(), e );
235         }
236         catch ( final ModelError e )
237         {
238             throw new MojoExecutionException( e.getMessage(), e );
239         }
240         catch ( final Exception e )
241         {
242             throw new MojoExecutionException( e.getMessage(), e );
243         }
244         finally
245         {
246             disableThreadContextClassLoader();
247             Thread.currentThread().setContextClassLoader( mavenLoader );
248 
249             try
250             {
251                 if ( out != null )
252                 {
253                     out.close();
254                 }
255             }
256             catch ( final IOException e )
257             {
258                 this.getLog().error( e );
259             }
260 
261             try
262             {
263                 if ( writer != null )
264                 {
265                     writer.close();
266                 }
267             }
268             catch ( final IOException e )
269             {
270                 this.getLog().error( e );
271             }
272         }
273     }
274 
275 }