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 | 0 | 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 | 0 | @Parameter( defaultValue = "true" ) |
61 | |
private Boolean generateApplicationXml = Boolean.TRUE; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | @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 | 0 | super.execute(); |
139 | |
|
140 | |
|
141 | 0 | if ( !generateApplicationXml.booleanValue() ) |
142 | |
{ |
143 | 0 | getLog().debug( "Generation of application.xml is disabled" ); |
144 | |
} |
145 | |
else |
146 | |
{ |
147 | 0 | final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version ); |
148 | |
|
149 | |
|
150 | 0 | getLog().info( "Generating application.xml" ); |
151 | |
try |
152 | |
{ |
153 | 0 | generateStandardDeploymentDescriptor( javaEEVersion ); |
154 | |
} |
155 | 0 | catch ( EarPluginException e ) |
156 | |
{ |
157 | 0 | throw new MojoExecutionException( "Failed to generate application.xml", e ); |
158 | 0 | } |
159 | |
|
160 | |
try |
161 | |
{ |
162 | 0 | FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ), |
163 | |
new File( getWorkDirectory(), "META-INF" ) ); |
164 | |
} |
165 | 0 | catch ( IOException e ) |
166 | |
{ |
167 | 0 | throw new MojoExecutionException( "Unable to copy application.xml to final destination", e ); |
168 | 0 | } |
169 | |
} |
170 | |
|
171 | |
|
172 | 0 | if ( getJbossConfiguration() == null ) |
173 | |
{ |
174 | 0 | getLog().debug( "Generation of jboss-app.xml is disabled" ); |
175 | 0 | return; |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | |
|
180 | 0 | getLog().info( "Generating jboss-app.xml" ); |
181 | |
try |
182 | |
{ |
183 | 0 | generateJbossDeploymentDescriptor(); |
184 | |
} |
185 | 0 | catch ( EarPluginException e ) |
186 | |
{ |
187 | 0 | throw new MojoExecutionException( "Failed to generate jboss-app.xml", e ); |
188 | 0 | } |
189 | |
|
190 | |
try |
191 | |
{ |
192 | 0 | FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ), |
193 | |
new File( getWorkDirectory(), "META-INF" ) ); |
194 | |
} |
195 | 0 | catch ( IOException e ) |
196 | |
{ |
197 | 0 | throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e ); |
198 | 0 | } |
199 | |
} |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected void generateStandardDeploymentDescriptor( JavaEEVersion javaEEVersion ) |
206 | |
throws EarPluginException |
207 | |
{ |
208 | 0 | File outputDir = new File( generatedDescriptorLocation ); |
209 | 0 | if ( !outputDir.exists() ) |
210 | |
{ |
211 | 0 | outputDir.mkdirs(); |
212 | |
} |
213 | |
|
214 | 0 | File descriptor = new File( outputDir, "application.xml" ); |
215 | |
|
216 | 0 | final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId ); |
217 | 0 | final ApplicationXmlWriterContext context = |
218 | |
new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), buildEnvEntries(), |
219 | |
displayName, description, getActualLibraryDirectory(), applicationName, |
220 | |
initializeInOrder ); |
221 | 0 | writer.write( context ); |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
protected void generateJbossDeploymentDescriptor() |
228 | |
throws EarPluginException |
229 | |
{ |
230 | 0 | File outputDir = new File( generatedDescriptorLocation ); |
231 | 0 | if ( !outputDir.exists() ) |
232 | |
{ |
233 | 0 | outputDir.mkdirs(); |
234 | |
} |
235 | |
|
236 | 0 | File descriptor = new File( outputDir, "jboss-app.xml" ); |
237 | |
|
238 | 0 | JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding ); |
239 | 0 | writer.write( descriptor, getJbossConfiguration(), getModules() ); |
240 | 0 | } |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
private List<SecurityRole> buildSecurityRoles() |
249 | |
throws EarPluginException |
250 | |
{ |
251 | 0 | final List<SecurityRole> result = new ArrayList<SecurityRole>(); |
252 | 0 | if ( security == null ) |
253 | |
{ |
254 | 0 | return result; |
255 | |
} |
256 | |
try |
257 | |
{ |
258 | 0 | final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE ); |
259 | |
|
260 | 0 | for ( int i = 0; i < securityRoles.length; i++ ) |
261 | |
{ |
262 | 0 | PlexusConfiguration securityRole = securityRoles[i]; |
263 | 0 | final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE ); |
264 | 0 | final String roleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue(); |
265 | 0 | final String roleNameId = |
266 | |
securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE ); |
267 | 0 | final String description = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue(); |
268 | 0 | final String descriptionId = |
269 | |
securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE ); |
270 | |
|
271 | 0 | if ( roleName == null ) |
272 | |
{ |
273 | 0 | throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." ); |
274 | |
} |
275 | |
else |
276 | |
{ |
277 | 0 | result.add( new SecurityRole( roleName, roleNameId, id, description, descriptionId ) ); |
278 | |
} |
279 | |
} |
280 | 0 | return result; |
281 | |
} |
282 | 0 | catch ( PlexusConfigurationException e ) |
283 | |
{ |
284 | 0 | 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 | 0 | final List<EnvEntry> result = new ArrayList<EnvEntry>(); |
299 | 0 | if ( envEntries == null ) |
300 | |
{ |
301 | 0 | return result; |
302 | |
} |
303 | |
try |
304 | |
{ |
305 | 0 | final PlexusConfiguration[] allEnvEntries = envEntries.getChildren( EnvEntry.ENV_ENTRY ); |
306 | |
|
307 | 0 | for ( PlexusConfiguration envEntry : allEnvEntries ) |
308 | |
{ |
309 | 0 | final String description = envEntry.getChild( EnvEntry.DESCRIPTION ).getValue(); |
310 | 0 | final String envEntryName = envEntry.getChild( EnvEntry.ENV_ENTRY_NAME ).getValue(); |
311 | 0 | final String envEntryType = envEntry.getChild( EnvEntry.ENV_ENTRY_TYPE ).getValue(); |
312 | 0 | final String envEntryValue = envEntry.getChild( EnvEntry.ENV_ENTRY_VALUE ).getValue(); |
313 | |
|
314 | |
try |
315 | |
{ |
316 | 0 | result.add( new EnvEntry( description, envEntryName, envEntryType, envEntryValue ) ); |
317 | |
} |
318 | 0 | catch ( IllegalArgumentException e ) |
319 | |
{ |
320 | 0 | throw new EarPluginException( "Invalid env-entry [" + envEntry + "]", e ); |
321 | 0 | } |
322 | |
} |
323 | 0 | return result; |
324 | |
} |
325 | 0 | catch ( PlexusConfigurationException e ) |
326 | |
{ |
327 | 0 | throw new EarPluginException( "Invalid env-entry configuration", e ); |
328 | |
} |
329 | |
|
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
private String getActualLibraryDirectory() |
336 | |
throws EarPluginException |
337 | |
{ |
338 | 0 | final String mode = libraryDirectoryMode == null ? DEFAULT : libraryDirectoryMode.toUpperCase(); |
339 | |
|
340 | 0 | if ( DEFAULT.equals( mode ) ) |
341 | |
{ |
342 | 0 | return defaultLibBundleDir; |
343 | |
} |
344 | 0 | else if ( EMPTY.equals( mode ) ) |
345 | |
{ |
346 | 0 | return ""; |
347 | |
} |
348 | 0 | else if ( NONE.equals( mode ) ) |
349 | |
{ |
350 | 0 | return null; |
351 | |
} |
352 | |
else |
353 | |
{ |
354 | 0 | throw new EarPluginException( |
355 | |
"Unsupported library directory mode [" + libraryDirectoryMode + "] Supported modes " + |
356 | |
( Arrays.asList( DEFAULT, EMPTY, NONE ) ) ); |
357 | |
} |
358 | |
} |
359 | |
} |