From 76c17e2cea030a2f29cfb99f0e82a13bdab820d9 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 7 Nov 2018 22:14:18 +0100 Subject: [PATCH] Adding bean for maven repositorysystem management --- .../maven2/Maven3DependencyTreeBuilder.java | 10 ++- .../maven2/Maven2RepositoryStorage.java | 6 +- .../maven2/RepositoryModelResolver.java | 29 +++---- ...MavenUtil.java => MavenSystemManager.java} | 77 ++++++++++++------- .../resources/META-INF/spring-context.xml | 1 + 5 files changed, 72 insertions(+), 51 deletions(-) rename archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/{MavenUtil.java => MavenSystemManager.java} (65%) diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java index 7c61aec14..d200d081a 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java @@ -34,13 +34,12 @@ import org.apache.archiva.model.ArchivaRepositoryMetadata; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.RepositoryRegistry; -import org.apache.archiva.repository.maven2.MavenUtil; +import org.apache.archiva.repository.maven2.MavenSystemManager; import org.apache.archiva.repository.metadata.MetadataTools; import org.apache.archiva.xml.XMLException; import org.apache.commons.lang.StringUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.bridge.MavenRepositorySystem; -import org.apache.maven.model.building.DefaultModelBuilderFactory; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.DefaultArtifact; @@ -91,6 +90,9 @@ public class Maven3DependencyTreeBuilder @Inject RepositoryRegistry repositoryRegistry; + @Inject + MavenSystemManager mavenSystemManager; + @PostConstruct public void initialize() @@ -193,8 +195,8 @@ public class Maven3DependencyTreeBuilder private void resolve( ResolveRequest resolveRequest ) { - RepositorySystem system = MavenUtil.newRepositorySystem(); - RepositorySystemSession session = MavenUtil.newRepositorySystemSession( resolveRequest.localRepoDir ); + RepositorySystem system = mavenSystemManager.getRepositorySystem(); + RepositorySystemSession session = MavenSystemManager.newRepositorySystemSession( resolveRequest.localRepoDir ); org.eclipse.aether.artifact.Artifact artifact = new DefaultArtifact( resolveRequest.groupId + ":" + resolveRequest.artifactId + ":" + resolveRequest.version ); diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java index b3b420f29..317638730 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java @@ -54,6 +54,7 @@ import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.content.PathParser; +import org.apache.archiva.repository.maven2.MavenSystemManager; import org.apache.archiva.xml.XMLException; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; @@ -149,6 +150,9 @@ public class Maven2RepositoryStorage @Named( "pathParser#default" ) private PathParser pathParser; + @Inject + private MavenSystemManager mavenSystemManager; + private static final String METADATA_FILENAME_START = "maven-metadata"; private static final String METADATA_FILENAME = METADATA_FILENAME_START + ".xml"; @@ -292,7 +296,7 @@ public class Maven2RepositoryStorage // MRM-1411 req.setModelResolver( new RepositoryModelResolver( managedRepository, pathTranslator, wagonFactory, remoteRepositories, - networkProxies, managedRepository )); + networkProxies, managedRepository , mavenSystemManager)); Model model; try diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/RepositoryModelResolver.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/RepositoryModelResolver.java index 00bd622af..92ef88bf8 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/RepositoryModelResolver.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/RepositoryModelResolver.java @@ -32,7 +32,7 @@ import org.apache.archiva.proxy.common.WagonFactoryRequest; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.RepositoryCredentials; -import org.apache.archiva.repository.maven2.MavenUtil; +import org.apache.archiva.repository.maven2.MavenSystemManager; import org.apache.archiva.xml.XMLException; import org.apache.commons.lang.StringUtils; import org.apache.http.auth.UsernamePasswordCredentials; @@ -56,7 +56,6 @@ import org.apache.maven.wagon.authentication.AuthenticationException; import org.apache.maven.wagon.authentication.AuthenticationInfo; import org.apache.maven.wagon.authorization.AuthorizationException; import org.apache.maven.wagon.proxy.ProxyInfo; -import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; @@ -71,6 +70,7 @@ import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.inject.Inject; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -99,7 +99,7 @@ public class RepositoryModelResolver private static final String METADATA_FILENAME = "maven-metadata.xml"; - private DefaultServiceLocator locator; + private MavenSystemManager mavenSystemManager; // key/value: remote repo ID/network proxy Map networkProxyMap; @@ -115,7 +115,8 @@ public class RepositoryModelResolver public RepositoryModelResolver( ManagedRepository managedRepository, RepositoryPathTranslator pathTranslator, WagonFactory wagonFactory, List remoteRepositories, - Map networkProxiesMap, ManagedRepository targetRepository) + Map networkProxiesMap, ManagedRepository targetRepository, + MavenSystemManager mavenSystemManager) { this( Paths.get( managedRepository.getLocation() ), pathTranslator ); @@ -129,23 +130,13 @@ public class RepositoryModelResolver this.targetRepository = targetRepository; - this.locator = MavenRepositorySystemUtils.newServiceLocator( ); + this.session = MavenSystemManager.newRepositorySystemSession( managedRepository.getLocalPath().toString() ); - locator.addService( RepositoryConnectorFactory.class, - ArchivaRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class ); - locator.addService( VersionResolver.class, DefaultVersionResolver.class ); - locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class ); - locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class ); + this.versionRangeResolver = mavenSystemManager.getLocator().getService(VersionRangeResolver.class); - this.session = MavenUtil.newRepositorySystemSession( managedRepository.getLocalPath().toString() ); - - this.versionRangeResolver = locator.getService(VersionRangeResolver.class); + this.mavenSystemManager = mavenSystemManager; } - private RepositorySystem newRepositorySystem() - { - return locator.getService( RepositorySystem.class ); - } @Override public ModelSource resolveModel( String groupId, String artifactId, String version ) @@ -301,7 +292,7 @@ public class RepositoryModelResolver public ModelResolver newCopy() { return new RepositoryModelResolver( managedRepository, pathTranslator, wagonFactory, remoteRepositories, - networkProxyMap, targetRepository); + networkProxyMap, targetRepository, mavenSystemManager); } // FIXME: we need to do some refactoring, we cannot re-use the proxy components of archiva-proxy in maven2-repository @@ -316,7 +307,7 @@ public class RepositoryModelResolver Path tmpSha1 = null; Path tmpResource = null; String artifactPath = pathTranslator.toPath( groupId, artifactId, version, filename ); - Path resource = Paths.get(targetRepository.getLocation()).resolve( artifactPath ); + Path resource = targetRepository.getLocalPath().resolve( artifactPath ); Path workingDirectory = createWorkingDirectory( targetRepository.getLocation().toString() ); try diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenUtil.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenSystemManager.java similarity index 65% rename from archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenUtil.java rename to archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenSystemManager.java index 95d4df019..55b3fb6f8 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenUtil.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenSystemManager.java @@ -41,13 +41,27 @@ import org.eclipse.aether.util.graph.selector.AndDependencySelector; import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; /** * Some static utility methods that are used by different classes. */ -public class MavenUtil { +@Service("mavenSystemManager") +public class MavenSystemManager { + + static Logger log = LoggerFactory.getLogger(MavenSystemManager.class); + + private DefaultServiceLocator locator; + private RepositorySystem system; - static Logger log = LoggerFactory.getLogger(MavenUtil.class); + @PostConstruct + private synchronized void init() { + locator = newLocator(); + system = newRepositorySystem(locator); + + } /** * Creates a new aether repository system session for the given directory and assigns the @@ -56,41 +70,50 @@ public class MavenUtil { * @param localRepoDir The repository directory * @return The newly created session object. */ - public static RepositorySystemSession newRepositorySystemSession(String localRepoDir) - { - DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession( ); - - LocalRepository repo = new LocalRepository( localRepoDir ); - - DependencySelector depFilter = new AndDependencySelector( new ExclusionDependencySelector() ); - session.setDependencySelector( depFilter ); - SimpleLocalRepositoryManagerFactory repFactory = new SimpleLocalRepositoryManagerFactory( ); - try - { - LocalRepositoryManager manager = repFactory.newInstance( session, repo ); + public static RepositorySystemSession newRepositorySystemSession(String localRepoDir) { + DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession(); + + LocalRepository repo = new LocalRepository(localRepoDir); + + DependencySelector depFilter = new AndDependencySelector(new ExclusionDependencySelector()); + session.setDependencySelector(depFilter); + SimpleLocalRepositoryManagerFactory repFactory = new SimpleLocalRepositoryManagerFactory(); + try { + LocalRepositoryManager manager = repFactory.newInstance(session, repo); session.setLocalRepositoryManager(manager); - } - catch ( NoLocalRepositoryManagerException e ) - { + } catch (NoLocalRepositoryManagerException e) { log.error("Could not assign the repository manager to the session: {}", e.getMessage(), e); } return session; } + public RepositorySystem getRepositorySystem() { + return system; + } + + public DefaultServiceLocator getLocator() { + return locator; + } + /** * Finds the + * * @return */ - public static RepositorySystem newRepositorySystem() - { - DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator( ); - locator.addService( RepositoryConnectorFactory.class, - ArchivaRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class ); - locator.addService( VersionResolver.class, DefaultVersionResolver.class ); - locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class ); - locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class ); - - return locator.getService( RepositorySystem.class ); + public static RepositorySystem newRepositorySystem(DefaultServiceLocator locator) { + return locator.getService(RepositorySystem.class); + } + + public static DefaultServiceLocator newLocator() { + DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); + + locator.addService(RepositoryConnectorFactory.class, + ArchivaRepositoryConnectorFactory.class);// FileRepositoryConnectorFactory.class ); + locator.addService(VersionResolver.class, DefaultVersionResolver.class); + locator.addService(VersionRangeResolver.class, DefaultVersionRangeResolver.class); + locator.addService(ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class); + + return locator; } } diff --git a/archiva-modules/plugins/maven2-repository/src/main/resources/META-INF/spring-context.xml b/archiva-modules/plugins/maven2-repository/src/main/resources/META-INF/spring-context.xml index 9fb23a571..375a863fe 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/plugins/maven2-repository/src/main/resources/META-INF/spring-context.xml @@ -36,4 +36,5 @@ + \ No newline at end of file -- 2.39.5