Coverage Report - org.apache.maven.plugin.ear.GenerateApplicationXmlMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
GenerateApplicationXmlMojo
0%
0/61
0%
0/20
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 746740 2009-02-22 15:44:32Z snicoll $
 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 ) && !version.equals( VERSION_5 ) )
 95  
             {
 96  0
                 throw new MojoExecutionException( "Invalid version[" + version + "]" );
 97  
             }
 98  
 
 99  
             // Generate deployment descriptor and copy it to the build directory
 100  0
             getLog().info( "Generating application.xml" );
 101  
             try
 102  
             {
 103  0
                 generateStandardDeploymentDescriptor();
 104  
             }
 105  0
             catch ( EarPluginException e )
 106  
             {
 107  0
                 throw new MojoExecutionException( "Failed to generate application.xml", e );
 108  0
             }
 109  
 
 110  
             try
 111  
             {
 112  0
                 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ),
 113  
                                                new File( getWorkDirectory(), "META-INF" ) );
 114  
             }
 115  0
             catch ( IOException e )
 116  
             {
 117  0
                 throw new MojoExecutionException( "Unable to copy application.xml to final destination", e );
 118  0
             }
 119  
         }
 120  
 
 121  
         // Handle jboss-app.xml
 122  0
         if ( getJbossConfiguration() == null )
 123  
         {
 124  0
             getLog().debug( "Generation of jboss-app.xml is disabled" );
 125  0
             return;
 126  
         }
 127  
         else
 128  
         {
 129  
             // Generate deployment descriptor and copy it to the build directory
 130  0
             getLog().info( "Generating jboss-app.xml" );
 131  
             try
 132  
             {
 133  0
                 generateJbossDeploymentDescriptor();
 134  
             }
 135  0
             catch ( EarPluginException e )
 136  
             {
 137  0
                 throw new MojoExecutionException( "Failed to generate jboss-app.xml", e );
 138  0
             }
 139  
 
 140  
             try
 141  
             {
 142  0
                 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ),
 143  
                                                new File( getWorkDirectory(), "META-INF" ) );
 144  
             }
 145  0
             catch ( IOException e )
 146  
             {
 147  0
                 throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e );
 148  0
             }
 149  
         }
 150  0
     }
 151  
 
 152  
     /**
 153  
      * Generates the deployment descriptor.
 154  
      */
 155  
     protected void generateStandardDeploymentDescriptor()
 156  
         throws EarPluginException
 157  
     {
 158  0
         File outputDir = new File( generatedDescriptorLocation );
 159  0
         if ( !outputDir.exists() )
 160  
         {
 161  0
             outputDir.mkdirs();
 162  
         }
 163  
 
 164  0
         File descriptor = new File( outputDir, "application.xml" );
 165  
 
 166  0
         final ApplicationXmlWriter writer = new ApplicationXmlWriter( version, encoding );
 167  0
         final ApplicationXmlWriterContext context = new ApplicationXmlWriterContext(descriptor, getModules(), 
 168  
                 buildSecurityRoles(), displayName, description, defaultLibBundleDir);
 169  0
         writer.write( context );
 170  0
     }
 171  
 
 172  
     /**
 173  
      * Generates the jboss deployment descriptor.
 174  
      */
 175  
     protected void generateJbossDeploymentDescriptor()
 176  
         throws EarPluginException
 177  
     {
 178  0
         File outputDir = new File( generatedDescriptorLocation );
 179  0
         if ( !outputDir.exists() )
 180  
         {
 181  0
             outputDir.mkdirs();
 182  
         }
 183  
 
 184  0
         File descriptor = new File( outputDir, "jboss-app.xml" );
 185  
 
 186  0
         JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding );
 187  0
         writer.write( descriptor, getJbossConfiguration(), getModules() );
 188  0
     }
 189  
 
 190  
     /**
 191  
      * Builds the security roles based on the configuration.
 192  
      *
 193  
      * @return a list of SecurityRole object(s)
 194  
      * @throws EarPluginException if the configuration is invalid
 195  
      */
 196  
     private List buildSecurityRoles()
 197  
         throws EarPluginException
 198  
     {
 199  0
         final List result = new ArrayList();
 200  0
         if ( security == null )
 201  
         {
 202  0
             return result;
 203  
         }
 204  
         try
 205  
         {
 206  0
             final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE );
 207  
 
 208  0
             for ( int i = 0; i < securityRoles.length; i++ )
 209  
             {
 210  0
                 PlexusConfiguration securityRole = securityRoles[i];
 211  0
                 final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE );
 212  0
                 final String roleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue();
 213  0
                 final String roleNameId =
 214  
                     securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE );
 215  0
                 final String description = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue();
 216  0
                 final String descriptionId =
 217  
                     securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE );
 218  
 
 219  0
                 if ( roleName == null )
 220  
                 {
 221  0
                     throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." );
 222  
                 }
 223  
                 else
 224  
                 {
 225  0
                     result.add( new SecurityRole( roleName, roleNameId, id, description, descriptionId ) );
 226  
                 }
 227  
             }
 228  0
             return result;
 229  
         }
 230  0
         catch ( PlexusConfigurationException e )
 231  
         {
 232  0
             throw new EarPluginException( "Invalid security-role configuration", e );
 233  
         }
 234  
 
 235  
     }
 236  
 }