[MNG-7350] Introduce a factory for ModelCache

This commit is contained in:
Guillaume Nodet 2021-12-02 14:45:17 +01:00
parent 1954d51ff2
commit 14ca723438
7 changed files with 107 additions and 13 deletions

View File

@ -56,12 +56,14 @@ import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelBuildingResult;
import org.apache.maven.model.building.ModelCache;
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.building.StringModelSource;
import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.repository.internal.ArtifactDescriptorUtils;
import org.apache.maven.repository.internal.ModelCacheFactory;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
@ -111,7 +113,8 @@ public class DefaultProjectBuilder
@Requirement
private ProjectDependenciesResolver dependencyResolver;
private final ReactorModelCache modelCache = new ReactorModelCache();
@Requirement
private ModelCacheFactory modelCacheFactory;
// ----------------------------------------------------------------------
// MavenProjectBuilder Implementation
@ -122,7 +125,8 @@ public class DefaultProjectBuilder
throws ProjectBuildingException
{
return build( pomFile, new FileModelSource( pomFile ),
new InternalConfig( request, null, useGlobalModelCache() ? getModelCache() : null ) );
new InternalConfig( request, null,
useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : null ) );
}
private boolean useGlobalModelCache()
@ -135,7 +139,8 @@ public class DefaultProjectBuilder
throws ProjectBuildingException
{
return build( null, modelSource,
new InternalConfig( request, null, useGlobalModelCache() ? getModelCache() : null ) );
new InternalConfig( request, null,
useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : null ) );
}
private ProjectBuildingResult build( File pomFile, ModelSource modelSource, InternalConfig config )
@ -306,7 +311,8 @@ public class DefaultProjectBuilder
org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact );
pomArtifact = ArtifactDescriptorUtils.toPomArtifact( pomArtifact );
InternalConfig config = new InternalConfig( request, null, useGlobalModelCache() ? getModelCache() : null );
InternalConfig config = new InternalConfig( request, null,
useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : null );
boolean localProject;
@ -369,7 +375,7 @@ public class DefaultProjectBuilder
ReactorModelPool modelPool = new ReactorModelPool();
InternalConfig config = new InternalConfig( request, modelPool,
useGlobalModelCache() ? getModelCache() : new ReactorModelCache() );
useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : new ReactorModelCache() );
Map<String, MavenProject> projectIndex = new HashMap<>( 256 );
@ -1058,9 +1064,9 @@ public class DefaultProjectBuilder
private final ReactorModelPool modelPool;
private final ReactorModelCache modelCache;
private final ModelCache modelCache;
InternalConfig( ProjectBuildingRequest request, ReactorModelPool modelPool, ReactorModelCache modelCache )
InternalConfig( ProjectBuildingRequest request, ReactorModelPool modelPool, ModelCache modelCache )
{
this.request = request;
this.modelPool = modelPool;
@ -1073,9 +1079,9 @@ public class DefaultProjectBuilder
}
private ReactorModelCache getModelCache()
private ModelCache createModelCache( RepositorySystemSession session )
{
return this.modelCache;
return modelCacheFactory.createCache( session );
}
}

View File

@ -146,6 +146,8 @@ public abstract class AbstractCoreMavenComponentTestCase
.setSystemProperties( executionProperties )
.setUserProperties( new Properties() );
initRepoSession( configuration );
List<MavenProject> projects = new ArrayList<>();
if ( pom != null )
@ -174,8 +176,6 @@ public abstract class AbstractCoreMavenComponentTestCase
projects.add( project );
}
initRepoSession( configuration );
MavenSession session =
new MavenSession( getContainer(), configuration.getRepositorySession(), request,
new DefaultMavenExecutionResult() );

View File

