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