1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
46
47
48
49
50
51
52
53
54 public class SpringDescriptorMojo extends AbstractContainerMojo
55 {
56
57
58 private static final String FACTORY_BEAN_TEMPLATE_LOCATION =
59 "META-INF/templates/FactoryBean.java.vm";
60
61
62
63
64
65
66
67 private File springDescriptor;
68
69
70
71
72
73
74 private String factoryBean;
75
76
77
78
79
80
81 protected File sourceRoot;
82
83
84 public SpringDescriptorMojo()
85 {
86 super();
87 }
88
89
90
91
92
93
94 protected File getSpringDescriptorFile()
95 {
96 return this.springDescriptor;
97 }
98
99
100
101
102
103
104 protected File getSourceRoot()
105 {
106 return this.sourceRoot;
107 }
108
109
110
111
112
113
114
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 }