@ -89,6 +89,8 @@ public class DefaultArtifactDescriptorReader
private ModelBuilder modelBuilder;
private ModelCacheFactory modelCacheFactory;
public DefaultArtifactDescriptorReader()
{
// enable no-arg constructor
@ -97,7 +99,8 @@ public class DefaultArtifactDescriptorReader
@Inject
DefaultArtifactDescriptorReader( RemoteRepositoryManager remoteRepositoryManager, VersionResolver versionResolver,
VersionRangeResolver versionRangeResolver, ArtifactResolver artifactResolver,
ModelBuilder modelBuilder, RepositoryEventDispatcher repositoryEventDispatcher )
ModelBuilder modelBuilder, RepositoryEventDispatcher repositoryEventDispatcher,
ModelCacheFactory modelCacheFactory )
{
setRemoteRepositoryManager( remoteRepositoryManager );
setVersionResolver( versionResolver );
@ -105,6 +108,7 @@ public class DefaultArtifactDescriptorReader
setArtifactResolver( artifactResolver );
setModelBuilder( modelBuilder );
setRepositoryEventDispatcher( repositoryEventDispatcher );
setModelCacheFactory( modelCacheFactory );
}
public void initService( ServiceLocator locator )
@ -119,6 +123,7 @@ public class DefaultArtifactDescriptorReader
setModelBuilder( new DefaultModelBuilderFactory().newInstance() );
}
setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) );
setModelCacheFactory( locator.getService( ModelCacheFactory.class ) );
}
public DefaultArtifactDescriptorReader setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager )
@ -162,6 +167,13 @@ public class DefaultArtifactDescriptorReader
return this;
}
public DefaultArtifactDescriptorReader setModelCacheFactory( ModelCacheFactory modelCacheFactory )
{
this.modelCacheFactory = Objects.requireNonNull( modelCacheFactory,
"modelCacheFactory cannot be null" );
return this;
}
public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session,
ArtifactDescriptorRequest request )
throws ArtifactDescriptorException
@ -276,7 +288,7 @@ public class DefaultArtifactDescriptorReader
modelRequest.setTwoPhaseBuilding( false );
modelRequest.setSystemProperties( toProperties( session.getSystemProperties() ) );
modelRequest.setUserProperties( toProperties( session.getUserProperties() ) );
modelRequest.setModelCache( DefaultModelCache.newInstance( session ) );
modelRequest.setModelCache( modelCacheFactory.createCache( session ) );
modelRequest.setModelResolver( new DefaultModelResolver( session, trace.newChild( modelRequest ),
request.getRequestContext(), artifactResolver,
versionRangeResolver, remoteRepositoryManager,

View File

@ -0,0 +1,41 @@
package org.apache.maven.repository.internal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.maven.model.building.ModelCache;
import org.eclipse.aether.RepositorySystemSession;
/**
* Default implementation of {@link ModelCacheFactory}.
*/
@Singleton
@Named
public class DefaultModelCacheFactory implements ModelCacheFactory
{
@Override
public ModelCache createCache( RepositorySystemSession session )
{
return DefaultModelCache.newInstance( session );
}
}

View File

@ -79,6 +79,7 @@ public final class MavenRepositorySystemUtils
locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
locator.addService( MetadataGeneratorFactory.class, SnapshotMetadataGeneratorFactory.class );
locator.addService( MetadataGeneratorFactory.class, VersionsMetadataGeneratorFactory.class );
locator.addService( ModelCacheFactory.class, DefaultModelCacheFactory.class );
return locator;
}

View File

@ -56,6 +56,7 @@ public final class MavenResolverModule
.to( VersionsMetadataGeneratorFactory.class ).in( Singleton.class );
bind( ModelBuilder.class ).toInstance( new DefaultModelBuilderFactory().newInstance() );
bind( ModelCacheFactory.class ).to( DefaultModelCacheFactory.class ).in( Singleton.class );
}
@Provides

View File

@ -0,0 +1,33 @@
package org.apache.maven.repository.internal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.building.ModelCache;
import org.eclipse.aether.RepositorySystemSession;
/**
* Factory for {@link ModelCache} objects.
*/
public interface ModelCacheFactory
{
ModelCache createCache( RepositorySystemSession session );
}