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.codehaus.plexus.configuration.PlexusConfiguration;
25 import org.codehaus.plexus.configuration.PlexusConfigurationException;
26 import org.codehaus.plexus.util.FileUtils;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34
35
36
37
38
39
40
41
42 public class GenerateApplicationXmlMojo
43 extends AbstractEarMojo
44 {
45
46
47
48
49
50
51
52
53 private Boolean generateApplicationXml = Boolean.TRUE;
54
55
56
57
58
59
60
61 private String displayName;
62
63
64
65
66
67
68
69 private String description;
70
71
72
73
74
75
76
77 private PlexusConfiguration security;
78
79
80 public void execute()
81 throws MojoExecutionException, MojoFailureException
82 {
83
84 super.execute();
85
86
87 if ( !generateApplicationXml.booleanValue() )
88 {
89 getLog().debug( "Generation of application.xml is disabled" );
90 }
91 else
92 {
93
94 if ( !version.equals( VERSION_1_3 ) && !version.equals( VERSION_1_4 ) &&
95 !version.equals( VERSION_5 ) && !version.equals( VERSION_6 ) )
96 {
97 throw new MojoExecutionException( "Invalid version[" + version + "]" );
98 }
99
100
101 getLog().info( "Generating application.xml" );
102 try
103 {
104 generateStandardDeploymentDescriptor();
105 }
106 catch ( EarPluginException e )
107 {
108 throw new MojoExecutionException( "Failed to generate application.xml", e );
109 }
110
111 try
112 {
113 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ),
114 new File( getWorkDirectory(), "META-INF" ) );
115 }
116 catch ( IOException e )
117 {
118 throw new MojoExecutionException( "Unable to copy application.xml to final destination", e );
119 }
120 }
121
122
123 if ( getJbossConfiguration() == null )
124 {
125 getLog().debug( "Generation of jboss-app.xml is disabled" );
126 return;
127 }
128 else
129 {
130
131 getLog().info( "Generating jboss-app.xml" );
132 try
133 {
134 generateJbossDeploymentDescriptor();
135 }
136 catch ( EarPluginException e )
137 {
138 throw new MojoExecutionException( "Failed to generate jboss-app.xml", e );
139 }
140
141 try
142 {
143 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ),
144 new File( getWorkDirectory(), "META-INF" ) );
145 }
146 catch ( IOException e )
147 {
148 throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e );
149 }
150 }
151 }
152
153
154
155
156 protected void generateStandardDeploymentDescriptor()
157 throws EarPluginException
158 {
159 File outputDir = new File( generatedDescriptorLocation );
160 if ( !outputDir.exists() )
161 {
162 outputDir.mkdirs();
163 }
164
165 File descriptor = new File( outputDir, "application.xml" );
166
167 final ApplicationXmlWriter writer = new ApplicationXmlWriter( version, encoding );
168 final ApplicationXmlWriterContext context = new ApplicationXmlWriterContext(descriptor, getModules(),
169 buildSecurityRoles(), displayName, description, defaultLibBundleDir);
170 writer.write( context );
171 }
172
173
174
175
176 protected void generateJbossDeploymentDescriptor()
177 throws EarPluginException
178 {
179 File outputDir = new File( generatedDescriptorLocation );
180 if ( !outputDir.exists() )
181 {
182 outputDir.mkdirs();
183 }
184
185 File descriptor = new File( outputDir, "jboss-app.xml" );
186
187 JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding );
188 writer.write( descriptor, getJbossConfiguration(), getModules() );
189 }
190
191
192
193
194
195
196
197 private List buildSecurityRoles()
198 throws EarPluginException
199 {
200 final List result = new ArrayList();
201 if ( security == null )
202 {
203 return result;
204 }
205 try
206 {
207 final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE );
208
209 for ( int i = 0; i < securityRoles.length; i++ )
210 {
211 PlexusConfiguration securityRole = securityRoles[i];
212 final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE );
213 final String roleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue();
214 final String roleNameId =
215 securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE );
216 final String description = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue();
217 final String descriptionId =
218 securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE );
219
220 if ( roleName == null )
221 {
222 throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." );
223 }
224 else
225 {
226 result.add( new SecurityRole( roleName, roleNameId, id, description, descriptionId ) );
227 }
228 }
229 return result;
230 }
231 catch ( PlexusConfigurationException e )
232 {
233 throw new EarPluginException( "Invalid security-role configuration", e );
234 }
235
236 }
237 }