import org.apache.archiva.repository.features.IndexCreationFeature;
import org.apache.archiva.repository.features.RemoteIndexFeature;
import org.apache.commons.lang.StringUtils;
-import org.apache.maven.index.DefaultScannerListener;
-import org.apache.maven.index.Indexer;
-import org.apache.maven.index.IndexerEngine;
-import org.apache.maven.index.Scanner;
-import org.apache.maven.index.ScanningRequest;
-import org.apache.maven.index.ScanningResult;
+import org.apache.maven.index.*;
import org.apache.maven.index.context.IndexCreator;
import org.apache.maven.index.context.IndexingContext;
import org.apache.maven.index.packer.IndexPacker;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListSet;
+import java.util.stream.Collectors;
/**
- * Maven implementation of index manager
+ * Maven implementation of index manager.
+ * The index manager is a singleton, so we try to make sure, that index operations are not running
+ * parallel by synchronizing on the index path.
+ * A update operation waits for parallel running methods to finish before starting, but after a certain
+ * time of retries a IndexUpdateFailedException is thrown.
*/
@Service( "archivaIndexManager#maven" )
public class MavenIndexManager implements ArchivaIndexManager
@Inject
private IndexUpdater indexUpdater;
+ @Inject
+ private ArtifactContextProducer artifactContextProducer;
+
private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
private static final int WAIT_TIME = 100;
void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
}
+ /*
+ * This method is used to do some actions around the update execution code. And to make sure, that no other
+ * method is running on the same index.
+ */
private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
{
IndexingContext indexingContext = null;
}
@Override
- public void addArtifactToIndex( ArchivaIndexingContext context, ArtifactReference artifactReference ) throws IndexUpdateFailedException
+ public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<Path> artifactReference ) throws IndexUpdateFailedException
{
-
+ executeUpdateFunction(context, indexingContext -> {
+ Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
+ try {
+ indexer.addArtifactsToIndex(artifacts, indexingContext);
+ } catch (IOException e) {
+ log.error("IOException while adding artifact {}", e.getMessage(), e);
+ throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
+ + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
+ }
+ });
}
@Override
- public void removeArtifactFromIndex( ArchivaIndexingContext context, ArtifactReference artifactReference ) throws IndexUpdateFailedException
+ public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<Path> artifactReference ) throws IndexUpdateFailedException
{
+ executeUpdateFunction(context, indexingContext -> {
+ Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
+ try {
+ indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
+ } catch (IOException e) {
+ log.error("IOException while removing artifact {}", e.getMessage(), e);
+ throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
+ + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
+ }
+ });
}
import java.io.IOException;
import java.net.URI;
+import java.nio.file.Path;
+import java.util.Collection;
public interface ArchivaIndexManager {
void update(ArchivaIndexingContext context, URI remoteUpdateUri, boolean fullUpdate) throws IndexUpdateFailedException;
/**
- * Adds a artifact to the index.
+ * Adds a list of artifacts to the index.
* @param context
* @param artifactReference
*/
- void addArtifactToIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) throws IndexUpdateFailedException;
+ void addArtifactsToIndex(ArchivaIndexingContext context, Collection<Path> artifactReference) throws IndexUpdateFailedException;
/**
- * Removes a artifact from the index.
+ * Removes a list of artifacts from the index.
* @param context
* @param artifactReference
*/
- void removeArtifactFromIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) throws IndexUpdateFailedException;
+ void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<Path> artifactReference) throws IndexUpdateFailedException;
/**