Search in sources :

Example 6 with RepositoryListener

use of org.apache.archiva.metadata.audit.RepositoryListener in project archiva by apache.

the class DaysOldRepositoryPurgeTest method testOrderOfDeletion.

@Test
public void testOrderOfDeletion() throws Exception {
    org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME);
    ArtifactCleanupFeature atf = repoConfiguration.getFeature(ArtifactCleanupFeature.class);
    List<RepositoryListener> listeners = Collections.singletonList(listener);
    when(sessionFactory.createSession()).thenReturn(repositorySession);
    when(repositorySession.getRepository()).thenReturn(metadataRepository);
    repositorySession.save();
    repoPurge = new DaysOldRepositoryPurge(getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, listeners);
    String repoRoot = prepareTestRepos();
    String projectNs = "org.apache.maven.plugins";
    String projectPath = projectNs.replaceAll("\\.", "/");
    String projectName = "maven-assembly-plugin";
    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");
    setLastModified(projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP);
    // test listeners for the correct artifacts
    String[] exts = { ".md5", ".sha1", "" };
    for (int i = 0; i < exts.length; i++) {
        listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" + exts[i]);
        listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" + exts[i]);
    }
    // Provide the metadata list
    List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
    when(metadataRepository.getArtifacts(repositorySession, TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
    repoPurge.process(PATH_TO_TEST_ORDER_OF_DELETION);
    // Verify the metadataRepository invocations
    verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
    ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
    verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact(eq(repositorySession), metadataArg.capture(), eq(projectVersion));
    List<ArtifactMetadata> metaL = metadataArg.getAllValues();
    for (ArtifactMetadata meta : metaL) {
        assertTrue(meta.getId().startsWith(projectName));
        assertTrue(deletedVersions.contains(meta.getVersion()));
    }
    assertDeleted(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar");
    assertDeleted(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1");
    assertDeleted(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5");
    assertDeleted(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom");
    assertDeleted(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1");
    assertDeleted(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5");
    // the following should not have been deleted
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1");
    assertExists(projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5");
}
Also used : RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) Path(java.nio.file.Path) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Test(org.junit.Test)

Example 7 with RepositoryListener

use of org.apache.archiva.metadata.audit.RepositoryListener 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<Artifact> references) {
    if (references != null && !references.isEmpty()) {
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Map<ArtifactInfo, ArtifactMetadata> metaRemovalList = new HashMap<>();
        Map<String, Collection<ArtifactMetadata>> metaResolved = new HashMap<>();
        for (Artifact reference : references) {
            String baseVersion = reference.getVersion().getId();
            String namespace = reference.getVersion().getProject().getNamespace().getId();
            // Needed for tracking in the hashmap
            String metaBaseId = reference.toKey();
            if (!metaResolved.containsKey(metaBaseId)) {
                try {
                    metaResolved.put(metaBaseId, metadataRepository.getArtifacts(repositorySession, repository.getId(), namespace, reference.getId(), baseVersion));
                } catch (MetadataResolutionException e) {
                    log.error("Error during metadata retrieval {}: {}", metaBaseId, e.getMessage());
                }
            }
            StorageAsset artifactFile = reference.getAsset();
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), namespace, reference.getId(), reference.getVersion().getId(), artifactFile.getName());
            }
            if (reference.exists()) {
                try {
                    repository.deleteItem(reference);
                } catch (ContentAccessException e) {
                    log.error("Error while trying to delete artifact {}: {}", reference.toString(), e.getMessage(), e);
                } catch (ItemNotFoundException e) {
                    log.error("Asset deleted from background other thread: {}", e.getMessage());
                }
            }
            boolean snapshotVersion = VersionUtil.isSnapshot(baseVersion);
            // 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.getArtifactVersion())) {
                            ArtifactInfo info = new ArtifactInfo(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getVersion());
                            if (StringUtils.isNotBlank(reference.getClassifier())) {
                                info.setClassifier(reference.getClassifier());
                            } else {
                                info.setClassifier("");
                            }
                            metaRemovalList.put(info, artifactMetadata);
                        }
                    }
                }
            } else // otherwise we delete the artifact version
            {
                ArtifactInfo info = new ArtifactInfo(namespace, reference.getId(), baseVersion, reference.getArtifactVersion());
                for (ArtifactMetadata metadata : metaResolved.get(metaBaseId)) {
                    metaRemovalList.put(info, metadata);
                }
            }
            triggerAuditEvent(repository.getRepository().getId(), reference.toKey(), AuditEvent.PURGE_ARTIFACT);
        // purgeSupportFiles( artifactFile );
        }
        purgeMetadata(metadataRepository, metaRemovalList);
        try {
            repositorySession.save();
        } catch (org.apache.archiva.metadata.repository.MetadataSessionException e) {
            e.printStackTrace();
        }
    }
}
Also used : RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) HashMap(java.util.HashMap) org.apache.archiva.metadata.repository(org.apache.archiva.metadata.repository) Artifact(org.apache.archiva.repository.content.Artifact) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Collection(java.util.Collection) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 8 with RepositoryListener

