Coverage Report - org.apache.maven.plugin.ear.AbstractEarMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEarMojo
0%
0/93
0%
0/34
4.714
 
 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.artifact.Artifact;
 23  
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 24  
 import org.apache.maven.plugin.AbstractMojo;
 25  
 import org.apache.maven.plugin.MojoExecutionException;
 26  
 import org.apache.maven.plugin.MojoFailureException;
 27  
 import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService;
 28  
 import org.apache.maven.plugin.ear.util.JavaEEVersion;
 29  
 import org.apache.maven.project.MavenProject;
 30  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 31  
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
 32  
 
 33  
 import java.io.File;
 34  
 import java.util.ArrayList;
 35  
 import java.util.Iterator;
 36  
 import java.util.List;
 37  
 import java.util.Set;
 38  
 
 39  
 /**
 40  
  * A base class for EAR-processing related tasks.
 41  
  *
 42  
  * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
 43  
  * @version $Id: AbstractEarMojo.java 1050900 2010-12-19 17:10:52Z snicoll $
 44  
  */
 45  0
 public abstract class AbstractEarMojo
 46  
     extends AbstractMojo
 47  
 {
 48  
     public static final String APPLICATION_XML_URI = "META-INF/application.xml";
 49  
 
 50  
     public static final String META_INF = "META-INF";
 51  
 
 52  
     public static final String UTF_8 = "UTF-8";
 53  
 
 54  
     /**
 55  
      * The version of the application.xml to generate. Valid values
 56  
      * are 1.3, 1.4, 5 and 6.
 57  
      *
 58  
      * @parameter default-value="1.3"
 59  
      */
 60  
     protected String version;
 61  
 
 62  
     /**
 63  
      * Character encoding for the auto-generated deployment file(s).
 64  
      *
 65  
      * @parameter default-value="UTF-8"
 66  
      */
 67  
     protected String encoding;
 68  
 
 69  
     /**
 70  
      * Directory where the deployment descriptor file(s) will be auto-generated.
 71  
      *
 72  
      * @parameter default-value="${project.build.directory}"
 73  
      */
 74  
     protected String generatedDescriptorLocation;
 75  
 
 76  
     /**
 77  
      * The maven project.
 78  
      *
 79  
      * @parameter default-value="${project}"
 80  
      * @required
 81  
      * @readonly
 82  
      */
 83  
     protected MavenProject project;
 84  
 
 85  
     /**
 86  
      * The ear modules configuration.
 87  
      *
 88  
      * @parameter
 89  
      */
 90  
     private EarModule[] modules;
 91  
 
 92  
     /**
 93  
      * The artifact type mappings.
 94  
      *
 95  
      * @parameter
 96  
      */
 97  
     protected PlexusConfiguration artifactTypeMappings;
 98  
 
 99  
     /**
 100  
      * The default bundle dir for libraries.
 101  
      *
 102  
      * @parameter alias="defaultJavaBundleDir"
 103  
      */
 104  
     protected String defaultLibBundleDir;
 105  
 
 106  
     /**
 107  
      * Should libraries be added in application.xml
 108  
      *
 109  
      * @parameter default-value="false"
 110  
      */
 111  0
     private Boolean includeLibInApplicationXml = Boolean.FALSE;
 112  
 
 113  
     /**
 114  
      * The file name mapping to use for all dependencies included
 115  
      * in the EAR file.
 116  
      *
 117  
      * @parameter
 118  
      */
 119  
     private String fileNameMapping;
 120  
 
 121  
     /**
 122  
      * Directory that resources are copied to during the build.
 123  
      *
 124  
      * @parameter default-value="${project.build.directory}/${project.build.finalName}"
 125  
      * @required
 126  
      */
 127  
     private File workDirectory;
 128  
 
 129  
     /**
 130  
      * The JBoss specific configuration.
 131  
      *
 132  
      * @parameter
 133  
      */
 134  
     private PlexusConfiguration jboss;
 135  
 
 136  
     /**
 137  
      * The id to use to define the main artifact (e.g. the artifact without
 138  
      * a classifier) when there is multiple candidates.
 139  
      *
 140  
      * @parameter
 141  
      */
 142  0
     private String mainArtifactId = "none";
 143  
 
 144  
     private List earModules;
 145  
 
 146  
     private List allModules;
 147  
 
 148  
     private JbossConfiguration jbossConfiguration;
 149  
 
 150  
     public void execute()
 151  
         throws MojoExecutionException, MojoFailureException
 152  
     {
 153  0
         final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
 154  0
         getLog().debug( "Resolving artifact type mappings ..." );
 155  
         ArtifactTypeMappingService typeMappingService;
 156  
         try
 157  
         {
 158  0
             typeMappingService = new ArtifactTypeMappingService();
 159  0
             typeMappingService.configure( artifactTypeMappings );
 160  
         }
 161  0
         catch ( EarPluginException e )
 162  
         {
 163  0
             throw new MojoExecutionException( "Failed to initialize artifact type mappings", e );
 164  
         }
 165  0
         catch ( PlexusConfigurationException e )
 166  
         {
 167  0
             throw new MojoExecutionException( "Invalid artifact type mappings configuration", e );
 168  0
         }
 169  
 
 170  0
         getLog().debug( "Initializing JBoss configuration if necessary ..." );
 171  
         try
 172  
         {
 173  0
             initializeJbossConfiguration();
 174  
         }
 175  0
         catch ( EarPluginException e )
 176  
         {
 177  0
             throw new MojoExecutionException( "Failed to initialize JBoss configuration", e );
 178  0
         }
 179  
 
 180  0
         getLog().debug( "Initializing ear execution context" );
 181  0
         EarExecutionContext earExecutionContext =
 182  
             new EarExecutionContext( project, mainArtifactId, defaultLibBundleDir, jbossConfiguration, fileNameMapping,
 183  
                                      typeMappingService );
 184  
 
 185  0
         getLog().debug( "Resolving ear modules ..." );
 186  0
         allModules = new ArrayList();
 187  
         try
 188  
         {
 189  0
             if ( modules != null && modules.length > 0 )
 190  
             {
 191  
                 // Let's validate user-defined modules
 192  0
                 EarModule module = null;
 193  
 
 194  0
                 for ( int i = 0; i < modules.length; i++ )
 195  
                 {
 196  0
                     module = modules[i];
 197  0
                     getLog().debug( "Resolving ear module[" + module + "]" );
 198  0
                     module.setEarExecutionContext( earExecutionContext );
 199  0
                     module.resolveArtifact( project.getArtifacts() );
 200  0
                     allModules.add( module );
 201  
                 }
 202  
             }
 203  
 
 204  
             // Let's add other modules
 205  0
             Set artifacts = project.getArtifacts();
 206  0
             for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
 207  
             {
 208  0
                 Artifact artifact = (Artifact) iter.next();
 209  
 
 210  
                 // If the artifact's type is POM, ignore and continue
 211  
                 // since it's used for transitive deps only.
 212  0
                 if ( "pom".equals( artifact.getType() ) )
 213  
                 {
 214  0
                     continue;
 215  
                 }
 216  
 
 217  
                 // Artifact is not yet registered and it has neither test, nor a
 218  
                 // provided scope, not is it optional
 219  0
                 ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
 220  0
                 if ( !isArtifactRegistered( artifact, allModules ) && !artifact.isOptional() &&
 221  
                     filter.include( artifact ) )
 222  
                 {
 223  0
                     EarModule module = EarModuleFactory.newEarModule( artifact, javaEEVersion, defaultLibBundleDir,
 224  
                                                                       includeLibInApplicationXml, typeMappingService );
 225  0
                     module.setEarExecutionContext( earExecutionContext );
 226  0
                     allModules.add( module );
 227  
                 }
 228  0
             }
 229  
         }
 230  0
         catch ( EarPluginException e )
 231  
         {
 232  0
             throw new MojoExecutionException( "Failed to initialize ear modules", e );
 233  0
         }
 234  
 
 235  
         // Now we have everything let's built modules which have not been excluded
 236  0
         earModules = new ArrayList();
 237  0
         for ( Iterator iter = allModules.iterator(); iter.hasNext(); )
 238  
         {
 239  0
             EarModule earModule = (EarModule) iter.next();
 240  0
             if ( earModule.isExcluded() )
 241  
             {
 242  0
                 getLog().debug( "Skipping ear module[" + earModule + "]" );
 243  
             }
 244  
             else
 245  
             {
 246  0
                 earModules.add( earModule );
 247  
             }
 248  0
         }
 249  
 
 250  0
     }
 251  
 
 252  
     protected List getModules()
 253  
     {
 254  0
         if ( earModules == null )
 255  
         {
 256  0
             throw new IllegalStateException( "Ear modules have not been initialized" );
 257  
         }
 258  0
         return earModules;
 259  
     }
 260  
 
 261  
     protected MavenProject getProject()
 262  
     {
 263  0
         return project;
 264  
     }
 265  
 
 266  
     protected File getWorkDirectory()
 267  
     {
 268  0
         return workDirectory;
 269  
     }
 270  
 
 271  
     protected JbossConfiguration getJbossConfiguration()
 272  
     {
 273  0
         return jbossConfiguration;
 274  
     }
 275  
 
 276  
     private static boolean isArtifactRegistered( Artifact a, List currentList )
 277  
     {
 278  0
         Iterator i = currentList.iterator();
 279  0
         while ( i.hasNext() )
 280  
         {
 281  0
             EarModule em = (EarModule) i.next();
 282  0
             if ( em.getArtifact().equals( a ) )
 283  
             {
 284  0
                 return true;
 285  
             }
 286  0
         }
 287  0
         return false;
 288  
     }
 289  
 
 290  
     /**
 291  
      * Initializes the JBoss configuration.
 292  
      *
 293  
      * @throws EarPluginException if the configuration is invalid
 294  
      */
 295  
     private void initializeJbossConfiguration()
 296  
         throws EarPluginException
 297  
     {
 298  0
         if ( jboss == null )
 299  
         {
 300  0
             jbossConfiguration = null;
 301  
         }
 302  
         else
 303  
         {
 304  
             try
 305  
             {
 306  0
                 String version = jboss.getChild( JbossConfiguration.VERSION ).getValue();
 307  0
                 if ( version == null )
 308  
                 {
 309  0
                     getLog().info( "JBoss version not set, using JBoss 4 by default" );
 310  0
                     version = JbossConfiguration.VERSION_4;
 311  
                 }
 312  0
                 final String securityDomain = jboss.getChild( JbossConfiguration.SECURITY_DOMAIN ).getValue();
 313  0
                 final String unauthenticatedPrincipal =
 314  
                     jboss.getChild( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL ).getValue();
 315  
 
 316  0
                 final PlexusConfiguration loaderRepositoryEl = jboss.getChild( JbossConfiguration.LOADER_REPOSITORY );
 317  0
                 final String loaderRepository = loaderRepositoryEl.getValue();
 318  0
                 final String loaderRepositoryClass =
 319  
                     loaderRepositoryEl.getAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE );
 320  0
                 final PlexusConfiguration loaderRepositoryConfigEl =
 321  
                     jboss.getChild( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
 322  0
                 final String loaderRepositoryConfig = loaderRepositoryConfigEl.getValue();
 323  0
                 final String configParserClass =
 324  
                     loaderRepositoryConfigEl.getAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE );
 325  
 
 326  0
                 final String jmxName = jboss.getChild( JbossConfiguration.JMX_NAME ).getValue();
 327  0
                 final String moduleOrder = jboss.getChild( JbossConfiguration.MODULE_ORDER ).getValue();
 328  
 
 329  0
                 final List dataSources = new ArrayList();
 330  0
                 final PlexusConfiguration dataSourcesEl = jboss.getChild( JbossConfiguration.DATASOURCES );
 331  0
                 if ( dataSourcesEl != null )
 332  
                 {
 333  
 
 334  0
                     final PlexusConfiguration[] dataSourcesConfig =
 335  
                         dataSourcesEl.getChildren( JbossConfiguration.DATASOURCE );
 336  0
                     for ( int i = 0; i < dataSourcesConfig.length; i++ )
 337  
                     {
 338  0
                         PlexusConfiguration dataSourceConfig = dataSourcesConfig[i];
 339  0
                         dataSources.add( dataSourceConfig.getValue() );
 340  
 
 341  
                     }
 342  
                 }
 343  0
                 final String libraryDirectory = jboss.getChild( JbossConfiguration.LIBRARY_DIRECTORY ).getValue();
 344  0
                 jbossConfiguration = new JbossConfiguration( version, securityDomain, unauthenticatedPrincipal, jmxName,
 345  
                                                              loaderRepository, moduleOrder, dataSources,
 346  
                                                              libraryDirectory, loaderRepositoryConfig,
 347  
                                                              loaderRepositoryClass, configParserClass );
 348  
             }
 349  0
             catch ( PlexusConfigurationException e )
 350  
             {
 351  0
                 throw new EarPluginException( "Invalid JBoss configuration", e );
 352  0
             }
 353  
         }
 354  0
     }
 355  
 }