From 97b2c9c9095b95500ab15caaf447369ed9251f08 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 1 Nov 2017 22:52:14 +0100 Subject: [PATCH] Refactoring the repository content factory Adding content provider service that creates repository content instances remove direct access to the repository content instances. --- .../src/test/resources/spring-context.xml | 4 +- .../AbstractRepositoryPurgeTest.java | 6 +- .../mock/ManagedRepositoryContentMock.java | 181 ------------------ .../mock/RemoteRepositoryContentMock.java | 78 -------- ...ing-context-cleanup-released-snapshots.xml | 3 +- .../src/test/resources/spring-context.xml | 1 - .../LegacyConverterArtifactConsumer.java | 10 +- .../src/test/resources/spring-context.xml | 5 - .../archiva/proxy/AbstractProxyTestCase.java | 5 +- .../archiva/proxy/HttpProxyTransferTest.java | 5 +- .../src/test/resources/spring-context.xml | 4 +- .../src/test/resources/spring-context.xml | 3 - .../repository/ManagedRepositoryContent.java | 26 +-- .../repository/RemoteRepositoryContent.java | 21 +- .../archiva/repository/RepositoryContent.java | 52 +++++ .../repository/RepositoryContentFactory.java | 86 +++++---- .../repository/RepositoryContentProvider.java | 82 ++++++++ .../repository/RepositoryRegistry.java | 4 +- .../src/test/resources/spring-context.xml | 3 - .../src/test/resources/spring-context.xml | 4 - .../META-INF/spring-context-test.xml | 3 - .../src/test/resources/spring-context.xml | 3 - .../spring-context-artifacts-download.xml | 5 - .../spring-context-merge-index-download.xml | 4 - .../resources/spring-context-rss-servlet.xml | 6 +- .../resources/spring-context-test-common.xml | 4 +- .../resources/spring-context-with-jcr.xml | 4 - .../webdav/ArchivaDavResourceFactoryTest.java | 70 ++++++- .../spring-context-servlet-security-test.xml | 5 +- .../src/test/resources/spring-context.xml | 5 +- .../AbstractDefaultRepositoryContent.java | 19 +- .../ManagedDefaultRepositoryContent.java | 27 +-- .../content/maven2/MavenContentProvider.java | 123 ++++++++++++ .../RemoteDefaultRepositoryContent.java | 10 +- .../ManagedDefaultRepositoryContentTest.java | 9 +- .../RemoteDefaultRepositoryContentTest.java | 7 +- .../AbstractRepositoryLayerTestCase.java | 8 +- .../content/maven2/RepositoryRequestTest.java | 6 +- .../metadata/MetadataToolsTest.java | 12 +- .../resources/spring-context-no-mock-conf.xml | 2 - .../spring-context-repo-request-test.xml | 4 - .../src/test/resources/spring-context.xml | 1 - 42 files changed, 474 insertions(+), 446 deletions(-) delete mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java delete mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java create mode 100644 archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryContent.java create mode 100644 archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryContentProvider.java create mode 100644 archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/MavenContentProvider.java diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml index b5ff21555..56894fc5f 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml @@ -32,10 +32,8 @@ - + - - diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java index b0e9a9898..d65d1a491 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java @@ -27,6 +27,7 @@ import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPat import org.apache.archiva.repository.BasicManagedRepository; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.ReleaseScheme; +import org.apache.archiva.repository.RepositoryContentProvider; import org.apache.archiva.repository.events.RepositoryListener; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; @@ -184,8 +185,9 @@ public abstract class AbstractRepositoryPurgeTest { if ( repo == null ) { - repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class ); - repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) ); + org.apache.archiva.repository.ManagedRepository repoCfg = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); + RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class ); + repo = provider.createManagedContent( repoCfg ); } return repo; diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java deleted file mode 100644 index 9a53bf28d..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java +++ /dev/null @@ -1,181 +0,0 @@ -package org.apache.archiva.repository.mock; - -/* - * 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.archiva.model.ArchivaArtifact; -import org.apache.archiva.model.ArtifactReference; -import org.apache.archiva.model.ProjectReference; -import org.apache.archiva.model.VersionedReference; -import org.apache.archiva.repository.ContentNotFoundException; -import org.apache.archiva.repository.ManagedRepository; -import org.apache.archiva.repository.ManagedRepositoryContent; -import org.apache.archiva.repository.RepositoryException; -import org.apache.archiva.repository.layout.LayoutException; -import org.springframework.stereotype.Service; - -import java.net.URI; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Set; - -/** - * @author Martin Stockhammer - */ -@Service("managedRepositoryContent#mock") -public class ManagedRepositoryContentMock implements ManagedRepositoryContent -{ - private ManagedRepository repository; - - @Override - public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException - { - - } - - @Override - public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException - { - - } - - @Override - public void deleteGroupId( String groupId ) throws ContentNotFoundException - { - - } - - @Override - public void deleteProject( String namespace, String projectId ) throws RepositoryException - { - - } - - @Override - public String getId( ) - { - return null; - } - - @Override - public Set getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException - { - return null; - } - - @Override - public String getRepoRoot() - { - return convertUriToPath( repository.getLocation() ); - } - - private String convertUriToPath( URI uri ) { - if (uri.getScheme()==null) { - return Paths.get(uri.getPath()).toString(); - } else if ("file".equals(uri.getScheme())) { - return Paths.get(uri).toString(); - } else { - return uri.toString(); - } - } - - @Override - public ManagedRepository getRepository( ) - { - return repository; - } - - @Override - public Set getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException - { - return null; - } - - @Override - public Set getVersions( VersionedReference reference ) throws ContentNotFoundException - { - return null; - } - - @Override - public boolean hasContent( ArtifactReference reference ) - { - return false; - } - - @Override - public boolean hasContent( ProjectReference reference ) - { - return false; - } - - @Override - public boolean hasContent( VersionedReference reference ) - { - return false; - } - - @Override - public void setRepository( ManagedRepository repo ) - { - this.repository = repo; - } - - @Override - public ArtifactReference toArtifactReference( String path ) throws LayoutException - { - return null; - } - - @Override - public Path toFile( ArtifactReference reference ) - { - return null; - } - - @Override - public Path toFile( ArchivaArtifact reference ) - { - return null; - } - - @Override - public String toMetadataPath( ProjectReference reference ) - { - return null; - } - - @Override - public String toMetadataPath( VersionedReference reference ) - { - return null; - } - - @Override - public String toPath( ArtifactReference reference ) - { - return null; - } - - @Override - public String toPath( ArchivaArtifact reference ) - { - return null; - } -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java deleted file mode 100644 index 238b0cf61..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.apache.archiva.repository.mock; - -/* - * 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.archiva.model.ArtifactReference; -import org.apache.archiva.model.RepositoryURL; -import org.apache.archiva.repository.RemoteRepository; -import org.apache.archiva.repository.RemoteRepositoryContent; -import org.apache.archiva.repository.layout.LayoutException; -import org.springframework.stereotype.Service; - -/** - * @author Martin Stockhammer - */ -@Service("remoteRepositoryContent#mock") -public class RemoteRepositoryContentMock implements RemoteRepositoryContent -{ - RemoteRepository repository; - - @Override - public String getId( ) - { - return null; - } - - @Override - public RemoteRepository getRepository( ) - { - return null; - } - - @Override - public RepositoryURL getURL( ) - { - return null; - } - - @Override - public void setRepository( RemoteRepository repo ) - { - this.repository = repo; - } - - @Override - public ArtifactReference toArtifactReference( String path ) throws LayoutException - { - return null; - } - - @Override - public String toPath( ArtifactReference reference ) - { - return null; - } - - @Override - public RepositoryURL toURL( ArtifactReference reference ) - { - return null; - } -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context-cleanup-released-snapshots.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context-cleanup-released-snapshots.xml index c6fa2a127..5119c55e9 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context-cleanup-released-snapshots.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context-cleanup-released-snapshots.xml @@ -32,9 +32,8 @@ - + - diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context.xml index 360e20d92..e63eb7af3 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/spring-context.xml @@ -44,7 +44,6 @@ - - - - - diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java index ba1fdebd8..1c56a04af 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java @@ -31,13 +31,22 @@ import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.Configuration; +import org.apache.archiva.configuration.FileTypes; import org.apache.archiva.configuration.RepositoryGroupConfiguration; +import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; import org.apache.archiva.proxy.DefaultRepositoryProxyConnectors; import org.apache.archiva.proxy.model.ProxyFetchResult; import org.apache.archiva.repository.EditableManagedRepository; import org.apache.archiva.repository.ManagedRepositoryContent; +import org.apache.archiva.repository.RemoteRepository; +import org.apache.archiva.repository.RemoteRepositoryContent; +import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryContent; import org.apache.archiva.repository.RepositoryContentFactory; +import org.apache.archiva.repository.RepositoryContentProvider; +import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent; import org.apache.archiva.repository.content.maven2.RepositoryRequest; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; @@ -62,8 +71,10 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Set; import static org.easymock.EasyMock.*; @@ -128,6 +139,12 @@ public class ArchivaDavResourceFactoryTest @Inject DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin; + @Inject + List artifactMappingProviders; + + @Inject + FileTypes fileTypes; + @Before @Override @@ -215,7 +232,7 @@ public class ArchivaDavResourceFactoryTest private ManagedRepositoryContent createManagedRepositoryContent( String repoId ) throws RepositoryAdminException { - ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent(); + ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent(artifactMappingProviders, fileTypes); org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repoId ); repoContent.setRepository( repo ); if (repo!=null && repo instanceof EditableManagedRepository) @@ -225,6 +242,52 @@ public class ArchivaDavResourceFactoryTest return repoContent; } + private RepositoryContentProvider createRepositoryContentProvider(ManagedRepositoryContent content) { + Set TYPES = new HashSet<>( ); + TYPES.add(RepositoryType.MAVEN); + return new RepositoryContentProvider( ) + { + + + @Override + public boolean supportsLayout( String layout ) + { + return true; + } + + @Override + public Set getSupportedRepositoryTypes( ) + { + return TYPES; + } + + @Override + public boolean supports( RepositoryType type ) + { + return true; + } + + @Override + public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException + { + return null; + } + + @Override + public ManagedRepositoryContent createManagedContent( org.apache.archiva.repository.ManagedRepository repository ) throws RepositoryException + { + content.setRepository( repository ); + return content; + } + + @Override + public T createContent( Class clazz, V repository ) throws RepositoryException + { + return null; + } + }; + } + @After @Override public void tearDown() @@ -580,7 +643,10 @@ public class ArchivaDavResourceFactoryTest { ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO ); ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); - beanFactory.registerSingleton("managedRepositoryContent#legacy", legacyRepo); + RepositoryContentProvider provider = createRepositoryContentProvider(legacyRepo ); + beanFactory.registerSingleton("repositoryContentProvider#legacy", provider); + RepositoryContentFactory repoContentFactory = applicationContext.getBean( "repositoryContentFactory#default", RepositoryContentFactory.class ); + repoContentFactory.getRepositoryContentProviders().add(provider); defaultManagedRepositoryAdmin.addManagedRepository( createManagedRepository( LEGACY_REPO, Paths.get( "target/test-classes/" + LEGACY_REPO ).toString(), "legacy" ), false, null ); diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context-servlet-security-test.xml b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context-servlet-security-test.xml index 693816099..9f38b827d 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context-servlet-security-test.xml +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context-servlet-security-test.xml @@ -31,10 +31,7 @@ - - - + base-package="org.apache.archiva.redback.keys,org.apache.archiva.webdav,org.apache.archiva.metadata.repository,org.apache.archiva.webdav.util,org.apache.archiva.common.plexusbridge"/> diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context.xml b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context.xml index 28f59f75d..fba970705 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/spring-context.xml @@ -30,10 +30,7 @@ - - - - + diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/AbstractDefaultRepositoryContent.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/AbstractDefaultRepositoryContent.java index 4f8b80cbd..a15b5d76c 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/AbstractDefaultRepositoryContent.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/AbstractDefaultRepositoryContent.java @@ -20,6 +20,7 @@ package org.apache.archiva.repository.content.maven2; */ import org.apache.archiva.common.utils.VersionUtil; +import org.apache.archiva.configuration.FileTypes; import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator; @@ -27,6 +28,7 @@ import org.apache.archiva.model.ArchivaArtifact; import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.model.ProjectReference; import org.apache.archiva.model.VersionedReference; +import org.apache.archiva.repository.RepositoryContent; import org.apache.archiva.repository.content.PathParser; import org.apache.archiva.repository.layout.LayoutException; import org.apache.commons.lang.StringUtils; @@ -40,8 +42,10 @@ import java.util.List; /** * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout. */ -public abstract class AbstractDefaultRepositoryContent +public abstract class AbstractDefaultRepositoryContent implements RepositoryContent { + + protected Logger log = LoggerFactory.getLogger( getClass() ); public static final String MAVEN_METADATA = "maven-metadata.xml"; @@ -56,16 +60,19 @@ public abstract class AbstractDefaultRepositoryContent private PathParser defaultPathParser = new DefaultPathParser(); + + /** * */ - @Inject protected List artifactMappingProviders; - @PostConstruct - protected void initialize() - { - // no op + AbstractDefaultRepositoryContent(List artifactMappingProviders) { + this.artifactMappingProviders = artifactMappingProviders; + } + + public void setArtifactMappingProviders(List artifactMappingProviders) { + this.artifactMappingProviders = artifactMappingProviders; } public ArtifactReference toArtifactReference( String path ) diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java index 298aa5d18..847dd7a6b 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java @@ -21,6 +21,7 @@ package org.apache.archiva.repository.content.maven2; import org.apache.archiva.common.utils.PathUtil; import org.apache.archiva.configuration.FileTypes; +import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider; import org.apache.archiva.model.ArchivaArtifact; import org.apache.archiva.model.ArtifactReference; @@ -31,11 +32,7 @@ import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.layout.LayoutException; import org.apache.commons.lang.StringUtils; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Service; -import javax.inject.Inject; -import javax.inject.Named; import java.io.IOException; import java.net.URI; import java.nio.file.Files; @@ -43,6 +40,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -51,22 +49,29 @@ import java.util.stream.Stream; /** * ManagedDefaultRepositoryContent */ -@Service ("managedRepositoryContent#maven") -@Scope ("prototype") public class ManagedDefaultRepositoryContent extends AbstractDefaultRepositoryContent implements ManagedRepositoryContent { - @Inject - @Named ( "fileTypes" ) + private FileTypes filetypes; + public void setFileTypes(FileTypes fileTypes) { + this.filetypes = fileTypes; + } + + + private org.apache.archiva.repository.ManagedRepository repository; - public ManagedDefaultRepositoryContent() + public ManagedDefaultRepositoryContent(FileTypes fileTypes) { + super(Collections.singletonList( new DefaultArtifactMappingProvider() )); + setFileTypes( fileTypes ); + } + public ManagedDefaultRepositoryContent( List artifactMappingProviders, FileTypes fileTypes ) { - // default to use if there are none supplied as components - this.artifactMappingProviders = Collections.singletonList( new DefaultArtifactMappingProvider() ); + super(artifactMappingProviders==null ? Collections.singletonList( new DefaultArtifactMappingProvider() ) : artifactMappingProviders); + setFileTypes( fileTypes ); } @Override diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/MavenContentProvider.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/MavenContentProvider.java new file mode 100644 index 000000000..423c4b17e --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/MavenContentProvider.java @@ -0,0 +1,123 @@ +package org.apache.archiva.repository.content.maven2; + +/* + * 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.archiva.configuration.FileTypes; +import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; +import org.apache.archiva.repository.ManagedRepository; +import org.apache.archiva.repository.ManagedRepositoryContent; +import org.apache.archiva.repository.RemoteRepository; +import org.apache.archiva.repository.RemoteRepositoryContent; +import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryContent; +import org.apache.archiva.repository.RepositoryContentProvider; +import org.apache.archiva.repository.RepositoryException; +import org.apache.archiva.repository.RepositoryType; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import javax.inject.Named; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Maven implementation of the repository content provider. Only default layout and + * maven repository types are supported. + */ +@Service("repositoryContentProvider#maven") +public class MavenContentProvider implements RepositoryContentProvider +{ + + @Inject + @Named( "fileTypes" ) + private FileTypes filetypes; + + @Inject + protected List artifactMappingProviders; + + + private static final Set REPOSITORY_TYPES = new HashSet<>( ); + static { + REPOSITORY_TYPES.add(RepositoryType.MAVEN); + } + + @Override + public boolean supportsLayout( String layout ) + { + return "default".equals( layout ); + } + + @Override + public Set getSupportedRepositoryTypes( ) + { + return REPOSITORY_TYPES; + } + + @Override + public boolean supports( RepositoryType type ) + { + return type.equals( RepositoryType.MAVEN ); + } + + @Override + public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException + { + if (!supports( repository.getType() )) { + throw new RepositoryException( "Repository type "+repository.getType()+" is not supported by this implementation." ); + } + if (!supportsLayout( repository.getLayout() )) { + throw new RepositoryException( "Repository layout "+repository.getLayout()+" is not supported by this implementation." ); + } + RemoteDefaultRepositoryContent content = new RemoteDefaultRepositoryContent(artifactMappingProviders); + content.setRepository( repository ); + return content; + } + + @Override + public ManagedRepositoryContent createManagedContent( ManagedRepository repository ) throws RepositoryException + { + if (!supports( repository.getType() )) { + throw new RepositoryException( "Repository type "+repository.getType()+" is not supported by this implementation." ); + } + if (!supportsLayout( repository.getLayout() )) { + throw new RepositoryException( "Repository layout "+repository.getLayout()+" is not supported by this implementation." ); + } + ManagedDefaultRepositoryContent content = new ManagedDefaultRepositoryContent(artifactMappingProviders, filetypes); + content.setRepository( repository ); + return content; + } + + @Override + public T createContent( Class clazz, V repository ) throws RepositoryException + { + if (!supports( repository.getType() )) { + throw new RepositoryException( "Repository type "+repository.getType()+" is not supported by this implementation." ); + } + if (repository instanceof ManagedRepository && ManagedRepositoryContent.class.isAssignableFrom( clazz ) ) { + return (T) this.createManagedContent( (ManagedRepository) repository ); + } else if (repository instanceof RemoteRepository && RemoteRepository.class.isAssignableFrom( clazz )) { + return (T) this.createRemoteContent( (RemoteRepository) repository ); + } else { + throw new RepositoryException( "Repository flavour is not supported: "+repository.getClass().getName() ); + } + } + +} diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/RemoteDefaultRepositoryContent.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/RemoteDefaultRepositoryContent.java index 2942c2348..b9c2238fa 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/RemoteDefaultRepositoryContent.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/RemoteDefaultRepositoryContent.java @@ -19,6 +19,7 @@ package org.apache.archiva.repository.content.maven2; * under the License. */ +import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.model.RepositoryURL; import org.apache.archiva.repository.RemoteRepository; @@ -27,17 +28,22 @@ import org.apache.archiva.repository.layout.LayoutException; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; +import java.util.List; + /** * RemoteDefaultRepositoryContent */ -@Service( "remoteRepositoryContent#maven" ) -@Scope( "prototype" ) public class RemoteDefaultRepositoryContent extends AbstractDefaultRepositoryContent implements RemoteRepositoryContent { private RemoteRepository repository; + + public RemoteDefaultRepositoryContent( List artifactMappingProviders ) { + super(artifactMappingProviders); + } + @Override public String getId( ) { diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/ManagedDefaultRepositoryContentTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/ManagedDefaultRepositoryContentTest.java index 86749a224..41e62faa3 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/ManagedDefaultRepositoryContentTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/ManagedDefaultRepositoryContentTest.java @@ -28,6 +28,7 @@ import org.apache.archiva.model.ProjectReference; import org.apache.archiva.model.VersionedReference; import org.apache.archiva.repository.EditableManagedRepository; import org.apache.archiva.repository.ManagedRepositoryContent; +import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent; import org.apache.archiva.repository.layout.LayoutException; import org.apache.archiva.repository.maven2.MavenManagedRepository; import org.junit.Before; @@ -52,9 +53,7 @@ import static org.junit.Assert.fail; public class ManagedDefaultRepositoryContentTest extends AbstractDefaultRepositoryContentTestCase { - @Inject - @Named ( "managedRepositoryContent#default" ) - private ManagedRepositoryContent repoContent; + private ManagedDefaultRepositoryContent repoContent; @Inject FileTypes fileTypes; @@ -63,6 +62,9 @@ public class ManagedDefaultRepositoryContentTest @Named ( "archivaConfiguration#default" ) ArchivaConfiguration archivaConfiguration; + @Inject + List artifactMappingProviders; + @Before public void setUp() throws Exception @@ -77,6 +79,7 @@ public class ManagedDefaultRepositoryContentTest fileTypes.afterConfigurationChange( null, "fileType", null ); + repoContent = new ManagedDefaultRepositoryContent(artifactMappingProviders, fileTypes); //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" ); repoContent.setRepository( repository ); } diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/RemoteDefaultRepositoryContentTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/RemoteDefaultRepositoryContentTest.java index da3f4095f..0a1c03acf 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/RemoteDefaultRepositoryContentTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/RemoteDefaultRepositoryContentTest.java @@ -22,11 +22,13 @@ package org.apache.archiva.metadata.repository.storage.maven2; import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.RemoteRepositoryContent; +import org.apache.archiva.repository.content.maven2.RemoteDefaultRepositoryContent; import org.apache.archiva.repository.layout.LayoutException; import org.junit.Before; import javax.inject.Inject; import javax.inject.Named; +import java.util.List; /** * RemoteDefaultRepositoryContentTest @@ -34,8 +36,10 @@ import javax.inject.Named; public class RemoteDefaultRepositoryContentTest extends AbstractDefaultRepositoryContentTestCase { + @Inject - @Named ( "remoteRepositoryContent#default" ) + private List artifactMappingProviders; + private RemoteRepositoryContent repoContent; @Before @@ -45,6 +49,7 @@ public class RemoteDefaultRepositoryContentTest RemoteRepository repository = createRemoteRepository( "testRemoteRepo", "Unit Test Remote Repo", "http://repo1.maven.org/maven2/" ); + repoContent = new RemoteDefaultRepositoryContent(artifactMappingProviders); //repoContent = (RemoteRepositoryContent) lookup( RemoteRepositoryContent.class, "default" ); repoContent.setRepository( repository ); } diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/AbstractRepositoryLayerTestCase.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/AbstractRepositoryLayerTestCase.java index 1a6cf7cf9..98091f3f4 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/AbstractRepositoryLayerTestCase.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/AbstractRepositoryLayerTestCase.java @@ -73,9 +73,9 @@ public abstract class AbstractRepositoryLayerTestCase repo.setLocation( location.toAbsolutePath().toUri() ); repo.setLayout( layout ); + RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class ); ManagedRepositoryContent repoContent = - applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class ); - repoContent.setRepository( repo ); + provider.createManagedContent( repo ); return repoContent; } @@ -87,9 +87,9 @@ public abstract class AbstractRepositoryLayerTestCase repo.setLocation( new URI( url ) ); repo.setLayout( layout ); + RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class ); RemoteRepositoryContent repoContent = - applicationContext.getBean( "remoteRepositoryContent#" + layout, RemoteRepositoryContent.class ); - repoContent.setRepository( repo ); + provider.createRemoteContent( repo ); return repoContent; } diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/content/maven2/RepositoryRequestTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/content/maven2/RepositoryRequestTest.java index 17068d642..928482b18 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/content/maven2/RepositoryRequestTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/content/maven2/RepositoryRequestTest.java @@ -23,6 +23,7 @@ import org.apache.archiva.common.utils.FileUtils; import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.ManagedRepositoryContent; +import org.apache.archiva.repository.RepositoryContentProvider; import org.apache.archiva.repository.layout.LayoutException; import org.apache.archiva.repository.maven2.MavenManagedRepository; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; @@ -518,9 +519,10 @@ public class RepositoryRequestTest repo.setLocation( location.toAbsolutePath().toUri() ); repo.setLayout( layout ); + RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class ); + ManagedRepositoryContent repoContent = - applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class ); - repoContent.setRepository( repo ); + provider.createManagedContent( repo ); return repoContent; } diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/metadata/MetadataToolsTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/metadata/MetadataToolsTest.java index 45164862e..650f49fff 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/metadata/MetadataToolsTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/metadata/MetadataToolsTest.java @@ -32,6 +32,7 @@ import org.apache.archiva.policies.SnapshotsPolicy; import org.apache.archiva.repository.AbstractRepositoryLayerTestCase; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.RemoteRepositoryContent; +import org.apache.archiva.repository.RepositoryContentProvider; import org.apache.archiva.repository.layout.LayoutException; import org.apache.archiva.repository.maven2.MavenManagedRepository; import org.apache.commons.io.FileUtils; @@ -365,9 +366,11 @@ public class MetadataToolsTest MavenManagedRepository repo = createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRootDir ); + + RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class ); + ManagedRepositoryContent repoContent = - applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class ); - repoContent.setRepository( repo ); + provider.createManagedContent( repo ); Set testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference ); @@ -618,9 +621,10 @@ public class MetadataToolsTest MavenManagedRepository repoConfig = createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRoot ); + RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class ); + ManagedRepositoryContent repoContent = - applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class ); - repoContent.setRepository( repoConfig ); + provider.createManagedContent( repoConfig ); return repoContent; } diff --git a/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-no-mock-conf.xml b/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-no-mock-conf.xml index 8b00cdcdf..b5de647ad 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-no-mock-conf.xml +++ b/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-no-mock-conf.xml @@ -29,8 +29,6 @@ - - \ No newline at end of file diff --git a/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-repo-request-test.xml b/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-repo-request-test.xml index 1601f2ae4..3f28158ae 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-repo-request-test.xml +++ b/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context-repo-request-test.xml @@ -35,10 +35,6 @@ - - - - diff --git a/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context.xml b/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context.xml index 5f445d75a..197d7e409 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context.xml +++ b/archiva-modules/plugins/maven2-repository/src/test/resources/spring-context.xml @@ -33,7 +33,6 @@ - \ No newline at end of file -- 2.39.5