use of org.apache.archiva.metadata.audit.RepositoryListener in project archiva by apache.

the class DaysOldRepositoryPurgeTest method testMetadataDrivenSnapshots.

@Test
public void testMetadataDrivenSnapshots() throws Exception {
    org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME);
    ArtifactCleanupFeature atf = repoConfiguration.getFeature(ArtifactCleanupFeature.class);
    List<RepositoryListener> listeners = Collections.singletonList(listener);
    when(sessionFactory.createSession()).thenReturn(repositorySession);
    when(repositorySession.getRepository()).thenReturn(metadataRepository);
    repositorySession.save();
    repoPurge = new DaysOldRepositoryPurge(getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, listeners);
    String repoRoot = prepareTestRepos();
    String projectNs = "org.codehaus.plexus";
    String projectPath = projectNs.replaceAll("\\.", "/");
    String projectName = "plexus-utils";
    String projectVersion = "1.4.3-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.4.3-20070113.163208-4");
    String versionRoot = projectRoot + "/" + projectVersion;
    Calendar currentDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    setLastModified(versionRoot, currentDate.getTimeInMillis());
    String timestamp = new SimpleDateFormat("yyyyMMdd.HHmmss").format(currentDate.getTime());
    for (int i = 5; i <= 7; i++) {
        Path jarFile = Paths.get(versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar");
        Files.createFile(jarFile);
        Path pomFile = Paths.get(versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom");
        Files.createFile(pomFile);
        // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
        if (i == 5) {
            Files.setLastModifiedTime(jarFile, FileTime.fromMillis(OLD_TIMESTAMP));
            Files.setLastModifiedTime(pomFile, FileTime.fromMillis(OLD_TIMESTAMP));
        }
    }
    // test listeners for the correct artifacts
    String[] exts = { ".sha1", "" };
    for (int i = 0; i < exts.length; i++) {
        listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils", "1.4.3-SNAPSHOT", "plexus-utils-1.4.3-20070113.163208-4.jar" + exts[i]);
        listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils", "1.4.3-SNAPSHOT", "plexus-utils-1.4.3-20070113.163208-4.pom" + exts[i]);
    }
    // Provide the metadata list
    List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
    when(metadataRepository.getArtifacts(repositorySession, TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
    repoPurge.process(PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT);
    // Verify the metadataRepository invocations
    verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
    ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
    verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact(eq(repositorySession), metadataArg.capture(), eq(projectVersion));
    List<ArtifactMetadata> metaL = metadataArg.getAllValues();
    for (ArtifactMetadata meta : metaL) {
        assertTrue(meta.getId().startsWith(projectName));
        assertTrue(deletedVersions.contains(meta.getVersion()));
    }
    // this should be deleted since the filename version (timestamp) is older than
    // 100 days even if the last modified date was <100 days ago
    assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar");
    assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1");
    assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom");
    assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1");
    // this should not be deleted because last modified date is <100 days ago
    assertExists(versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar");
    assertExists(versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom");
    for (int i = 5; i <= 7; i++) {
        assertExists(versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar");
        assertExists(versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom");
    }
}
Also used : RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) Path(java.nio.file.Path) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature) SimpleDateFormat(java.text.SimpleDateFormat) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Test(org.junit.Test)

Example 9 with RepositoryListener

use of org.apache.archiva.metadata.audit.RepositoryListener in project archiva by apache.

the class DefaultMetadataResolver method resolveProjectVersion.

@Override
public ProjectVersionMetadata resolveProjectVersion(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion) throws MetadataResolutionException {
    MetadataRepository metadataRepository = session.getRepository();
    ProjectVersionMetadata metadata = metadataRepository.getProjectVersion(session, repoId, namespace, projectId, projectVersion);
    // may then work here and be more efficient than always trying again)
    if (metadata == null || metadata.isIncomplete()) {
        try {
            ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion).browsingRequest(true);
            metadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
            log.debug("Resolved project version metadata from storage: {}", metadata);
            // eg. maven projects should be able to process parent here
            if (!metadata.getDependencies().isEmpty()) {
                ProjectVersionReference ref = new ProjectVersionReference();
                ref.setNamespace(namespace);
                ref.setProjectId(projectId);
                ref.setProjectVersion(projectVersion);
                ref.setReferenceType(ProjectVersionReference.ReferenceType.DEPENDENCY);
            }
            try {
                for (RepositoryListener listener : listeners) {
                    listener.addArtifact(session, repoId, namespace, projectId, metadata);
                }
                metadataRepository.updateProjectVersion(session, repoId, namespace, projectId, metadata);
            } catch (MetadataRepositoryException e) {
                log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
            }
            session.markDirty();
        } catch (RepositoryStorageMetadataInvalidException e) {
            for (RepositoryListener listener : listeners) {
                listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
            }
            throw new MetadataResolutionException(e.getMessage(), e);
        } catch (RepositoryStorageMetadataNotFoundException e) {
            for (RepositoryListener listener : listeners) {
                listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
            }
        // no need to rethrow - return null
        } catch (RepositoryStorageRuntimeException e) {
            for (RepositoryListener listener : listeners) {
                listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
            }
            throw new MetadataResolutionException(e.getMessage(), e);
        }
    }
    return metadata;
}
Also used : RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ProjectVersionReference(org.apache.archiva.metadata.model.ProjectVersionReference) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)

Example 10 with RepositoryListener

use of org.apache.archiva.metadata.audit.RepositoryListener in project archiva by apache.

the class DefaultMetadataResolver method resolveProjectVersions.

@Override
public Collection<String> resolveProjectVersions(RepositorySession session, String repoId, String namespace, String projectId) throws MetadataResolutionException {
    try {
        MetadataRepository metadataRepository = session.getRepository();
        Collection<String> projectVersions = metadataRepository.getProjectVersions(session, repoId, namespace, projectId);
        Collection<String> storageProjectVersions = repositoryStorage.listProjectVersions(repoId, namespace, projectId, new ExcludesFilter<String>(projectVersions));
        if (storageProjectVersions != null && !storageProjectVersions.isEmpty()) {
            log.debug("Resolved project versions from storage: {}", storageProjectVersions);
            for (String projectVersion : storageProjectVersions) {
                try {
                    ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion);
                    ProjectVersionMetadata versionMetadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
                    for (RepositoryListener listener : listeners) {
                        listener.addArtifact(session, repoId, namespace, projectId, versionMetadata);
                    }
                    metadataRepository.updateProjectVersion(session, repoId, namespace, projectId, versionMetadata);
                } catch (MetadataRepositoryException e) {
                    log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
                } catch (RepositoryStorageMetadataInvalidException e) {
                    log.warn("Not update project in metadata repository due to an error resolving it from storage: {}", e.getMessage());
                    for (RepositoryListener listener : listeners) {
                        listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
                    }
                } catch (RepositoryStorageMetadataNotFoundException e) {
                    for (RepositoryListener listener : listeners) {
                        listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
                    }
                }
            }
            session.markDirty();
            projectVersions = new ArrayList<>(projectVersions);
            projectVersions.addAll(storageProjectVersions);
        }
        return projectVersions;
    } catch (RepositoryStorageRuntimeException e) {
        throw new MetadataResolutionException(e.getMessage(), e);
    }
}
Also used : RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)

Aggregations

RepositoryListener (org.apache.archiva.metadata.audit.RepositoryListener)10 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)5 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)3 RepositoryStorageRuntimeException (org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException)3 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)3 ArtifactCleanupFeature (org.apache.archiva.repository.features.ArtifactCleanupFeature)3 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)3 Path (java.nio.file.Path)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)2 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)2 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)2 RepositoryStorageMetadataInvalidException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)2 RepositoryStorageMetadataNotFoundException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)2 Artifact (org.apache.archiva.repository.content.Artifact)2 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)2 ContentAccessException (org.apache.archiva.repository.content.ContentAccessException)2 LayoutException (org.apache.archiva.repository.content.LayoutException)2 Version (org.apache.archiva.repository.content.Version)2 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)2