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