1 package org.apache.maven.plugins.ear;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.plugins.ear.util.JavaEEVersion;
35 import org.codehaus.plexus.configuration.PlexusConfiguration;
36 import org.codehaus.plexus.interpolation.InterpolationException;
37 import org.codehaus.plexus.interpolation.Interpolator;
38 import org.codehaus.plexus.interpolation.MapBasedValueSource;
39 import org.codehaus.plexus.interpolation.StringSearchInterpolator;
40 import org.codehaus.plexus.interpolation.ValueSource;
41 import org.codehaus.plexus.util.FileUtils;
42
43
44
45
46
47
48 @Mojo( name = "generate-application-xml",
49 defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
50 threadSafe = true,
51 requiresDependencyResolution = ResolutionScope.TEST )
52 public class GenerateApplicationXmlMojo
53 extends AbstractEarMojo
54 {
55
56
57
58
59 public static final String DEFAULT = "DEFAULT";
60
61
62
63
64 public static final String EMPTY = "EMPTY";
65
66
67
68
69 public static final String NONE = "NONE";
70
71
72
73
74 @Parameter( defaultValue = "true" )
75 private Boolean generateApplicationXml = Boolean.TRUE;
76
77
78
79
80 @Parameter( defaultValue = "false" )
81 private Boolean generateModuleId = Boolean.FALSE;
82
83
84
85
86 @Parameter
87 private String applicationName;
88
89
90
91
92 @Parameter( defaultValue = "${project.artifactId}" )
93 private String displayName;
94
95
96
97
98 @Parameter( defaultValue = "${project.description}" )
99 private String description;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 @Parameter( defaultValue = DEFAULT )
117 private String libraryDirectoryMode;
118
119
120
121
122
123
124
125 @Parameter
126 private Boolean initializeInOrder;
127
128
129
130
131
132
133 @Parameter
134 private String applicationId;
135
136
137
138
139 @Parameter
140 private PlexusConfiguration security;
141
142
143
144
145 @Parameter( alias = "env-entries" )
146 private PlexusConfiguration envEntries;
147
148
149
150
151 @Parameter( alias = "ejb-refs" )
152 private PlexusConfiguration ejbRefs;
153
154
155
156
157 @Parameter
158 private PlexusConfiguration resourceRefs;
159
160
161
162
163 public void execute()
164 throws MojoExecutionException, MojoFailureException
165 {
166
167 super.execute();
168
169
170 if ( !generateApplicationXml )
171 {
172 getLog().debug( "Generation of application.xml is disabled" );
173 }
174 else
175 {
176 final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
177
178
179 getLog().info( "Generating application.xml" );
180 try
181 {
182 generateStandardDeploymentDescriptor( javaEEVersion );
183 }
184 catch ( EarPluginException e )
185 {
186 throw new MojoExecutionException( "Failed to generate application.xml", e );
187 }
188
189 try
190 {
191 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ),
192 new File( getWorkDirectory(), "META-INF" ) );
193 }
194 catch ( IOException e )
195 {
196 throw new MojoExecutionException( "Unable to copy application.xml to final destination", e );
197 }
198 }
199
200
201 if ( getJbossConfiguration() == null )
202 {
203 getLog().debug( "Generation of jboss-app.xml is disabled" );
204 }
205 else
206 {
207
208 getLog().info( "Generating jboss-app.xml" );
209 try
210 {
211 generateJbossDeploymentDescriptor();
212 }
213 catch ( EarPluginException e )
214 {
215 throw new MojoExecutionException( "Failed to generate jboss-app.xml", e );
216 }
217
218 try
219 {
220 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ),
221 new File( getWorkDirectory(), "META-INF" ) );
222 }
223 catch ( IOException e )
224 {
225 throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e );
226 }
227 }
228 }
229
230
231
232
233
234
235
236 protected void generateStandardDeploymentDescriptor( JavaEEVersion javaEEVersion )
237 throws EarPluginException
238 {
239 File outputDir = new File( generatedDescriptorLocation );
240 if ( !outputDir.exists() )
241 {
242 if ( !outputDir.mkdirs() )
243 {
244 throw new EarPluginException( "Error creating " + outputDir );
245 }
246 }
247
248 File descriptor = new File( outputDir, "application.xml" );
249
250 final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId );
251 final ApplicationXmlWriterContext context =
252 new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), buildEnvEntries(),
253 buildEjbEntries(), buildResourceRefs(), displayName, description,
254 getActualLibraryDirectory(), applicationName,
255 initializeInOrder ).setApplicationId( applicationId );
256 writer.write( context );
257 }
258
259
260
261
262
263
264 protected void generateJbossDeploymentDescriptor()
265 throws EarPluginException
266 {
267 File outputDir = new File( generatedDescriptorLocation );
268 if ( !outputDir.exists() )
269 {
270 if ( !outputDir.mkdirs() )
271 {
272 throw new EarPluginException( "Error creating " + outputDir );
273 }
274 }
275
276 File descriptor = new File( outputDir, "jboss-app.xml" );
277
278 JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding );
279 writer.write( descriptor, getJbossConfiguration(), getModules() );
280 }
281
282
283
284
285
286
287
288 private List<SecurityRole> buildSecurityRoles()
289 throws EarPluginException
290 {
291 final List<SecurityRole> result = new ArrayList<SecurityRole>();
292 if ( security == null )
293 {
294 return result;
295 }
296 final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE );
297
298 for ( PlexusConfiguration securityRole : securityRoles )
299 {
300 final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE );
301 final String childRoleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue();
302 final String childRoleNameId =
303 securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE );
304 final String childDescription = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue();
305 final String childDescriptionId =
306 securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE );
307
308 if ( childRoleName == null )
309 {
310 throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." );
311 }
312 else
313 {
314 result.add( new SecurityRole( childRoleName, childRoleNameId, id, childDescription,
315 childDescriptionId ) );
316 }
317 }
318 return result;
319 }
320
321
322
323
324
325
326
327
328
329
330 private String interpolate( Interpolator interpolator, String element )
331 throws InterpolationException
332 {
333 if ( element == null )
334 {
335 return element;
336 }
337 else
338 {
339 return interpolator.interpolate( element );
340 }
341 }
342
343
344
345
346
347
348
349 private List<EnvEntry> buildEnvEntries()
350 throws EarPluginException
351 {
352 final List<EnvEntry> result = new ArrayList<EnvEntry>();
353 if ( envEntries == null )
354 {
355 return result;
356 }
357 try
358 {
359 StringSearchInterpolator ssi = new StringSearchInterpolator();
360 ValueSource vs = new MapBasedValueSource( project.getProperties() );
361 ssi.addValueSource( vs );
362
363 final PlexusConfiguration[] allEnvEntries = envEntries.getChildren( EnvEntry.ENV_ENTRY );
364
365 getLog().debug( "buildEnvEntries: allEnvEntries size:" + allEnvEntries.length );
366 for ( PlexusConfiguration envEntry : allEnvEntries )
367 {
368 final String childDescription =
369 interpolate( ssi, envEntry.getChild( EnvEntry.DESCRIPTION ).getValue() );
370 final String childEnvEntryName =
371 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_ENTRY_NAME ).getValue() );
372 final String childEnvEntryType =
373 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_ENTRY_TYPE ).getValue() );
374 final String childEnvEntryValue =
375 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_ENTRY_VALUE ).getValue() );
376 final String childEnvLookupNameValue =
377 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_LOOKUP_NAME ).getValue() );
378
379 try
380 {
381 result.add( new EnvEntry( childDescription, childEnvEntryName, childEnvEntryType,
382 childEnvEntryValue, childEnvLookupNameValue ) );
383 }
384 catch ( IllegalArgumentException e )
385 {
386 throw new EarPluginException( "Invalid env-entry [" + envEntry + "]", e );
387 }
388 }
389 return result;
390 }
391 catch ( InterpolationException e )
392 {
393 throw new EarPluginException( "Interpolation exception:", e );
394 }
395
396 }
397
398
399
400
401
402
403
404 private List<EjbRef> buildEjbEntries()
405 throws EarPluginException
406 {
407 final List<EjbRef> result = new ArrayList<EjbRef>();
408 if ( ejbRefs == null )
409 {
410 return result;
411 }
412 try
413 {
414 StringSearchInterpolator ssi = new StringSearchInterpolator();
415 ValueSource vs = new MapBasedValueSource( project.getProperties() );
416 ssi.addValueSource( vs );
417
418 final PlexusConfiguration[] allEjbEntries = ejbRefs.getChildren( EjbRef.EJB_REF );
419
420 for ( PlexusConfiguration ejbEntry : allEjbEntries )
421 {
422 final String childDescription =
423 interpolate( ssi, ejbEntry.getChild( EnvEntry.DESCRIPTION ).getValue() );
424 final String childEjbEntryName = interpolate( ssi, ejbEntry.getChild( EjbRef.EJB_NAME ).getValue() );
425 final String childEjbEntryType = interpolate( ssi, ejbEntry.getChild( EjbRef.EJB_TYPE ).getValue() );
426 final String childEjbLookupNameValue =
427 interpolate( ssi, ejbEntry.getChild( EjbRef.EJB_LOOKUP_NAME ).getValue() );
428
429 try
430 {
431 result.add( new EjbRef( childDescription, childEjbEntryName, childEjbEntryType,
432 childEjbLookupNameValue ) );
433 }
434 catch ( IllegalArgumentException e )
435 {
436 throw new EarPluginException( "Invalid ejb-ref [" + ejbEntry + "]", e );
437 }
438 }
439 return result;
440 }
441 catch ( InterpolationException e )
442 {
443 throw new EarPluginException( "Interpolation exception:", e );
444 }
445
446 }
447
448
449
450
451
452
453
454 private List<ResourceRef> buildResourceRefs()
455 throws EarPluginException
456 {
457 final List<ResourceRef> result = new ArrayList<ResourceRef>();
458 if ( resourceRefs == null )
459 {
460 return result;
461 }
462 try
463 {
464 getLog().debug( "Resources found" );
465 StringSearchInterpolator ssi = new StringSearchInterpolator();
466 ValueSource vs = new MapBasedValueSource( project.getProperties() );
467 ssi.addValueSource( vs );
468
469
470 final PlexusConfiguration[] allResourceRefEntries = resourceRefs.getChildren( "resourceRef" );
471
472 getLog().debug( "allResourceRefEntries length: " + allResourceRefEntries.length );
473 for ( PlexusConfiguration resEntry : allResourceRefEntries )
474 {
475 getLog().debug( "Resources resEntry:" + resEntry.getName() );
476
477 final String childResRefName =
478 interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_REF_NAME ).getValue() );
479 final String childResType =
480 interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_TYPE ).getValue() );
481 final String childResRefAuth =
482 interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_AUTH ).getValue() );
483 final String childResRefLookupName =
484 interpolate( ssi, resEntry.getChild( ResourceRef.LOOKUP_NAME ).getValue() );
485
486 try
487 {
488 result.add(
489 new ResourceRef( childResRefName, childResType, childResRefAuth, childResRefLookupName ) );
490 }
491 catch ( IllegalArgumentException e )
492 {
493 throw new EarPluginException( "Invalid resource-ref [" + resEntry + "]", e );
494 }
495 }
496 return result;
497 }
498 catch ( InterpolationException e )
499 {
500 throw new EarPluginException( "Interpolation exception:", e );
501 }
502
503 }
504
505
506
507
508 private String getActualLibraryDirectory()
509 throws EarPluginException
510 {
511 final String mode = libraryDirectoryMode == null ? DEFAULT : libraryDirectoryMode.toUpperCase();
512
513 if ( DEFAULT.equals( mode ) )
514 {
515 return defaultLibBundleDir;
516 }
517 else if ( EMPTY.equals( mode ) )
518 {
519 return "";
520 }
521 else if ( NONE.equals( mode ) )
522 {
523 return null;
524 }
525 else
526 {
527 throw new EarPluginException( "Unsupported library directory mode [" + libraryDirectoryMode
528 + "] Supported modes " + ( Arrays.asList( DEFAULT, EMPTY, NONE ) ) );
529 }
530 }
531 }