diff options
author | Martin Stockhammer <martin_s@apache.org> | 2021-06-04 17:44:30 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2021-06-04 17:44:30 +0200 |
commit | a1b92c562fae957e601875e50f87a3ff93fecfd6 (patch) | |
tree | d2efb37aafbf235b4b3d1d9d9d117ebaa315cc24 /archiva-modules/archiva-maven | |
parent | 0ce53483a85b6b7a3a14417434edf56934b474ba (diff) | |
download | archiva-a1b92c562fae957e601875e50f87a3ff93fecfd6.tar.gz archiva-a1b92c562fae957e601875e50f87a3ff93fecfd6.zip |
Fixing Index context creation
Diffstat (limited to 'archiva-modules/archiva-maven')
4 files changed, 99 insertions, 30 deletions
diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java index b1231844d..234edcd51 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java @@ -19,6 +19,7 @@ package org.apache.archiva.indexer.maven; * under the License. */ +import org.apache.archiva.common.utils.FileUtils; import org.apache.archiva.common.utils.PathUtil; import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.indexer.ArchivaIndexManager; @@ -38,6 +39,7 @@ import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.UnsupportedRepositoryTypeException; +import org.apache.archiva.repository.storage.AssetType; import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.repository.storage.RepositoryStorage; import org.apache.archiva.repository.storage.StorageAsset; @@ -499,7 +501,8 @@ public class MavenIndexManager implements ArchivaIndexManager { StorageAsset newPath = getIndexPath(repo); IndexingContext ctx = context.getBaseContext(IndexingContext.class); Path oldPath = ctx.getIndexDirectoryFile().toPath(); - if (oldPath.equals(newPath)) { + Path newFilePath = newPath.getFilePath( ); + if (oldPath.equals(newFilePath)) { // Nothing to do, if path does not change return context; } @@ -510,7 +513,13 @@ public class MavenIndexManager implements ArchivaIndexManager { return createContext(repo); } else { context.close(false); - Files.move(oldPath, newPath.getFilePath()); + if (Files.exists( newFilePath )) { + FileUtils.copyContent( oldPath, newFilePath ); + FileUtils.deleteDirectory( oldPath ); + } else + { + Files.move( oldPath, newFilePath ); + } return createContext(repo); } } catch (IOException e) { @@ -629,7 +638,7 @@ public class MavenIndexManager implements ArchivaIndexManager { if ( !indexDir.exists() ) { - indexDir = storage.addAsset(indexDir.getPath(), true); + indexDir.create( AssetType.CONTAINER ); } return indexDir; } @@ -758,11 +767,23 @@ public class MavenIndexManager implements ArchivaIndexManager { { if ( rif.getIndexUri( ) == null ) { - return baseUri.resolve( DEFAULT_INDEX_PATH ).toString( ); + return baseUri.resolve( "/"+DEFAULT_INDEX_PATH ).toString( ); } else { - return baseUri.resolve( rif.getIndexUri( ) ).toString( ); + URI rifUri = rif.getIndexUri( ); + if (rifUri.isAbsolute()) { + return rifUri.toString( ); + } else + { + if (baseUri.getScheme().toLowerCase().equals( "file" )) { + return Paths.get( baseUri ).resolve( rifUri.getPath() ).toUri( ).toString( ); + } else + { + String pathString = rifUri.getPath( ).startsWith( "/" ) ? rifUri.getPath( ) : "/" + rifUri.getPath( ); + return baseUri.resolve( pathString ).toString( ); + } + } } } diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java index ca2477f53..e30a0f2b3 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java @@ -22,6 +22,7 @@ package org.apache.archiva.repository.mock; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.archiva.configuration.RepositoryGroupConfiguration; +import org.apache.archiva.event.EventHandler; import org.apache.archiva.repository.base.BasicManagedRepository; import org.apache.archiva.repository.base.BasicRemoteRepository; import org.apache.archiva.repository.EditableManagedRepository; @@ -37,6 +38,7 @@ import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryGroup; import org.apache.archiva.repository.RepositoryProvider; import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.event.RepositoryEvent; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; @@ -249,6 +251,12 @@ public class RepositoryProviderMock implements RepositoryProvider return null; } + @Override + public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler ) + { + // do nothing + } + @Override public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java index 02b0cadac..2afc249bb 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java @@ -26,6 +26,9 @@ import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.event.Event; +import org.apache.archiva.event.EventHandler; +import org.apache.archiva.event.EventManager; +import org.apache.archiva.event.EventType; import org.apache.archiva.repository.EditableManagedRepository; import org.apache.archiva.repository.EditableRemoteRepository; import org.apache.archiva.repository.EditableRepository; @@ -33,6 +36,7 @@ import org.apache.archiva.repository.EditableRepositoryGroup; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.RemoteRepository; +import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryCredentials; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryGroup; @@ -41,6 +45,7 @@ import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.UnsupportedURIException; import org.apache.archiva.repository.base.BasicManagedRepository; import org.apache.archiva.repository.base.PasswordCredentials; +import org.apache.archiva.repository.event.RepositoryEvent; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; @@ -61,7 +66,9 @@ import java.nio.file.Paths; import java.time.Duration; import java.time.Period; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -81,6 +88,29 @@ public class MavenRepositoryProvider implements RepositoryProvider { @Inject private FileLockManager fileLockManager; + private class EventHandlerInfo { + EventType<?> type; + EventHandler<?> handler; + + public EventHandlerInfo( EventType<?> type, EventHandler<?> handler ) + { + this.type = type; + this.handler = handler; + } + + public EventType<?> getType( ) + { + return type; + } + + public EventHandler<?> getHandler( ) + { + return handler; + } + } + + private List<EventHandler<? super RepositoryEvent>> repositoryEventHandlers = new ArrayList<>( ); + private static final Logger log = LoggerFactory.getLogger(MavenRepositoryProvider.class); static final Set<RepositoryType> TYPES = new HashSet<>(); @@ -100,14 +130,16 @@ public class MavenRepositoryProvider implements RepositoryProvider { } public MavenManagedRepository createManagedInstance(String id, String name, Path baseDir) { - FilesystemStorage storage = null; + FilesystemStorage storage; try { storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager); } catch (IOException e) { log.error("Could not initialize fileystem for repository {}", id); throw new RuntimeException(e); } - return new MavenManagedRepository(id, name, storage); + MavenManagedRepository repo = new MavenManagedRepository( id, name, storage ); + registerEventHandler( repo ); + return repo; } @Override @@ -116,14 +148,16 @@ public class MavenRepositoryProvider implements RepositoryProvider { } public MavenRemoteRepository createRemoteInstance(String id, String name, Path baseDir) { - FilesystemStorage storage = null; + FilesystemStorage storage; try { storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager); } catch (IOException e) { log.error("Could not initialize fileystem for repository {}", id); throw new RuntimeException(e); } - return new MavenRemoteRepository(id, name, storage); + MavenRemoteRepository repo = new MavenRemoteRepository( id, name, storage ); + registerEventHandler( repo ); + return repo; } @Override @@ -132,14 +166,22 @@ public class MavenRepositoryProvider implements RepositoryProvider { } public MavenRepositoryGroup createRepositoryGroup(String id, String name, Path baseDir) { - FilesystemStorage storage = null; + FilesystemStorage storage; try { storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager); } catch (IOException e) { log.error("Could not initialize fileystem for repository {}", id); throw new RuntimeException(e); } - return new MavenRepositoryGroup(id, name, storage); + MavenRepositoryGroup group = new MavenRepositoryGroup( id, name, storage ); + registerEventHandler( group ); + return group; + } + + private void registerEventHandler( Repository repo ) { + for (EventHandler<? super RepositoryEvent> eventHandler : repositoryEventHandlers) { + repo.registerEventHandler( RepositoryEvent.ANY, eventHandler ); + } } private URI getURIFromString(String uriStr) throws RepositoryException { @@ -166,7 +208,7 @@ public class MavenRepositoryProvider implements RepositoryProvider { try { uri = new URI(newCfg); } catch (URISyntaxException e1) { - log.error("Could not create URI from {} -> ", uriStr, newCfg); + log.error("Could not create URI from {} -> {}", uriStr, newCfg); throw new RepositoryException("The config entry " + uriStr + " cannot be converted to URI."); } } @@ -421,16 +463,8 @@ public class MavenRepositoryProvider implements RepositoryProvider { cfg.setRetentionPeriod(artifactCleanupFeature.getRetentionPeriod().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); - } + cfg.setReleases( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.RELEASE ) ); + cfg.setSnapshots( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) ); return cfg; } @@ -450,11 +484,17 @@ public class MavenRepositoryProvider implements RepositoryProvider { cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() ); } cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL()); - cfg.setRepositories(repositoryGroup.getRepositories().stream().map(r -> r.getId()).collect(Collectors.toList())); + cfg.setRepositories(repositoryGroup.getRepositories().stream().map( Repository::getId ).collect(Collectors.toList())); cfg.setCronExpression(repositoryGroup.getSchedulingDefinition()); return cfg; } + @Override + public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler ) + { + this.repositoryEventHandlers.add( eventHandler ); + } + private ManagedRepositoryConfiguration getStageRepoConfig(ManagedRepositoryConfiguration repository) { ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration(); stagingRepository.setId(repository.getId() + StagingRepositoryFeature.STAGING_REPO_POSTFIX); @@ -470,7 +510,7 @@ public class MavenRepositoryProvider implements RepositoryProvider { stagingRepository.setLocation(path.substring(0, lastIndex) + "/" + stagingRepository.getId()); if (StringUtils.isNotBlank(repository.getIndexDir())) { - Path indexDir = null; + Path indexDir; try { indexDir = Paths.get(new URI(repository.getIndexDir().startsWith("file://") ? repository.getIndexDir() : "file://" + repository.getIndexDir())); if (indexDir.isAbsolute()) { @@ -488,7 +528,7 @@ public class MavenRepositoryProvider implements RepositoryProvider { // in case of absolute dir do not use the same } if (StringUtils.isNotBlank(repository.getPackedIndexDir())) { - Path packedIndexDir = null; + Path packedIndexDir; packedIndexDir = Paths.get(repository.getPackedIndexDir()); if (packedIndexDir.isAbsolute()) { Path newDir = packedIndexDir.getParent().resolve(packedIndexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX); diff --git a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java index ae7189184..8a4ad112e 100644 --- a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java @@ -109,7 +109,7 @@ public class DownloadRemoteIndexTaskTest server = new Server( ); serverConnector = new ServerConnector( server, new HttpConnectionFactory()); server.addConnector( serverConnector ); - createContext( server, Paths.get( "src/test/" ) ); + createContext( server, Paths.get( "src/test" ) ); this.server.start(); this.port = serverConnector.getLocalPort(); log.info( "start server on port {}", this.port ); @@ -185,13 +185,13 @@ public class DownloadRemoteIndexTaskTest Files.createDirectories( indexDirectory ); remoteRepository.setLocation( new URI( "http://localhost:" + port ) ); repoDirectory.toFile().deleteOnExit(); - createIndexingContext( remoteRepository ); - RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get(); rif.setDownloadRemoteIndex( true ); rif.setIndexUri( new URI("http://localhost:" + port + "/index-updates/" ) ); - IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( ); - icf.setLocalIndexPath( remoteRepository.getAsset( "index" ) ); + createIndexingContext( remoteRepository ); + + // IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( ); + // icf.setLocalIndexPath( remoteRepository.getAsset( "index" ) ); return remoteRepository; } |