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  public class GenerateApplicationXmlMojo
45      extends AbstractEarMojo
46  {
47  
48  
49      
50  
51  
52  
53  
54      private Boolean generateApplicationXml = Boolean.TRUE;
55  
56      
57  
58  
59  
60  
61      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         super.execute();
114 
115         
116         if ( !generateApplicationXml.booleanValue() )
117         {
118             getLog().debug( "Generation of application.xml is disabled" );
119         }
120         else
121         {
122             final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
123 
124             
125             getLog().info( "Generating application.xml" );
126             try
127             {
128                 generateStandardDeploymentDescriptor( javaEEVersion );
129             }
130             catch ( EarPluginException e )
131             {
132                 throw new MojoExecutionException( "Failed to generate application.xml", e );
133             }
134 
135             try
136             {
137                 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ),
138                                                new File( getWorkDirectory(), "META-INF" ) );
139             }
140             catch ( IOException e )
141             {
142                 throw new MojoExecutionException( "Unable to copy application.xml to final destination", e );
143             }
144         }
145 
146         
147         if ( getJbossConfiguration() == null )
148         {
149             getLog().debug( "Generation of jboss-app.xml is disabled" );
150             return;
151         }
152         else
153         {
154             
155             getLog().info( "Generating jboss-app.xml" );
156             try
157             {
158                 generateJbossDeploymentDescriptor();
159             }
160             catch ( EarPluginException e )
161             {
162                 throw new MojoExecutionException( "Failed to generate jboss-app.xml", e );
163             }
164 
165             try
166             {
167                 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ),
168                                                new File( getWorkDirectory(), "META-INF" ) );
169             }
170             catch ( IOException e )
171             {
172                 throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e );
173             }
174         }
175     }
176 
177     
178 
179 
180     protected void generateStandardDeploymentDescriptor( JavaEEVersion javaEEVersion )
181         throws EarPluginException
182     {
183         File outputDir = new File( generatedDescriptorLocation );
184         if ( !outputDir.exists() )
185         {
186             outputDir.mkdirs();
187         }
188 
189         File descriptor = new File( outputDir, "application.xml" );
190 
191         final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId );
192         final ApplicationXmlWriterContext context =
193             new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), displayName, description,
194                                              defaultLibBundleDir, applicationName, initializeInOrder );
195         writer.write( context );
196     }
197 
198     
199 
200 
201     protected void generateJbossDeploymentDescriptor()
202         throws EarPluginException
203     {
204         File outputDir = new File( generatedDescriptorLocation );
205         if ( !outputDir.exists() )
206         {
207             outputDir.mkdirs();
208         }
209 
210         File descriptor = new File( outputDir, "jboss-app.xml" );
211 
212         JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding );
213         writer.write( descriptor, getJbossConfiguration(), getModules() );
214     }
215 
216     
217 
218 
219 
220 
221 
222     private List buildSecurityRoles()
223         throws EarPluginException
224     {
225         final List result = new ArrayList();
226         if ( security == null )
227         {
228             return result;
229         }
230         try
231         {
232             final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE );
233 
234             for ( int i = 0; i < securityRoles.length; i++ )
235             {
236                 PlexusConfiguration securityRole = securityRoles[i];
237                 final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE );
238                 final String roleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue();
239                 final String roleNameId =
240                     securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE );
241                 final String description = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue();
242                 final String descriptionId =
243                     securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE );
244 
245                 if ( roleName == null )
246                 {
247                     throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." );
248                 }
249                 else
250                 {
251                     result.add( new SecurityRole( roleName, roleNameId, id, description, descriptionId ) );
252                 }
253             }
254             return result;
255         }
256         catch ( PlexusConfigurationException e )
257         {
258             throw new EarPluginException( "Invalid security-role configuration", e );
259         }
260 
261     }
262 }