1 package org.apache.maven.plugin.ear;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.apache.maven.plugin.ear.util.JavaEEVersion;
25 import org.apache.maven.plugins.annotations.LifecyclePhase;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.plugins.annotations.ResolutionScope;
29 import org.codehaus.plexus.configuration.PlexusConfiguration;
30 import org.codehaus.plexus.configuration.PlexusConfigurationException;
31 import org.codehaus.plexus.util.FileUtils;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38
39
40
41
42
43
44
45 @Mojo( name = "generate-application-xml", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true,
46 requiresDependencyResolution = ResolutionScope.TEST )
47 public class GenerateApplicationXmlMojo
48 extends AbstractEarMojo
49 {
50
51 public static final String DEFAULT = "DEFAULT";
52
53 public static final String EMPTY = "EMPTY";
54
55 public static final String NONE = "NONE";
56
57
58
59
60 @Parameter( defaultValue = "true" )
61 private Boolean generateApplicationXml = Boolean.TRUE;
62
63
64
65
66 @Parameter( defaultValue = "false" )
67 private Boolean generateModuleId = Boolean.FALSE;
68
69
70
71
72
73 @Parameter
74 private String applicationName;
75
76
77
78
79
80 @Parameter( defaultValue = "${project.artifactId}" )
81 private String displayName;
82
83
84
85
86
87 @Parameter( defaultValue = "${project.description}" )
88 private String description;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @Parameter( defaultValue = DEFAULT )
106 private String libraryDirectoryMode;
107
108
109
110
111
112
113
114
115
116
117 @Parameter
118 private Boolean initializeInOrder;
119
120
121
122
123
124 @Parameter
125 private PlexusConfiguration security;
126
127
128
129
130
131 @Parameter( alias = "env-entries" )
132 private PlexusConfiguration envEntries;
133
134 public void execute()
135 throws MojoExecutionException, MojoFailureException
136 {
137
138 super.execute();
139
140
141 if ( !generateApplicationXml.booleanValue() )
142 {
143 getLog().debug( "Generation of application.xml is disabled" );
144 }
145 else
146 {
147 final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
148
149
150 getLog().info( "Generating application.xml" );
151 try
152 {
153 generateStandardDeploymentDescriptor( javaEEVersion );
154 }
155 catch ( EarPluginException e )
156 {
157 throw new MojoExecutionException( "Failed to generate application.xml", e );
158 }
159
160 try
161 {
162 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ),
163 new File( getWorkDirectory(), "META-INF" ) );
164 }
165 catch ( IOException e )
166 {
167 throw new MojoExecutionException( "Unable to copy application.xml to final destination", e );
168 }
169 }
170
171
172 if ( getJbossConfiguration() == null )
173 {
174 getLog().debug( "Generation of jboss-app.xml is disabled" );
175 return;
176 }
177 else
178 {
179
180 getLog().info( "Generating jboss-app.xml" );
181 try
182 {
183 generateJbossDeploymentDescriptor();
184 }
185 catch ( EarPluginException e )
186 {
187 throw new MojoExecutionException( "Failed to generate jboss-app.xml", e );
188 }
189
190 try
191 {
192 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ),
193 new File( getWorkDirectory(), "META-INF" ) );
194 }
195 catch ( IOException e )
196 {
197 throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e );
198 }
199 }
200 }
201
202
203
204
205 protected void generateStandardDeploymentDescriptor( JavaEEVersion javaEEVersion )
206 throws EarPluginException
207 {
208 File outputDir = new File( generatedDescriptorLocation );
209 if ( !outputDir.exists() )
210 {
211 outputDir.mkdirs();
212 }
213
214 File descriptor = new File( outputDir, "application.xml" );
215
216 final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId );
217 final ApplicationXmlWriterContext context =
218 new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), buildEnvEntries(),
219 displayName, description, getActualLibraryDirectory(), applicationName,
220 initializeInOrder );
221 writer.write( context );
222 }
223
224
225
226
227 protected void generateJbossDeploymentDescriptor()
228 throws EarPluginException
229 {
230 File outputDir = new File( generatedDescriptorLocation );
231 if ( !outputDir.exists() )
232 {
233 outputDir.mkdirs();
234 }
235
236 File descriptor = new File( outputDir, "jboss-app.xml" );
237
238 JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding );
239 writer.write( descriptor, getJbossConfiguration(), getModules() );
240 }
241
242
243
244
245
246
247
248 private List<SecurityRole> buildSecurityRoles()
249 throws EarPluginException
250 {
251 final List<SecurityRole> result = new ArrayList<SecurityRole>();
252 if ( security == null )
253 {
254 return result;
255 }
256 try
257 {
258 final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE );
259
260 for ( int i = 0; i < securityRoles.length; i++ )
261 {
262 PlexusConfiguration securityRole = securityRoles[i];
263 final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE );
264 final String roleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue();
265 final String roleNameId =
266 securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE );
267 final String description = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue();
268 final String descriptionId =
269 securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE );
270
271 if ( roleName == null )
272 {
273 throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." );
274 }
275 else
276 {
277 result.add( new SecurityRole( roleName, roleNameId, id, description, descriptionId ) );
278 }
279 }
280 return result;
281 }
282 catch ( PlexusConfigurationException e )
283 {
284 throw new EarPluginException( "Invalid security-role configuration", e );
285 }
286
287 }
288
289
290
291
292
293
294
295 private List<EnvEntry> buildEnvEntries()
296 throws EarPluginException
297 {
298 final List<EnvEntry> result = new ArrayList<EnvEntry>();
299 if ( envEntries == null )
300 {
301 return result;
302 }
303 try
304 {
305 final PlexusConfiguration[] allEnvEntries = envEntries.getChildren( EnvEntry.ENV_ENTRY );
306
307 for ( PlexusConfiguration envEntry : allEnvEntries )
308 {
309 final String description = envEntry.getChild( EnvEntry.DESCRIPTION ).getValue();
310 final String envEntryName = envEntry.getChild( EnvEntry.ENV_ENTRY_NAME ).getValue();
311 final String envEntryType = envEntry.getChild( EnvEntry.ENV_ENTRY_TYPE ).getValue();
312 final String envEntryValue = envEntry.getChild( EnvEntry.ENV_ENTRY_VALUE ).getValue();
313
314 try
315 {
316 result.add( new EnvEntry( description, envEntryName, envEntryType, envEntryValue ) );
317 }
318 catch ( IllegalArgumentException e )
319 {
320 throw new EarPluginException( "Invalid env-entry [" + envEntry + "]", e );
321 }
322 }
323 return result;
324 }
325 catch ( PlexusConfigurationException e )
326 {
327 throw new EarPluginException( "Invalid env-entry configuration", e );
328 }
329
330 }
331
332
333
334
335 private String getActualLibraryDirectory()
336 throws EarPluginException
337 {
338 final String mode = libraryDirectoryMode == null ? DEFAULT : libraryDirectoryMode.toUpperCase();
339
340 if ( DEFAULT.equals( mode ) )
341 {
342 return defaultLibBundleDir;
343 }
344 else if ( EMPTY.equals( mode ) )
345 {
346 return "";
347 }
348 else if ( NONE.equals( mode ) )
349 {
350 return null;
351 }
352 else
353 {
354 throw new EarPluginException(
355 "Unsupported library directory mode [" + libraryDirectoryMode + "] Supported modes " +
356 ( Arrays.asList( DEFAULT, EMPTY, NONE ) ) );
357 }
358 }
359 }