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");
}
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();
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations