From: Martin Stockhammer Date: Sun, 15 Oct 2017 21:36:30 +0000 (+0200) Subject: Improving repository provider X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4a3d11737e0abf85c6e14498887f6c1d6dadd776;p=archiva.git Improving repository provider --- diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRemoteRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRemoteRepository.java index 4d2842342..e1e570a78 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRemoteRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRemoteRepository.java @@ -39,7 +39,7 @@ public abstract class AbstractRemoteRepository extends AbstractRepository implem private Map uExtraParameters = Collections.unmodifiableMap( extraParameters ); private Map extraHeaders = new HashMap<>( ); private Map uExtraHeaders = Collections.unmodifiableMap( extraHeaders ); - private Duration timeout; + private Duration timeout = Duration.ofSeconds( 60 ); private String proxyId; private RemoteRepositoryContent content; diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java index ad55d5c46..a517d84d7 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java @@ -52,7 +52,6 @@ public abstract class AbstractRepository implements EditableRepository private Set uFailoverLocations = Collections.unmodifiableSet( failoverLocations ); private boolean scanned = true; String schedulingDefinition = "0 0 02 * *"; - private boolean index = true; private String layout; public static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); @@ -134,10 +133,7 @@ public abstract class AbstractRepository implements EditableRepository } @Override - public boolean hasIndex( ) - { - return index; - } + public abstract boolean hasIndex( ); @Override public String getLayout( ) @@ -208,12 +204,6 @@ public abstract class AbstractRepository implements EditableRepository this.scanned = scanned; } - @Override - public void setIndex( boolean hasIndex ) - { - this.index = hasIndex; - } - @Override public void setLayout( String layout ) { diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java index 3af558080..67154223e 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/EditableRepository.java @@ -107,12 +107,6 @@ public interface EditableRepository extends Repository */ void setSchedulingDefinition(String cronExpression) throws IllegalArgumentException; - /** - * Set true, if the repository has indexes stored. - * @param hasIndex true, if the repository has indexes, otherwise false. - */ - void setIndex(boolean hasIndex); - /** * Sets the layout string. * @param layout diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryProvider.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryProvider.java index 0d5fd7eb9..9a4c8f1cf 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryProvider.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryProvider.java @@ -36,5 +36,11 @@ public interface RepositoryProvider ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration) throws RepositoryException; + ManagedRepository createStagingInstance(ManagedRepositoryConfiguration baseConfiguration) throws RepositoryException; + RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration) throws RepositoryException; + + RemoteRepositoryConfiguration getRemoteConfiguration(RemoteRepository remoteRepository) throws RepositoryException; + + ManagedRepositoryConfiguration getManagedConfiguration(ManagedRepository managedRepository) throws RepositoryException; } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java index 8f608d228..a5273a612 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java @@ -23,6 +23,7 @@ import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.archiva.repository.features.StagingRepositoryFeature; +import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -30,16 +31,20 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Registry for repositories */ -@Service("repositoryRegistry") +@Service( "repositoryRegistry" ) public class RepositoryRegistry { @@ -57,98 +62,135 @@ public class RepositoryRegistry @Inject RepositoryContentFactory repositoryContentFactory; - private Map managedRepositories = new HashMap<>( ); - private Map remoteRepositories = new HashMap<>( ); + private Map managedRepositories = new HashMap<>( ); + private Map uManagedRepository = Collections.unmodifiableMap( managedRepositories ); + + private Map remoteRepositories = new HashMap<>( ); + private Map uRemoteRepositories = Collections.unmodifiableMap( remoteRepositories ); + + private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock( ); @PostConstruct - private void initialize() { - managedRepositories = getManagedRepositoriesFromConfig(); - remoteRepositories = getRemoteRepositoriesFromConfig(); + private void initialize( ) + { + rwLock.writeLock( ).lock( ); + try + { + managedRepositories.clear( ); + managedRepositories.putAll( getManagedRepositoriesFromConfig( ) ); + remoteRepositories.clear( ); + remoteRepositories.putAll( getRemoteRepositoriesFromConfig( ) ); + } + finally + { + rwLock.writeLock( ).unlock( ); + } } - private Map getProviderMap() { - Map map = new HashMap<>( ); - if (repositoryProviders!=null) { - for(RepositoryProvider provider : repositoryProviders) { - for (RepositoryType type : provider.provides()) { - map.put(type, provider); + private Map getProviderMap( ) + { + Map map = new HashMap<>( ); + if ( repositoryProviders != null ) + { + for ( RepositoryProvider provider : repositoryProviders ) + { + for ( RepositoryType type : provider.provides( ) ) + { + map.put( type, provider ); } } } return map; } - private Map getManagedRepositoriesFromConfig() + private Map getManagedRepositoriesFromConfig( ) { List managedRepoConfigs = - getArchivaConfiguration().getConfiguration().getManagedRepositories(); + getArchivaConfiguration( ).getConfiguration( ).getManagedRepositories( ); if ( managedRepoConfigs == null ) { - return Collections.emptyMap(); + return Collections.emptyMap( ); } - Map managedRepos = new LinkedHashMap<>( managedRepoConfigs.size() ); + Map managedRepos = new LinkedHashMap<>( managedRepoConfigs.size( ) ); Map providerMap = getProviderMap( ); for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs ) { RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) ); - if (providerMap.containsKey( repositoryType )) { + if ( providerMap.containsKey( repositoryType ) ) + { try { ManagedRepository repo = createNewManagedRepository( providerMap.get( repositoryType ), repoConfig ); managedRepos.put(repo.getId(), repo); - } catch (Exception e) { - log.error("Could not create managed repository {}: {}", repoConfig.getId(), e.getMessage(), e); + } + catch ( Exception e ) + { + log.error( "Could not create managed repository {}: {}", repoConfig.getId( ), e.getMessage( ), e ); } } } - return managedRepos; } - private ManagedRepository createNewManagedRepository(RepositoryProvider provider, ManagedRepositoryConfiguration cfg) throws RepositoryException + private ManagedRepository createNewManagedRepository( RepositoryProvider provider, ManagedRepositoryConfiguration cfg ) throws RepositoryException { ManagedRepository repo = provider.createManagedInstance( cfg ); - if (repo.supportsFeature( StagingRepositoryFeature.class )) { - StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get(); - if (feature.isStageRepoNeeded()) { - ManagedRepository stageRepo = getManagedRepository( repo.getId()+StagingRepositoryFeature.STAGING_REPO_POSTFIX ); + if ( repo.supportsFeature( StagingRepositoryFeature.class ) ) + { + StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get( ); + if ( feature.isStageRepoNeeded( ) ) + { + ManagedRepository stageRepo = getStageRepository( provider, cfg ); feature.setStagingRepository( stageRepo ); } } - if (repo instanceof EditableManagedRepository) + if ( repo instanceof EditableManagedRepository ) { - ((EditableManagedRepository)repo).setContent( repositoryContentFactory.getManagedRepositoryContent( repo.getId( ) ) ); + ( (EditableManagedRepository) repo ).setContent( repositoryContentFactory.getManagedRepositoryContent( repo.getId( ) ) ); } return repo; } - private Map getRemoteRepositoriesFromConfig() { + private ManagedRepository getStageRepository( RepositoryProvider provider, ManagedRepositoryConfiguration baseRepoCfg ) throws RepositoryException + { + ManagedRepository stageRepo = getManagedRepository( baseRepoCfg.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX ); + if ( stageRepo == null ) + { + stageRepo = provider.createStagingInstance( baseRepoCfg ); + } + return stageRepo; + } + + + private Map getRemoteRepositoriesFromConfig( ) + { List remoteRepoConfigs = - getArchivaConfiguration().getConfiguration().getRemoteRepositories(); + getArchivaConfiguration( ).getConfiguration( ).getRemoteRepositories( ); if ( remoteRepoConfigs == null ) { - return Collections.emptyMap(); + return Collections.emptyMap( ); } - Map remoteRepos = new LinkedHashMap<>( remoteRepoConfigs.size() ); + Map remoteRepos = new LinkedHashMap<>( remoteRepoConfigs.size( ) ); Map providerMap = getProviderMap( ); for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs ) { RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) ); - if (providerMap.containsKey( repositoryType )) { + if ( providerMap.containsKey( repositoryType ) ) + { try { - remoteRepos.put(repoConfig.getId(), providerMap.get(repositoryType).createRemoteInstance( repoConfig )); + remoteRepos.put( repoConfig.getId( ), providerMap.get( repositoryType ).createRemoteInstance( repoConfig ) ); } catch ( Exception e ) { - log.error("Could not create repository {} from config: {}", repoConfig.getId(), e.getMessage(), e); + log.error( "Could not create repository {} from config: {}", repoConfig.getId( ), e.getMessage( ), e ); } } } @@ -156,35 +198,102 @@ public class RepositoryRegistry return remoteRepos; } - private ArchivaConfiguration getArchivaConfiguration() { + private ArchivaConfiguration getArchivaConfiguration( ) + { return this.archivaConfiguration; } - public List getRepositories() { - ArrayList li = new ArrayList<>( ); - li.addAll(managedRepositories.values()); - li.addAll(remoteRepositories.values()); - return Collections.unmodifiableList( li ); + public Collection getRepositories( ) + { + rwLock.readLock( ).lock( ); + try + { + return Stream.concat( managedRepositories.values( ).stream( ), remoteRepositories.values( ).stream( ) ).collect( Collectors.toList( ) ); + } + finally + { + rwLock.readLock( ).unlock( ); + } } - public List getManagedRepositories() { - return Collections.unmodifiableList( new ArrayList(managedRepositories.values()) ); + public Collection getManagedRepositories( ) + { + return uManagedRepository.values( ); } - public List getRemoteRepositories() { - return Collections.unmodifiableList( new ArrayList(remoteRepositories.values()) ); + public Collection getRemoteRepositories( ) + { + return uRemoteRepositories.values( ); } - public Repository getRepository(String repoId) { - return null; + public Repository getRepository( String repoId ) + { + rwLock.readLock( ).lock( ); + try + { + if ( managedRepositories.containsKey( repoId ) ) + { + return managedRepositories.get( repoId ); + } + else + { + return remoteRepositories.get( repoId ); + } + } + finally + { + rwLock.readLock( ).unlock( ); + } } - public ManagedRepository getManagedRepository(String repoId) { - return null; + public ManagedRepository getManagedRepository( String repoId ) + { + rwLock.readLock().lock(); + try + { + return managedRepositories.get( repoId ); + } finally + { + rwLock.readLock().unlock(); + } } - public RemoteRepository getRemoteRepository(String repoId) { - return null; + public RemoteRepository getRemoteRepository( String repoId ) + { + rwLock.readLock().lock(); + try + { + return remoteRepositories.get( repoId ); + } finally + { + rwLock.readLock().unlock(); + } + } + + public void addRepository( ManagedRepository managedRepository ) + { + rwLock.writeLock( ).lock( ); + try + { + managedRepositories.put( managedRepository.getId( ), managedRepository ); + } + finally + { + rwLock.writeLock( ).unlock( ); + } + } + + public void addRepository( RemoteRepository remoteRepository ) + { + rwLock.writeLock( ).lock( ); + try + { + remoteRepositories.put( remoteRepository.getId( ), remoteRepository ); + } + finally + { + rwLock.writeLock( ).unlock( ); + } } } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java index 38c44c83e..276232e82 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java @@ -20,6 +20,8 @@ package org.apache.archiva.repository.features; */ +import org.apache.commons.lang.StringUtils; + import java.net.URI; /** @@ -82,4 +84,7 @@ public class IndexCreationFeature implements RepositoryFeature { this.proxyId = proxyId; } + + /** + * Returns true, if there is a index available. + * + * @return + */ + public boolean hasIndex() { + return this.indexUri!=null && !StringUtils.isEmpty( this.indexUri.getPath() ); + } } diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenManagedRepository.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenManagedRepository.java index 8f885c23a..f9b169808 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenManagedRepository.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenManagedRepository.java @@ -101,7 +101,9 @@ public class MavenManagedRepository extends AbstractManagedRepository return false; } - - - + @Override + public boolean hasIndex( ) + { + return indexCreationFeature.hasIndex(); + } } diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRemoteRepository.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRemoteRepository.java index 9887a056d..224177d74 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRemoteRepository.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRemoteRepository.java @@ -40,13 +40,14 @@ import java.util.Locale; public class MavenRemoteRepository extends AbstractRemoteRepository implements RemoteRepository { - private RemoteIndexFeature remoteIndexFeature = new RemoteIndexFeature(); + final private RemoteIndexFeature remoteIndexFeature = new RemoteIndexFeature(); + final private IndexCreationFeature indexCreationFeature = new IndexCreationFeature( ); private static final RepositoryCapabilities CAPABILITIES = new StandardCapabilities( new ReleaseScheme[] { ReleaseScheme.RELEASE, ReleaseScheme.SNAPSHOT }, new String[] { MavenManagedRepository.DEFAULT_LAYOUT, MavenManagedRepository.LEGACY_LAYOUT}, new String[] {}, - new String[] {RemoteIndexFeature.class.getName()}, + new String[] {RemoteIndexFeature.class.getName(), IndexCreationFeature.class.getName()}, true, true, true, @@ -64,6 +65,12 @@ public class MavenRemoteRepository extends AbstractRemoteRepository super( primaryLocale, RepositoryType.MAVEN, id, name ); } + @Override + public boolean hasIndex( ) + { + return remoteIndexFeature.hasIndex(); + } + @Override public RepositoryCapabilities getCapabilities( ) { @@ -75,6 +82,8 @@ public class MavenRemoteRepository extends AbstractRemoteRepository { if (RemoteIndexFeature.class.equals( clazz )) { return (RepositoryFeature) remoteIndexFeature; + } else if (IndexCreationFeature.class.equals(clazz)) { + return (RepositoryFeature) indexCreationFeature; } else { throw new UnsupportedFeatureException( ); } @@ -83,7 +92,7 @@ public class MavenRemoteRepository extends AbstractRemoteRepository @Override public > boolean supportsFeature( Class clazz ) { - if ( RemoteIndexFeature.class.equals(clazz)) { + if ( RemoteIndexFeature.class.equals(clazz) || IndexCreationFeature.class.equals(clazz)) { return true; } else { return false; diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java index 2f862fee4..0df46964b 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java @@ -27,6 +27,7 @@ import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.PasswordCredentials; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.RemoteRepository; +import org.apache.archiva.repository.RepositoryCredentials; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryProvider; import org.apache.archiva.repository.RepositoryType; @@ -35,18 +36,21 @@ import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; import org.apache.archiva.repository.features.StagingRepositoryFeature; import org.apache.commons.lang.StringUtils; +import org.apache.http.auth.UsernamePasswordCredentials; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.time.Duration; import java.time.Period; +import java.time.temporal.ChronoUnit; import java.util.HashSet; import java.util.Locale; import java.util.Set; -import java.util.regex.Pattern; /** * Provider for the maven2 repository implementations @@ -68,27 +72,30 @@ public class MavenRepositoryProvider implements RepositoryProvider return TYPES; } - private URI getURIFromConfig(String config) throws RepositoryException { + private URI getURIFromString( String uriStr) throws RepositoryException { URI uri; try { - uri = new URI(config); - if (uri.getScheme()==null) { - uri = new URI("file://"+config); + if (uriStr.startsWith("/")) { + // only absolute paths are prepended with file scheme + uri = new URI("file://"+uriStr); + } else + { + uri = new URI( uriStr ); } - if (!"file".equals(uri.getScheme())) { + if (uri.getScheme()!=null && !"file".equals(uri.getScheme())) { log.error("Bad URI scheme found: {}, URI={}", uri.getScheme(), uri); - throw new RepositoryException("The uri "+config+" is not valid. Only file:// URI is allowed for maven."); + throw new RepositoryException("The uri "+uriStr+" is not valid. Only file:// URI is allowed for maven."); } } catch (URISyntaxException e) { - String newCfg = "file://"+config; + String newCfg = "file://"+uriStr; try { uri = new URI(newCfg); } catch ( URISyntaxException e1 ) { - log.error("Could not create URI from {} -> ", config, newCfg); - throw new RepositoryException( "The config entry "+config+" cannot be converted to URI." ); + log.error("Could not create URI from {} -> ", uriStr, newCfg); + throw new RepositoryException( "The config entry "+uriStr+" cannot be converted to URI." ); } } return uri; @@ -98,7 +105,7 @@ public class MavenRepositoryProvider implements RepositoryProvider public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration cfg ) throws RepositoryException { MavenManagedRepository repo = new MavenManagedRepository(cfg.getId() ,cfg.getName()); - repo.setLocation( getURIFromConfig( cfg.getLocation() ) ); + repo.setLocation( getURIFromString( cfg.getLocation() ) ); setBaseConfig( repo, cfg ); repo.setSchedulingDefinition(cfg.getRefreshCronExpression()); repo.setBlocksRedeployment( cfg.isBlockRedeployments() ); @@ -113,12 +120,10 @@ public class MavenRepositoryProvider implements RepositoryProvider StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature( StagingRepositoryFeature.class ).get(); stagingRepositoryFeature.setStageRepoNeeded( cfg.isStageRepoNeeded() ); - // TODO: staging repository -> here or in repositoryregistry? - IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get( ); indexCreationFeature.setSkipPackedIndexCreation( cfg.isSkipPackedIndexCreation() ); - indexCreationFeature.setIndexPath( getURIFromConfig( cfg.getIndexDir() ) ); + indexCreationFeature.setIndexPath( getURIFromString( cfg.getIndexDir() ) ); ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get(); @@ -129,10 +134,15 @@ public class MavenRepositoryProvider implements RepositoryProvider return repo; } + @Override + public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration baseConfiguration ) throws RepositoryException + { + return createManagedInstance( getStageRepoConfig( baseConfiguration ) ); + } @Override - public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration cfg ) + public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration cfg ) throws RepositoryException { MavenRemoteRepository repo = new MavenRemoteRepository( cfg.getId( ), cfg.getName( ) ); setBaseConfig( repo, cfg ); @@ -146,6 +156,7 @@ public class MavenRepositoryProvider implements RepositoryProvider { log.error("Could not set remote url "+cfg.getUrl()); } + repo.setTimeout( Duration.ofSeconds( cfg.getTimeout() ) ); RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class ).get(); remoteIndexFeature.setDownloadRemoteIndex( cfg.isDownloadRemoteIndex() ); remoteIndexFeature.setDownloadRemoteIndexOnStartup( cfg.isDownloadRemoteIndexOnStartup() ); @@ -175,34 +186,144 @@ public class MavenRepositoryProvider implements RepositoryProvider } else { credentials.setPassword( new char[0] ); } + if (cfg.getIndexDir()!=null) { + IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get(); + indexCreationFeature.setIndexPath( getURIFromString( cfg.getIndexDir() ) ); + } return repo; } - private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg) { - repo.setDescription( Locale.getDefault( ), cfg.getDescription() ); - String indexDir = cfg.getIndexDir(); - try + @Override + public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException + { + if (!(remoteRepository instanceof MavenRemoteRepository)) { + log.error("Wrong remote repository type "+remoteRepository.getClass().getName()); + throw new RepositoryException( "The given repository type cannot be handled by the maven provider: "+remoteRepository.getClass().getName() ); + } + RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration(); + cfg.setType( remoteRepository.getType().toString() ); + cfg.setId( remoteRepository.getId() ); + cfg.setName( remoteRepository.getName() ); + cfg.setDescription( remoteRepository.getDescription() ); + cfg.setUrl(remoteRepository.getLocation().toString()); + cfg.setTimeout( (int)remoteRepository.getTimeout().toMillis()/1000 ); + cfg.setCheckPath( remoteRepository.getCheckPath() ); + RepositoryCredentials creds = remoteRepository.getLoginCredentials( ); + if (creds!=null) { - if ( StringUtils.isEmpty( indexDir )) { - repo.setIndex( false ); - } else - { - if ( indexDir.startsWith( "file://" ) ) - { - //repo.setIndexPath( new URI( indexDir ) ); - } - else - { - //repo.setIndexPath( new URI( "file://" + indexDir ) ); - } + if (creds instanceof PasswordCredentials) { + PasswordCredentials pCreds = (PasswordCredentials) creds; + cfg.setPassword( new String(pCreds.getPassword()) ); + cfg.setUsername( pCreds.getUsername() ); } } - catch ( Exception e ) + cfg.setLayout( remoteRepository.getLayout() ); + cfg.setExtraParameters( remoteRepository.getExtraParameters() ); + cfg.setExtraHeaders( remoteRepository.getExtraHeaders() ); + cfg.setRefreshCronExpression( remoteRepository.getSchedulingDefinition() ); + + IndexCreationFeature indexCreationFeature = remoteRepository.getFeature( IndexCreationFeature.class ).get(); + cfg.setIndexDir( indexCreationFeature.getIndexPath().toString()); + + RemoteIndexFeature remoteIndexFeature = remoteRepository.getFeature( RemoteIndexFeature.class ).get(); + cfg.setRemoteIndexUrl( remoteIndexFeature.getIndexUri().toString() ); + cfg.setRemoteDownloadTimeout( (int)remoteIndexFeature.getDownloadTimeout().get( ChronoUnit.SECONDS ) ); + cfg.setDownloadRemoteIndexOnStartup( remoteIndexFeature.isDownloadRemoteIndexOnStartup() ); + cfg.setDownloadRemoteIndex( remoteIndexFeature.isDownloadRemoteIndex() ); + cfg.setRemoteDownloadNetworkProxyId( remoteIndexFeature.getProxyId() ); + + + + + return cfg; + + } + + @Override + public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException + { + if (!(managedRepository instanceof MavenManagedRepository)) { + log.error("Wrong remote repository type "+managedRepository.getClass().getName()); + throw new RepositoryException( "The given repository type cannot be handled by the maven provider: "+managedRepository.getClass().getName() ); + } + ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration(); + cfg.setType( managedRepository.getType().toString() ); + cfg.setId( managedRepository.getId() ); + cfg.setName( managedRepository.getName() ); + cfg.setDescription( managedRepository.getDescription() ); + cfg.setLocation( managedRepository.getLocation().toString() ); + cfg.setLayout( managedRepository.getLayout() ); + cfg.setRefreshCronExpression( managedRepository.getSchedulingDefinition() ); + cfg.setScanned( managedRepository.isScanned() ); + cfg.setBlockRedeployments( managedRepository.blocksRedeployments() ); + StagingRepositoryFeature stagingRepositoryFeature = managedRepository.getFeature( StagingRepositoryFeature.class ).get(); + cfg.setStageRepoNeeded(stagingRepositoryFeature.isStageRepoNeeded()); + + + IndexCreationFeature indexCreationFeature = managedRepository.getFeature( IndexCreationFeature.class ).get(); + cfg.setIndexDir( indexCreationFeature.getIndexPath().toString()); + cfg.setSkipPackedIndexCreation( indexCreationFeature.isSkipPackedIndexCreation() ); + + ArtifactCleanupFeature artifactCleanupFeature = managedRepository.getFeature( ArtifactCleanupFeature.class ).get(); + cfg.setRetentionCount( artifactCleanupFeature.getRetentionCount()); + cfg.setRetentionTime( artifactCleanupFeature.getRetentionTime().getDays() ); + cfg.setDeleteReleasedSnapshots(artifactCleanupFeature.isDeleteReleasedSnapshots()); + + if (managedRepository.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )) { + cfg.setReleases( true ); + } else { + cfg.setReleases( false ); + } + if (managedRepository.getActiveReleaseSchemes().contains( ReleaseScheme.SNAPSHOT )) { + cfg.setSnapshots( true ); + } else { + cfg.setSnapshots( false ); + } + return cfg; + + } + + private ManagedRepositoryConfiguration getStageRepoConfig( ManagedRepositoryConfiguration repository ) + { + ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration(); + stagingRepository.setId( repository.getId() + StagingRepositoryFeature.STAGING_REPO_POSTFIX ); + stagingRepository.setLayout( repository.getLayout() ); + stagingRepository.setName( repository.getName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX ); + stagingRepository.setBlockRedeployments( repository.isBlockRedeployments() ); + stagingRepository.setRetentionTime( repository.getRetentionTime() ); + stagingRepository.setDeleteReleasedSnapshots( repository.isDeleteReleasedSnapshots() ); + + String path = repository.getLocation(); + int lastIndex = path.replace( '\\', '/' ).lastIndexOf( '/' ); + stagingRepository.setLocation( path.substring( 0, lastIndex ) + "/" + stagingRepository.getId() ); + + if ( StringUtils.isNotBlank( repository.getIndexDir() ) ) { - log.error("Could not set index path "+cfg.getIndexDir()); - repo.setIndex(false); + Path indexDir = Paths.get( repository.getIndexDir() ); + // in case of absolute dir do not use the same + if ( indexDir.isAbsolute() ) + { + stagingRepository.setIndexDir( stagingRepository.getLocation() + "/.index" ); + } + else + { + stagingRepository.setIndexDir( repository.getIndexDir() ); + } } + stagingRepository.setRefreshCronExpression( repository.getRefreshCronExpression() ); + stagingRepository.setReleases( repository.isReleases() ); + stagingRepository.setRetentionCount( repository.getRetentionCount() ); + stagingRepository.setScanned( repository.isScanned() ); + stagingRepository.setSnapshots( repository.isSnapshots() ); + stagingRepository.setSkipPackedIndexCreation( repository.isSkipPackedIndexCreation() ); + // do not duplicate description + //stagingRepository.getDescription("") + return stagingRepository; + } + + private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg) { + repo.setDescription( repo.getPrimaryLocale(), cfg.getDescription() ); repo.setLayout( cfg.getLayout() ); } diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java index 9a08f070b..aa5ddefb5 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java @@ -23,6 +23,7 @@ import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.archiva.repository.ManagedRepository; +import org.apache.archiva.repository.PasswordCredentials; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.RepositoryType; @@ -35,26 +36,27 @@ import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.sonatype.aether.util.layout.RepositoryLayout; import org.springframework.test.context.ContextConfiguration; import javax.inject.Inject; import javax.inject.Named; +import java.net.URI; +import java.time.Duration; import java.time.Period; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; +import java.util.HashMap; +import java.util.Map; import static org.junit.Assert.*; /** * @author Martin Stockhammer */ -@RunWith( ArchivaSpringJUnit4ClassRunner.class ) -@ContextConfiguration( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-no-mock-conf.xml" } ) public class MavenRepositoryProviderTest { - @Inject - @Named( "archivaConfiguration#default" ) - ArchivaConfiguration archivaConfiguration; - MavenRepositoryProvider provider; @@ -75,32 +77,51 @@ public class MavenRepositoryProviderTest @Test public void createManagedInstance( ) throws Exception { - assertNotNull(archivaConfiguration); - assertNotNull(archivaConfiguration.getConfiguration()); - ManagedRepositoryConfiguration repo = archivaConfiguration.getConfiguration().getManagedRepositories().get(0); + ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration( ); + repo.setId("testm001"); + repo.setName("Managed Test Repo 001"); + repo.setDescription( "This is a managed test" ); + repo.setRetentionTime( 37 ); + repo.setLocation( "file:///test/dir/repository" ); + repo.setSnapshots( true ); + repo.setReleases( true ); + repo.setRefreshCronExpression( "4 0 0 ? * TUE" ); + repo.setScanned( true ); + repo.setBlockRedeployments( true ); + repo.setDeleteReleasedSnapshots( true ); + repo.setRetentionCount( 33 ); + repo.setSkipPackedIndexCreation( true ); + repo.setStageRepoNeeded( true ); + repo.setIndexDir( "testmanaged/.index" ); + repo.setLayout( "maven2" ); + repo.setType( RepositoryType.MAVEN.toString() ); + + ManagedRepository mr = provider.createManagedInstance( repo ); assertNotNull(mr.getLocation()); - assertTrue(mr.getLocation().toString().endsWith( "/repositories/internal" )); - assertEquals("Archiva Managed Internal Repository", mr.getName()); - assertEquals(1, mr.getActiveReleaseSchemes().size()); - assertEquals( ReleaseScheme.RELEASE, mr.getActiveReleaseSchemes().iterator().next()); - assertEquals("internal", mr.getId()); + assertEquals("file:///test/dir/repository", mr.getLocation().toString()); + assertEquals("This is a managed test", mr.getDescription()); + assertEquals("Managed Test Repo 001", mr.getName()); + assertEquals(2, mr.getActiveReleaseSchemes().size()); + assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )); + assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.SNAPSHOT)); + assertEquals("testm001", mr.getId()); assertTrue(mr.blocksRedeployments()); - assertEquals("0 0 * * * ?", mr.getSchedulingDefinition()); + assertEquals("4 0 0 ? * TUE", mr.getSchedulingDefinition()); assertTrue(mr.isScanned()); ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class ).get(); - assertEquals( Period.ofDays( 30), artifactCleanupFeature.getRetentionTime()); - assertFalse(artifactCleanupFeature.isDeleteReleasedSnapshots()); - assertEquals(2, artifactCleanupFeature.getRetentionCount()); + assertEquals( Period.ofDays( 37), artifactCleanupFeature.getRetentionTime()); + assertTrue(artifactCleanupFeature.isDeleteReleasedSnapshots()); + assertEquals(33, artifactCleanupFeature.getRetentionCount()); IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get(); assertNotNull(indexCreationFeature.getIndexPath()); - assertTrue(indexCreationFeature.getIndexPath().toString().endsWith("/repositories/internal/.indexer")); - assertTrue(indexCreationFeature.getIndexPath().isAbsolute()); - assertFalse(indexCreationFeature.isSkipPackedIndexCreation()); + assertEquals("testmanaged/.index", indexCreationFeature.getIndexPath().toString()); + assertFalse(indexCreationFeature.getIndexPath().isAbsolute()); + assertTrue(indexCreationFeature.isSkipPackedIndexCreation()); StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class ).get(); - assertFalse(stagingRepositoryFeature.isStageRepoNeeded()); + assertTrue(stagingRepositoryFeature.isStageRepoNeeded()); assertNull(stagingRepositoryFeature.getStagingRepository()); @@ -109,17 +130,51 @@ public class MavenRepositoryProviderTest @Test public void createRemoteInstance( ) throws Exception { - assertNotNull(archivaConfiguration); - assertNotNull(archivaConfiguration.getConfiguration()); - RemoteRepositoryConfiguration repo = archivaConfiguration.getConfiguration().getRemoteRepositories().get(0); + RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration( ); + repo.setUsername("testuser001"); + repo.setPassword( "pwd0000abc" ); + repo.setCheckPath( "test/check.html" ); + repo.setTimeout( 50 ); + repo.setUrl( "https://repo.maven.apache.org/maven2/test" ); + repo.setDownloadRemoteIndex( true ); + repo.setDownloadRemoteIndexOnStartup( true ); + Map header = new HashMap( ); + header.put("header1","value1"); + header.put("header2","value2"); + repo.setExtraHeaders( header ); + Map params = new HashMap( ); + params.put("param1","pval1"); + params.put("param2","pval2"); + repo.setExtraParameters( params ); + repo.setRefreshCronExpression( "0 1 07 ? * MON" ); + repo.setRemoteDownloadTimeout( 333 ); + repo.setRemoteIndexUrl( "testremote/.index" ); + repo.setDescription( "This is a test" ); + repo.setId( "test001" ); + repo.setName( "Remote Test Repo 001" ); + repo.setIndexDir( "testindex/.index" ); + repo.setLayout( "maven2" ); + repo.setType( RepositoryType.MAVEN.toString() ); + repo.setIndexDir( "local/.index" ); + RemoteRepository mr = provider.createRemoteInstance( repo ); + assertEquals("test001", mr.getId()); + assertEquals("This is a test", mr.getDescription()); assertNotNull(mr.getLocation()); - assertEquals("https://repo.maven.apache.org/maven2", mr.getLocation().toString()); - assertEquals("Central Repository", mr.getName()); - assertEquals("central", mr.getId()); - assertEquals("0 0 08 ? * SUN", mr.getSchedulingDefinition()); + assertEquals("https://repo.maven.apache.org/maven2/test", mr.getLocation().toString()); + assertEquals("Remote Test Repo 001", mr.getName()); + assertEquals("test001", mr.getId()); + assertEquals("0 1 07 ? * MON", mr.getSchedulingDefinition()); + assertEquals(50, mr.getTimeout().get( ChronoUnit.SECONDS )); assertTrue(mr.isScanned()); - assertNull(mr.getLoginCredentials()); + assertNotNull(mr.getLoginCredentials()); + assertTrue(mr.getLoginCredentials() instanceof PasswordCredentials); + PasswordCredentials creds = (PasswordCredentials) mr.getLoginCredentials(); + assertEquals("testuser001", creds.getUsername()); + assertEquals("pwd0000abc", new String(creds.getPassword())); + assertEquals("value1", mr.getExtraHeaders().get("header1")); + assertEquals("pval2", mr.getExtraParameters().get("param2")); + assertEquals( "maven2", mr.getLayout()); try { ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class ).get( ); @@ -128,13 +183,8 @@ public class MavenRepositoryProviderTest // correct } - try - { - IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get( ); - throw new Exception("indexCreationFeature should not be available"); - } catch (UnsupportedFeatureException e) { - // correct - } + IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get( ); + assertEquals("local/.index", indexCreationFeature.getIndexPath().toString()); try { StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class ).get( ); @@ -146,4 +196,84 @@ public class MavenRepositoryProviderTest assertNull(remoteIndexFeature.getProxyId()); } + @Test + public void getManagedConfiguration() throws Exception { + MavenManagedRepository repo = new MavenManagedRepository( "test01", "My Test repo" ); + + repo.setLocation( new URI("https://this.is/a/test") ); + repo.setScanned( true ); + repo.setDescription( repo.getPrimaryLocale(), "This is a description" ); + repo.setLayout( "maven2" ); + repo.setBlocksRedeployment( true ); + repo.setName( repo.getPrimaryLocale(), "test0002" ); + repo.setSchedulingDefinition( "0 0 05 ? * WED" ); + repo.addActiveReleaseScheme( ReleaseScheme.RELEASE ); + repo.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT ); + StagingRepositoryFeature stagingFeat = repo.getFeature( StagingRepositoryFeature.class ).get( ); + stagingFeat.setStageRepoNeeded( true ); + IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get(); + indexCreationFeature.setIndexPath( new URI("test/.indexes") ); + indexCreationFeature.setSkipPackedIndexCreation( true ); + ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get(); + artifactCleanupFeature.setRetentionTime( Period.ofDays( 5 ) ); + artifactCleanupFeature.setRetentionCount( 7 ); + artifactCleanupFeature.setDeleteReleasedSnapshots( true ); + + ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo ); + assertEquals("https://this.is/a/test", cfg.getLocation()); + assertTrue(cfg.isScanned()); + assertEquals( "This is a description", cfg.getDescription() ); + assertEquals("maven2", cfg.getLayout()); + assertTrue(cfg.isBlockRedeployments()); + assertEquals("test0002", cfg.getName()); + assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression()); + assertTrue(cfg.isStageRepoNeeded()); + assertEquals("test/.indexes", cfg.getIndexDir()); + assertTrue(cfg.isSkipPackedIndexCreation()); + assertEquals(5, cfg.getRetentionTime()); + assertEquals(7, cfg.getRetentionCount()); + assertTrue(cfg.isDeleteReleasedSnapshots()); + assertTrue(cfg.isReleases()); + assertTrue(cfg.isSnapshots()); + assertTrue(cfg.isScanned()); + + + + } + + @Test + public void getRemoteConfiguration() throws Exception { + MavenRemoteRepository repo = new MavenRemoteRepository( "test01", "My Test repo" ); + + repo.setLocation( new URI("https://this.is/a/test") ); + repo.setScanned( true ); + repo.setDescription( repo.getPrimaryLocale(), "This is a description" ); + repo.setLayout( "maven2" ); + repo.setName( repo.getPrimaryLocale(), "test0003" ); + repo.setSchedulingDefinition( "0 0 05 ? * WED" ); + RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class ).get(); + remoteIndexFeature.setProxyId( "proxyabc" ); + remoteIndexFeature.setDownloadTimeout( Duration.ofSeconds( 54 ) ); + remoteIndexFeature.setDownloadRemoteIndex( false ); + remoteIndexFeature.setIndexUri( new URI("/this/remote/.index") ); + remoteIndexFeature.setDownloadRemoteIndexOnStartup( true ); + IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get(); + indexCreationFeature.setIndexPath( new URI("/this/local/.index") ); + + RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration( repo ); + assertEquals("https://this.is/a/test", cfg.getUrl()); + assertEquals( "This is a description", cfg.getDescription() ); + assertEquals("maven2", cfg.getLayout()); + assertEquals("test0003", cfg.getName()); + assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression()); + assertEquals("/this/remote/.index", cfg.getRemoteIndexUrl()); + assertEquals("proxyabc", cfg.getRemoteDownloadNetworkProxyId()); + assertEquals(54, cfg.getRemoteDownloadTimeout()); + assertFalse(cfg.isDownloadRemoteIndex()); + assertTrue(cfg.isDownloadRemoteIndexOnStartup()); + assertEquals("/this/local/.index", cfg.getIndexDir()); + + + } + } \ No newline at end of file