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:" + allEnvEntries );
366 getLog().debug( "buildEnvEntries: allEnvEntries size:" + allEnvEntries.length );
367 for ( PlexusConfiguration envEntry : allEnvEntries )
368 {
369 final String childDescription =
370 interpolate( ssi, envEntry.getChild( EnvEntry.DESCRIPTION ).getValue() );
371 final String childEnvEntryName =
372 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_ENTRY_NAME ).getValue() );
373 final String childEnvEntryType =
374 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_ENTRY_TYPE ).getValue() );
375 final String childEnvEntryValue =
376 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_ENTRY_VALUE ).getValue() );
377 final String childEnvLookupNameValue =
378 interpolate( ssi, envEntry.getChild( EnvEntry.ENV_LOOKUP_NAME ).getValue() );
379
380 try
381 {
382 result.add( new EnvEntry( childDescription, childEnvEntryName, childEnvEntryType,
383 childEnvEntryValue, childEnvLookupNameValue ) );
384 }
385 catch ( IllegalArgumentException e )
386 {
387 throw new EarPluginException( "Invalid env-entry [" + envEntry + "]", e );
388 }
389 }
390 return result;
391 }
392 catch ( InterpolationException e )
393 {
394 throw new EarPluginException( "Interpolation exception:", e );
395 }
396
397 }
398
399
400
401
402
403
404
405 private List<EjbRef> buildEjbEntries()
406 throws EarPluginException
407 {
408 final List<EjbRef> result = new ArrayList<EjbRef>();
409 if ( ejbRefs == null )
410 {
411 return result;
412 }
413 try
414 {
415 StringSearchInterpolator ssi = new StringSearchInterpolator();
416 ValueSource vs = new MapBasedValueSource( project.getProperties() );
417 ssi.addValueSource( vs );
418
419 final PlexusConfiguration[] allEjbEntries = ejbRefs.getChildren( EjbRef.EJB_REF );
420
421 for ( PlexusConfiguration ejbEntry : allEjbEntries )
422 {
423 final String childDescription =
424 interpolate( ssi, ejbEntry.getChild( EnvEntry.DESCRIPTION ).getValue() );
425 final String childEjbEntryName = interpolate( ssi, ejbEntry.getChild( EjbRef.EJB_NAME ).getValue() );
426 final String childEjbEntryType = interpolate( ssi, ejbEntry.getChild( EjbRef.EJB_TYPE ).getValue() );
427 final String childEjbLookupNameValue =
428 interpolate( ssi, ejbEntry.getChild( EjbRef.EJB_LOOKUP_NAME ).getValue() );
429
430 try
431 {
432 result.add( new EjbRef( childDescription, childEjbEntryName, childEjbEntryType,
433 childEjbLookupNameValue ) );
434 }
435 catch ( IllegalArgumentException e )
436 {
437 throw new EarPluginException( "Invalid ejb-ref [" + ejbEntry + "]", e );
438 }
439 }
440 return result;
441 }
442 catch ( InterpolationException e )
443 {
444 throw new EarPluginException( "Interpolation exception:", e );
445 }
446
447 }
448
449
450
451
452
453
454
455 private List<ResourceRef> buildResourceRefs()
456 throws EarPluginException
457 {
458 final List<ResourceRef> result = new ArrayList<ResourceRef>();
459 if ( resourceRefs == null )
460 {
461 return result;
462 }
463 try
464 {
465 getLog().debug( "Resources found" );
466 StringSearchInterpolator ssi = new StringSearchInterpolator();
467 ValueSource vs = new MapBasedValueSource( project.getProperties() );
468 ssi.addValueSource( vs );
469
470
471 final PlexusConfiguration[] allResourceRefEntries = resourceRefs.getChildren( "resourceRef" );
472
473 getLog().debug( "allResourceRefEntries: " + allResourceRefEntries );
474 getLog().debug( "allResourceRefEntries length: " + allResourceRefEntries.length );
475 for ( PlexusConfiguration resEntry : allResourceRefEntries )
476 {
477 getLog().debug( "Resources resEntry:" + resEntry.getName() );
478
479 final String childResRefName =
480 interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_REF_NAME ).getValue() );
481 final String childResType =
482 interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_TYPE ).getValue() );
483 final String childResRefAuth =
484 interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_AUTH ).getValue() );
485 final String childResRefLookupName =
486 interpolate( ssi, resEntry.getChild( ResourceRef.LOOKUP_NAME ).getValue() );
487
488 try
489 {
490 result.add(
491 new ResourceRef( childResRefName, childResType, childResRefAuth, childResRefLookupName ) );
492 }
493 catch ( IllegalArgumentException e )
494 {
495 throw new EarPluginException( "Invalid resource-ref [" + resEntry + "]", e );
496 }
497 }
498 return result;
499 }
500 catch ( InterpolationException e )
501 {
502 throw new EarPluginException( "Interpolation exception:", e );
503 }
504
505 }
506
507
508
509
510 private String getActualLibraryDirectory()
511 throws EarPluginException
512 {
513 final String mode = libraryDirectoryMode == null ? DEFAULT : libraryDirectoryMode.toUpperCase();
514
515 if ( DEFAULT.equals( mode ) )
516 {
517 return defaultLibBundleDir;
518 }
519 else if ( EMPTY.equals( mode ) )
520 {
521 return "";
522 }
523 else if ( NONE.equals( mode ) )
524 {
525 return null;
526 }
527 else
528 {
529 throw new EarPluginException( "Unsupported library directory mode [" + libraryDirectoryMode
530 + "] Supported modes " + ( Arrays.asList( DEFAULT, EMPTY, NONE ) ) );
531 }
532 }
533 }