View Javadoc

1   package org.apache.maven.plugin.ear;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A Mojo that generates the EAR deployment descriptor file(s).
35   *
36   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
37   * @version $Id: GenerateApplicationXmlMojo.java 802682 2009-08-10 07:57:43Z mkleint $
38   * @goal generate-application-xml
39   * @phase generate-resources
40   * @requiresDependencyResolution test
41   */
42  public class GenerateApplicationXmlMojo
43      extends AbstractEarMojo
44  {
45  
46  
47  
48      /**
49       * Whether the application.xml should be generated or not.
50       *
51       * @parameter
52       */
53      private Boolean generateApplicationXml = Boolean.TRUE;
54  
55      /**
56       * Display name of the application to be used when application.xml
57       * file is autogenerated.
58       *
59       * @parameter expression="${project.artifactId}"
60       */
61      private String displayName;
62  
63      /**
64       * Description of the application to be used when application.xml
65       * file is autogenerated.
66       *
67       * @parameter expression="${project.description}"
68       */
69      private String description;
70  
71      /**
72       * The security-roles to be added to the auto-generated
73       * application.xml file.
74       *
75       * @parameter
76       */
77      private PlexusConfiguration security;
78  
79  
80      public void execute()
81          throws MojoExecutionException, MojoFailureException
82      {
83          // Initializes ear modules
84          super.execute();
85  
86          // Handle application.xml
87          if ( !generateApplicationXml.booleanValue() )
88          {
89              getLog().debug( "Generation of application.xml is disabled" );
90          }
91          else
92          {
93              // Check version
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             // Generate deployment descriptor and copy it to the build directory
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         // Handle jboss-app.xml
123         if ( getJbossConfiguration() == null )
124         {
125             getLog().debug( "Generation of jboss-app.xml is disabled" );
126             return;
127         }
128         else
129         {
130             // Generate deployment descriptor and copy it to the build directory
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      * Generates the deployment descriptor.
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      * Generates the jboss deployment descriptor.
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      * Builds the security roles based on the configuration.
193      *
194      * @return a list of SecurityRole object(s)
195      * @throws EarPluginException if the configuration is invalid
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 }