Coverage Report - org.apache.maven.plugin.ear.GenerateApplicationXmlMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
GenerateApplicationXmlMojo
0%
0/61
0%
0/22
6.75
 
 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  0
 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  0
     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  0
         super.execute();
 85  
 
 86  
         // Handle application.xml
 87  0
         if ( !generateApplicationXml.booleanValue() )
 88  
         {
 89  0
             getLog().debug( "Generation of application.xml is disabled" );
 90  
         }
 91  
         else
 92  
         {
 93  
             // Check version
 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  
             // Generate deployment descriptor and copy it to the build directory
 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  
         // Handle jboss-app.xml
 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  
             // Generate deployment descriptor and copy it to the build directory
 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  
      * Generates the deployment descriptor.
 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  
      * Generates the jboss deployment descriptor.
 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  
      * 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  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  
 }