Coverage Report - org.apache.maven.extension.DefaultExtensionManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultExtensionManager
0 %
0/88
0 %
0/28
4,143
DefaultExtensionManager$ProjectArtifactExceptionFilter
0 %
0/6
0 %
0/4
4,143
 
 1  
 package org.apache.maven.extension;
 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.MavenArtifactFilterManager;
 23  
 import org.apache.maven.artifact.Artifact;
 24  
 import org.apache.maven.artifact.ArtifactUtils;
 25  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 26  
 import org.apache.maven.artifact.manager.WagonManager;
 27  
 import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
 28  
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 29  
 import org.apache.maven.artifact.metadata.ResolutionGroup;
 30  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 31  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 32  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 33  
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 34  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 35  
 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 36  
 import org.apache.maven.model.Extension;
 37  
 import org.apache.maven.plugin.DefaultPluginManager;
 38  
 import org.apache.maven.project.MavenProject;
 39  
 import org.apache.maven.wagon.Wagon;
 40  
 import org.codehaus.classworlds.ClassRealm;
 41  
 import org.codehaus.classworlds.ClassWorld;
 42  
 import org.codehaus.classworlds.DuplicateRealmException;
 43  
 import org.codehaus.classworlds.NoSuchRealmException;
 44  
 import org.codehaus.plexus.DefaultPlexusContainer;
 45  
 import org.codehaus.plexus.PlexusConstants;
 46  
 import org.codehaus.plexus.PlexusContainer;
 47  
 import org.codehaus.plexus.PlexusContainerException;
 48  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 49  
 import org.codehaus.plexus.context.Context;
 50  
 import org.codehaus.plexus.context.ContextException;
 51  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 52  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
 53  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 54  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 55  
 
 56  
 import java.io.File;
 57  
 import java.io.InputStream;
 58  
 import java.io.InputStreamReader;
 59  
 import java.util.Collections;
 60  
 import java.util.Iterator;
 61  
 import java.util.LinkedHashSet;
 62  
 import java.util.Map;
 63  
 import java.util.Set;
 64  
 import java.util.jar.JarFile;
 65  
 
 66  
 /**
 67  
  * Used to locate extensions.
 68  
  *
 69  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 70  
  * @author Jason van Zyl
 71  
  * @version $Id: DefaultExtensionManager.java 770474 2009-04-30 22:30:10Z jdcasey $
 72  
  */
 73  0
 public class DefaultExtensionManager
 74  
     extends AbstractLogEnabled
 75  
     implements ExtensionManager, Contextualizable
 76  
 {
 77  
     private ArtifactResolver artifactResolver;
 78  
 
 79  
     private ArtifactFactory artifactFactory;
 80  
 
 81  
     private ArtifactMetadataSource artifactMetadataSource;
 82  
 
 83  
     private DefaultPlexusContainer container;
 84  
 
 85  0
     private ArtifactFilter artifactFilter = MavenArtifactFilterManager.createExtensionFilter();
 86  
 
 87  
     private WagonManager wagonManager;
 88  
 
 89  
     private PlexusContainer extensionContainer;
 90  
 
 91  
     private static final String CONTAINER_NAME = "extensions";
 92  
 
 93  
     public void addExtension( Extension extension,
 94  
                               MavenProject project,
 95  
                               ArtifactRepository localRepository )
 96  
         throws ArtifactResolutionException, PlexusContainerException, ArtifactNotFoundException
 97  
     {
 98  0
         String extensionId = ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() );
 99  
 
 100  0
         getLogger().debug( "Initialising extension: " + extensionId );
 101  
 
 102  0
         Artifact artifact = (Artifact) project.getExtensionArtifactMap().get( extensionId );
 103  
 
 104  0
         if ( artifact != null )
 105  
         {
 106  0
             ArtifactFilter filter = new ProjectArtifactExceptionFilter( artifactFilter, project.getArtifact() );
 107  
 
 108  
             ResolutionGroup resolutionGroup;
 109  
             try
 110  
             {
 111  0
                 resolutionGroup = artifactMetadataSource.retrieve( artifact, localRepository,
 112  
                                                                    project.getRemoteArtifactRepositories() );
 113  
             }
 114  0
             catch ( ArtifactMetadataRetrievalException e )
 115  
             {
 116  0
                 throw new ArtifactResolutionException( "Unable to download metadata from repository for plugin '" +
 117  
                     artifact.getId() + "': " + e.getMessage(), artifact, e );
 118  0
             }
 119  
 
 120  
             // We use the same hack here to make sure that plexus 1.1 is available for extensions that do
 121  
             // not declare plexus-utils but need it. MNG-2900
 122  0
             Set rgArtifacts = resolutionGroup.getArtifacts();
 123  0
             rgArtifacts = DefaultPluginManager.checkPlexusUtils( rgArtifacts, artifactFactory );
 124  
 
 125  0
             Set dependencies = new LinkedHashSet();
 126  0
             dependencies.add( artifact );
 127  0
             dependencies.addAll( rgArtifacts );
 128  
 
 129  
             // Make sure that we do not influence the dependenecy resolution of extensions with the project's
 130  
             // dependencyManagement
 131  
 
 132  0
             ArtifactResolutionResult result = artifactResolver.resolveTransitively( dependencies, project.getArtifact(),
 133  
                                                                                     Collections.EMPTY_MAP,
 134  
                                                                                     //project.getManagedVersionMap(),
 135  
                                                                                     localRepository,
 136  
                                                                                     project.getRemoteArtifactRepositories(),
 137  
                                                                                     artifactMetadataSource, filter );
 138  
 
 139  
             // gross hack for some backwards compat (MNG-2749)
 140  
             // if it is a lone artifact, then we assume it to be a resource package, and put it in the main container
 141  
             // as before. If it has dependencies, that's when we risk conflict and exile to the child container
 142  
             // jvz: we have to make this 2 because plexus is always added now.
 143  
 
 144  0
             Set artifacts = result.getArtifacts();
 145  
 
 146  
             // Lifecycles are loaded by the Lifecycle executor by looking up lifecycle definitions from the
 147  
             // core container. So we need to look if an extension has a lifecycle mapping and use the container
 148  
             // and not an extension container. (MNG-2831)
 149  
 
 150  0
             if ( extensionContainsLifeycle( artifact.getFile() ) )
 151  
             {
 152  0
                 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
 153  
                 {
 154  0
                     Artifact a = (Artifact) i.next();
 155  
 
 156  0
                     if ( artifactFilter.include( a ) )
 157  
                     {
 158  0
                         getLogger().debug( "Adding extension to core container: " + a.getFile() );
 159  
 
 160  0
                         container.addJarResource( a.getFile() );
 161  
                     }
 162  0
                 }
 163  
             }
 164  0
             else if ( artifacts.size() == 2 )
 165  
             {
 166  0
                 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
 167  
                 {
 168  0
                     Artifact a = (Artifact) i.next();
 169  
 
 170  0
                     if ( !a.getArtifactId().equals( "plexus-utils" ) )
 171  
                     {
 172  0
                         a = project.replaceWithActiveArtifact( a );
 173  
 
 174  0
                         getLogger().debug( "Adding extension to core container: " + a.getFile() );
 175  
 
 176  0
                         container.addJarResource( a.getFile() );
 177  
                     }
 178  0
                 }
 179  
             }
 180  
             else
 181  
             {
 182  
                 // create a child container for the extension
 183  
                 // TODO: this could surely be simpler/different on trunk with the new classworlds
 184  
 
 185  0
                 if ( extensionContainer == null )
 186  
                 {
 187  0
                     extensionContainer = createContainer();
 188  
                 }
 189  
 
 190  0
                 for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
 191  
                 {
 192  0
                     Artifact a = (Artifact) i.next();
 193  
 
 194  0
                     a = project.replaceWithActiveArtifact( a );
 195  
 
 196  0
                     getLogger().debug( "Adding to extension classpath: " + a.getFile() );
 197  
 
 198  0
                     extensionContainer.addJarResource( a.getFile() );
 199  0
                 }
 200  
 
 201  0
                 if ( getLogger().isDebugEnabled() )
 202  
                 {
 203  0
                     getLogger().debug( "Extension container contents:" );
 204  0
                     extensionContainer.getContainerRealm().display();
 205  
                 }
 206  
             }
 207  
         }
 208  0
     }
 209  
 
 210  
     private PlexusContainer createContainer()
 211  
         throws PlexusContainerException
 212  
     {
 213  0
         DefaultPlexusContainer child = new DefaultPlexusContainer();
 214  
 
 215  0
         ClassWorld classWorld = container.getClassWorld();
 216  0
         child.setClassWorld( classWorld );
 217  
 
 218  0
         ClassRealm childRealm = null;
 219  
 
 220  
         // note: ideally extensions would live in their own realm, but this would mean that things like wagon-scm would
 221  
         // have no way to obtain SCM extensions
 222  0
         String childRealmId = "plexus.core.child-container[" + CONTAINER_NAME + "]";
 223  
         try
 224  
         {
 225  0
             childRealm = classWorld.getRealm( childRealmId );
 226  
         }
 227  0
         catch ( NoSuchRealmException e )
 228  
         {
 229  
             try
 230  
             {
 231  0
                 childRealm = classWorld.newRealm( childRealmId );
 232  
             }
 233  0
             catch ( DuplicateRealmException impossibleError )
 234  
             {
 235  0
                 getLogger().error( "An impossible error has occurred. After getRealm() failed, newRealm() " +
 236  
                     "produced duplication error on same id!", impossibleError );
 237  0
             }
 238  0
         }
 239  
 
 240  0
         childRealm.setParent( container.getContainerRealm() );
 241  
 
 242  0
         child.setCoreRealm( childRealm );
 243  
 
 244  0
         child.setName( CONTAINER_NAME );
 245  
 
 246  
         // This is what we are skipping - we use the parent realm, but not the parent container since otherwise
 247  
         // we won't reload component descriptors that already exist in there
 248  
 //        child.setParentPlexusContainer( this );
 249  
 
 250  
         // ----------------------------------------------------------------------
 251  
         // Set all the child elements from the parent that were set
 252  
         // programmatically.
 253  
         // ----------------------------------------------------------------------
 254  
 
 255  0
         child.setLoggerManager( container.getLoggerManager() );
 256  
 
 257  0
         child.initialize();
 258  
 
 259  0
         child.start();
 260  
 
 261  0
         return child;
 262  
     }
 263  
 
 264  
     public void registerWagons()
 265  
     {
 266  0
         if ( extensionContainer != null )
 267  
         {
 268  
             try
 269  
             {
 270  0
                 Map wagons = extensionContainer.lookupMap( Wagon.ROLE );
 271  0
                 getLogger().debug( "Wagons to register: " + wagons.keySet() );
 272  0
                 wagonManager.registerWagons( wagons.keySet(), extensionContainer );
 273  
             }
 274  0
             catch ( ComponentLookupException e )
 275  
             {
 276  
                 // no wagons found in the extension
 277  0
                 getLogger().debug( "No wagons found in the extensions or other internal error: " + e.getMessage(), e );
 278  0
             }
 279  
         }
 280  
         else
 281  
         {
 282  0
             getLogger().debug( "Wagons could not be registered as the extension container was never created" );
 283  
         }
 284  0
     }
 285  
 
 286  
     public void contextualize( Context context )
 287  
         throws ContextException
 288  
     {
 289  0
         container = (DefaultPlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
 290  0
     }
 291  
 
 292  0
     private static final class ProjectArtifactExceptionFilter
 293  
         implements ArtifactFilter
 294  
     {
 295  
         private ArtifactFilter passThroughFilter;
 296  
 
 297  
         private String projectDependencyConflictId;
 298  
 
 299  
         ProjectArtifactExceptionFilter( ArtifactFilter passThroughFilter,
 300  
                                         Artifact projectArtifact )
 301  0
         {
 302  0
             this.passThroughFilter = passThroughFilter;
 303  0
             projectDependencyConflictId = projectArtifact.getDependencyConflictId();
 304  0
         }
 305  
 
 306  
         public boolean include( Artifact artifact )
 307  
         {
 308  0
             String depConflictId = artifact.getDependencyConflictId();
 309  
 
 310  0
             return projectDependencyConflictId.equals( depConflictId ) || passThroughFilter.include( artifact );
 311  
         }
 312  
     }
 313  
 
 314  
     private boolean extensionContainsLifeycle( File extension )
 315  
     {
 316  
         JarFile f;
 317  
 
 318  
         try
 319  
         {
 320  0
             f = new JarFile( extension );
 321  
 
 322  0
             InputStream is = f.getInputStream( f.getEntry( "META-INF/plexus/components.xml" ) );
 323  
 
 324  0
             if ( is == null )
 325  
             {
 326  0
                 return false;
 327  
             }
 328  
 
 329  0
             Xpp3Dom dom = Xpp3DomBuilder.build( new InputStreamReader( is ) );
 330  
 
 331  0
             Xpp3Dom[] components = dom.getChild( "components" ).getChildren( "component" );
 332  
 
 333  0
             for ( int i = 0; i < components.length; i++ )
 334  
             {
 335  0
                 if ( components[i].getChild( "role" ).getValue().equals( "org.apache.maven.lifecycle.mapping.LifecycleMapping" ) )
 336  
                 {
 337  0
                     return true;
 338  
                 }
 339  
             }
 340  
         }
 341  0
         catch( Exception e )
 342  
         {
 343  
             // do nothing
 344  0
         }
 345  
 
 346  0
         return false;
 347  
     }
 348  
 }