Search in sources :

Example 41 with ArtifactMetadata

use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.

the class RetentionCountRepositoryPurgeTest method testIfAPomWasFound.

/**
 * Test if the artifact to be processed is a pom
 */
@Test
public void testIfAPomWasFound() throws Exception {
    String repoRoot = prepareTestRepos();
    String projectNs = "org.codehaus.castor";
    String projectPath = projectNs.replaceAll("\\.", "/");
    String projectName = "castor-anttasks";
    String projectVersion = "1.1.2-SNAPSHOT";
    String projectRoot = repoRoot + "/" + projectPath + "/" + projectName;
    Path repo = getTestRepoRootPath();
    Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
    Set<String> deletedVersions = new HashSet<>();
    deletedVersions.add("1.1.2-20070427.065136-1");
    String versionRoot = projectRoot + "/" + projectVersion;
    // test listeners for the correct artifacts
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks", "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar.md5");
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks", "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar.sha1");
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks", "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar");
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks", "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom.md5");
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks", "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom.sha1");
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks", "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom");
    listenerControl.replay();
    // Provide the metadata list
    List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
    when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
    repoPurge.process(PATH_TO_BY_RETENTION_COUNT_POM);
    listenerControl.verify();
    // Verify the metadataRepository invocations
    verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
    ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
    verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
    List<ArtifactMetadata> metaL = metadataArg.getAllValues();
    for (ArtifactMetadata meta : metaL) {
        assertTrue(meta.getId().startsWith(projectName));
        assertTrue(deletedVersions.contains(meta.getVersion()));
    }
    // assert if removed from repo
    assertDeleted(versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar");
    assertDeleted(versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.md5");
    assertDeleted(versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.sha1");
    assertDeleted(versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom");
    assertDeleted(versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.md5");
    assertDeleted(versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.sha1");
    // assert if not removed from repo
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.md5");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.sha1");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.md5");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.sha1");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.md5");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.sha1");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.md5");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.sha1");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.md5");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.sha1");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.md5");
    assertExists(versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.sha1");
}
Also used : Path(java.nio.file.Path) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 42 with ArtifactMetadata

use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.

the class ArchivaMetadataCreationConsumer method processFile.

@Override
public void processFile(String path) throws ConsumerException {
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        // note that we do minimal processing including checksums and POM information for performance of
        // the initial scan. Any request for this information will be intercepted and populated on-demand
        // or picked up by subsequent scans
        ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath(repoId, path);
        ProjectMetadata project = new ProjectMetadata();
        project.setNamespace(artifact.getNamespace());
        project.setId(artifact.getProject());
        String projectVersion = VersionUtil.getBaseVersion(artifact.getVersion());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        boolean createVersionMetadata = false;
        // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
        ProjectVersionMetadata versionMetadata = null;
        try {
            ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(artifact.getNamespace()).projectId(artifact.getProject()).projectVersion(projectVersion);
            versionMetadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
            createVersionMetadata = true;
        } catch (RepositoryStorageMetadataNotFoundException e) {
            log.warn("Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path, repoId);
            versionMetadata = new ProjectVersionMetadata();
            versionMetadata.setId(projectVersion);
            versionMetadata.setIncomplete(true);
            createVersionMetadata = true;
        } catch (RepositoryStorageMetadataInvalidException e) {
            log.warn("Error occurred resolving POM for artifact:{} (repository:{}); message: {}", new Object[] { path, repoId, e.getMessage() });
        }
        // read the metadata and update it if it is newer or doesn't exist
        artifact.setWhenGathered(whenGathered);
        metadataRepository.updateArtifact(repoId, project.getNamespace(), project.getId(), projectVersion, artifact);
        if (createVersionMetadata) {
            metadataRepository.updateProjectVersion(repoId, project.getNamespace(), project.getId(), versionMetadata);
        }
        metadataRepository.updateProject(repoId, project);
        repositorySession.save();
    } catch (MetadataRepositoryException e) {
        log.warn("Error occurred persisting metadata for artifact:{} (repository:{}); message: {}", path, repoId, e.getMessage(), e);
        repositorySession.revert();
    } catch (RepositoryStorageRuntimeException e) {
        log.warn("Error occurred persisting metadata for artifact:{} (repository:{}); message: {}", path, repoId, e.getMessage(), e);
        repositorySession.revert();
    } finally {
        repositorySession.close();
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 43 with ArtifactMetadata

use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.

the class AbstractRepositoryPurge method purge.

/**
 * Purge the repo. Update db and index of removed artifacts.
 *
 * @param references
 */
protected void purge(Set<ArtifactReference> references) {
    if (references != null && !references.isEmpty()) {
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Map<ArtifactInfo, ArtifactMetadata> metaRemovalList = new HashMap<>();
        Map<String, Collection<ArtifactMetadata>> metaResolved = new HashMap<>();
        for (ArtifactReference reference : references) {
            String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
            // Needed for tracking in the hashmap
            String metaBaseId = reference.getGroupId() + "/" + reference.getArtifactId() + "/" + baseVersion;
            if (!metaResolved.containsKey(metaBaseId)) {
                try {
                    metaResolved.put(metaBaseId, metadataRepository.getArtifacts(repository.getId(), reference.getGroupId(), reference.getArtifactId(), baseVersion));
                } catch (MetadataResolutionException e) {
                    log.error("Error during metadata retrieval {}: {}", metaBaseId, e.getMessage());
                }
            }
            Path artifactFile = repository.toFile(reference);
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), artifactFile.getFileName().toString());
            }
            try {
                Files.delete(artifactFile);
                log.debug("File deleted: {}", artifactFile.toAbsolutePath());
            } catch (IOException e) {
                log.error("Could not delete file {}: {}", artifactFile.toAbsolutePath(), e.getMessage(), e);
                continue;
            }
            try {
                repository.deleteArtifact(reference);
            } catch (ContentNotFoundException e) {
                log.warn("skip error deleting artifact {}: {}", reference, e.getMessage());
            }
            boolean snapshotVersion = VersionUtil.isSnapshot(reference.getVersion());
            // If this is a snapshot we have to search for artifacts with the same version. And remove all of them.
            if (snapshotVersion) {
                Collection<ArtifactMetadata> artifacts = metaResolved.get(metaBaseId);
                if (artifacts != null) {
                    // cleanup snapshots metadata
                    for (ArtifactMetadata artifactMetadata : artifacts) {
                        // Artifact metadata and reference version should match.
                        if (artifactMetadata.getVersion().equals(reference.getVersion())) {
                            ArtifactInfo info = new ArtifactInfo(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getVersion());
                            if (StringUtils.isNotBlank(reference.getClassifier())) {
                                info.setClassifier(reference.getClassifier());
                                metaRemovalList.put(info, artifactMetadata);
                            } else {
                                // metadataRepository.removeArtifact( artifactMetadata, baseVersion );
                                metaRemovalList.put(info, artifactMetadata);
                            }
                        }
                    }
                }
            } else // otherwise we delete the artifact version
            {
                ArtifactInfo info = new ArtifactInfo(reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion());
                for (ArtifactMetadata metadata : metaResolved.get(metaBaseId)) {
                    metaRemovalList.put(info, metadata);
                }
            }
            triggerAuditEvent(repository.getRepository().getId(), ArtifactReference.toKey(reference), AuditEvent.PURGE_ARTIFACT);
            purgeSupportFiles(artifactFile);
        }
        purgeMetadata(metadataRepository, metaRemovalList);
        repositorySession.save();
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) HashMap(java.util.HashMap) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) IOException(java.io.IOException) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Collection(java.util.Collection) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 44 with ArtifactMetadata

use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.

the class Maven2RepositoryMerger method getConflictingArtifacts.

@Override
public List<ArtifactMetadata> getConflictingArtifacts(MetadataRepository metadataRepository, String sourceRepo, String targetRepo) throws RepositoryMergerException {
    try {
        List<ArtifactMetadata> targetArtifacts = metadataRepository.getArtifacts(targetRepo);
        List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts(sourceRepo);
        List<ArtifactMetadata> conflictsArtifacts = new ArrayList<>();
        for (ArtifactMetadata targetArtifact : targetArtifacts) {
            for (ArtifactMetadata sourceArtifact : sourceArtifacts) {
                if (isEquals(targetArtifact, sourceArtifact)) {
                    if (!conflictsArtifacts.contains(sourceArtifact)) {
                        conflictsArtifacts.add(sourceArtifact);
                    }
                }
            }
        }
        sourceArtifacts.removeAll(conflictsArtifacts);
        return conflictsArtifacts;
    } catch (MetadataRepositoryException e) {
        throw new RepositoryMergerException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ArrayList(java.util.ArrayList) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 45 with ArtifactMetadata

use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.

the class Maven2RepositoryMerger method merge.

@Override
public void merge(MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId) throws RepositoryMergerException {
    try {
        List<ArtifactMetadata> artifactsInSourceRepo = metadataRepository.getArtifacts(sourceRepoId);
        for (ArtifactMetadata artifactMetadata : artifactsInSourceRepo) {
            artifactMetadata.setRepositoryId(targetRepoId);
            createFolderStructure(sourceRepoId, targetRepoId, artifactMetadata);
        }
    } catch (MetadataRepositoryException e) {
        throw new RepositoryMergerException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RepositoryMergerException(e.getMessage(), e);
    } catch (RepositoryException e) {
        throw new RepositoryMergerException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Aggregations

ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)96 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)23 Path (java.nio.file.Path)20 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)17 Date (java.util.Date)15 HashSet (java.util.HashSet)11 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)10 MavenArtifactFacet (org.apache.archiva.metadata.model.maven2.MavenArtifactFacet)9 HashMap (java.util.HashMap)8 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)8 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)7 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)7 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)7 ArtifactCleanupFeature (org.apache.archiva.repository.features.ArtifactCleanupFeature)7 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Node (javax.jcr.Node)6 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)6 IOException (java.io.IOException)5 RepositoryException (javax.jcr.RepositoryException)5