Coverage Report - org.apache.maven.plugin.ear.GenerateApplicationXmlMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
GenerateApplicationXmlMojo
0%
0/61
0%
0/14
6,25
 
 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.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  
  * Generates the EAR deployment descriptor file(s).
 36  
  *
 37  
  * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
 38  
  * @version $Id: GenerateApplicationXmlMojo.java 1228837 2012-01-08 13:19:38Z rfscholte $
 39  
  * @goal generate-application-xml
 40  
  * @phase generate-resources
 41  
  * @threadSafe
 42  
  * @requiresDependencyResolution test
 43  
  */
 44  0
 public class GenerateApplicationXmlMojo
 45  
     extends AbstractEarMojo
 46  
 {
 47  
 
 48  
 
 49  
     /**
 50  
      * Whether the application.xml should be generated or not.
 51  
      *
 52  
      * @parameter default-value="true"
 53  
      */
 54  0
     private Boolean generateApplicationXml = Boolean.TRUE;
 55  
 
 56  
     /**
 57  
      * Whether a module ID should be generated if none is specified.
 58  
      *
 59  
      * @parameter default-value="false"
 60  
      */
 61  0
     private Boolean generateModuleId = Boolean.FALSE;
 62  
 
 63  
     /**
 64  
      * Application name of the application to be used when the application.xml
 65  
      * file is auto-generated. Since JavaEE6.
 66  
      *
 67  
      * @parameter
 68  
      */
 69  
     private String applicationName;
 70  
 
 71  
     /**
 72  
      * Display name of the application to be used when the application.xml
 73  
      * file is auto-generated.
 74  
      *
 75  
      * @parameter default-value="${project.artifactId}"
 76  
      */
 77  
     private String displayName;
 78  
 
 79  
     /**
 80  
      * Description of the application to be used when the application.xml
 81  
      * file is auto-generated.
 82  
      *
 83  
      * @parameter default-value="${project.description}"
 84  
      */
 85  
     private String description;
 86  
 
 87  
     /**
 88  
      * Defines the value of the initialize in order element to be used when
 89  
      * the application.xml file is auto-generated. When set to true, modules
 90  
      * must be initialized in the order they're listed in this deployment descriptor,
 91  
      * with the exception of application client modules, which can be
 92  
      * initialized in any order. If initialize-in-order is not set or set to
 93  
      * false, the order of initialization is unspecified and may be
 94  
      * product-dependent. Since JavaEE6.
 95  
      *
 96  
      * @parameter
 97  
      */
 98  
     private Boolean initializeInOrder;
 99  
 
 100  
     /**
 101  
      * The security-roles to be added to the auto-generated
 102  
      * application.xml file.
 103  
      *
 104  
      * @parameter
 105  
      */
 106  
     private PlexusConfiguration security;
 107  
 
 108  
 
 109  
     public void execute()
 110  
         throws MojoExecutionException, MojoFailureException
 111  
     {
 112  
         // Initializes ear modules
 113  0
         super.execute();
 114  
 
 115  
         // Handle application.xml
 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  
             // Generate deployment descriptor and copy it to the build directory
 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  
         // Handle jboss-app.xml
 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  
             // Generate deployment descriptor and copy it to the build directory
 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  
      * Generates the deployment descriptor.
 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  
      * Generates the jboss deployment descriptor.
 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  
      * Builds the security roles based on the configuration.
 218  
      *
 219  
      * @return a list of SecurityRole object(s)
 220  
      * @throws EarPluginException if the configuration is invalid
 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  
 }