Search in sources :

Example 1 with RepositoryListener

use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.

the class DefaultMetadataResolver method resolveArtifacts.

@Override
public Collection<ArtifactMetadata> resolveArtifacts(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion) throws MetadataResolutionException {
    try {
        MetadataRepository metadataRepository = session.getRepository();
        Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repoId, namespace, projectId, projectVersion);
        ExcludesFilter<String> filter = new ExcludesFilter<String>(createArtifactIdList(artifacts));
        ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion).filter(filter);
        Collection<ArtifactMetadata> storageArtifacts = repositoryStorage.readArtifactsMetadata(readMetadataRequest);
        if (storageArtifacts != null && !storageArtifacts.isEmpty()) {
            log.debug("Resolved artifacts from storage: {}", storageArtifacts);
            for (ArtifactMetadata artifact : storageArtifacts) {
                try {
                    metadataRepository.updateArtifact(repoId, namespace, projectId, projectVersion, artifact);
                } catch (MetadataRepositoryException e) {
                    log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
                }
            }
            session.markDirty();
            artifacts = new ArrayList<>(artifacts);
            artifacts.addAll(storageArtifacts);
        }
        return artifacts;
    } catch (RepositoryStorageRuntimeException e) {
        for (RepositoryListener listener : listeners) {
            listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
        }
        throw new MetadataResolutionException(e.getMessage(), e);
    }
}
Also used : RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ExcludesFilter(org.apache.archiva.metadata.repository.filter.ExcludesFilter) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 2 with RepositoryListener

use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            // Nothing to do here, file doesn't exist, skip it.
            return;
        }
        ArtifactReference artifactRef = repository.toArtifactReference(path);
        if (!VersionUtil.isSnapshot(artifactRef.getVersion())) {
            // Nothing to do here, not a snapshot, skip it.
            return;
        }
        ProjectReference reference = new ProjectReference();
        reference.setGroupId(artifactRef.getGroupId());
        reference.setArtifactId(artifactRef.getArtifactId());
        // Gether the released versions
        List<String> releasedVersions = new ArrayList<>();
        Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
        for (org.apache.archiva.repository.ManagedRepository repo : repos) {
            if (repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
                try {
                    ManagedRepositoryContent repoContent = repo.getContent();
                    for (String version : repoContent.getVersions(reference)) {
                        if (!VersionUtil.isSnapshot(version)) {
                            releasedVersions.add(version);
                        }
                    }
                } catch (RepositoryException e) {
                // swallow
                }
            }
        }
        Collections.sort(releasedVersions, VersionComparator.getInstance());
        // Now clean out any version that is earlier than the highest released version.
        boolean needsMetadataUpdate = false;
        VersionedReference versionRef = new VersionedReference();
        versionRef.setGroupId(artifactRef.getGroupId());
        versionRef.setArtifactId(artifactRef.getArtifactId());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion()))) {
            versionRef.setVersion(artifactRef.getVersion());
            repository.deleteVersion(versionRef);
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(), artifactFile.getFileName().toString());
            }
            metadataRepository.removeProjectVersion(repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion());
            needsMetadataUpdate = true;
        }
        if (needsMetadataUpdate) {
            updateMetadata(artifactRef);
        }
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (ContentNotFoundException e) {
        throw new RepositoryPurgeException(e.getMessage(), e);
    } catch (MetadataRepositoryException e) {
        log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ProjectReference(org.apache.archiva.model.ProjectReference) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArrayList(java.util.ArrayList) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 3 with RepositoryListener

use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurgeTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    RepositoryContentFactory factory = applicationContext.getBean("repositoryContentFactory#cleanup-released-snapshots", RepositoryContentFactory.class);
    archivaConfiguration = applicationContext.getBean("archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class);
    listenerControl = EasyMock.createControl();
    listener = listenerControl.createMock(RepositoryListener.class);
    List<RepositoryListener> listeners = Collections.singletonList(listener);
    repoPurge = new CleanupReleasedSnapshotsRepositoryPurge(getRepository(), metadataTools, applicationContext.getBean(RepositoryRegistry.class), repositorySession, listeners);
    ((DefaultManagedRepositoryAdmin) applicationContext.getBean(ManagedRepositoryAdmin.class)).setArchivaConfiguration(archivaConfiguration);
}
Also used : RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) ManagedRepositoryAdmin(org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) RepositoryContentFactory(org.apache.archiva.repository.RepositoryContentFactory) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) Before(org.junit.Before)

Example 4 with RepositoryListener

use of org.apache.archiva.repository.events.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).get();
    List<RepositoryListener> listeners = Collections.singletonList(listener);
    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-20070427.065136-1", "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-20070427.065136-1", "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" + exts[i]);
    }
    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_TEST_ORDER_OF_DELETION);
    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()));
    }
    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.repository.events.RepositoryListener) Path(java.nio.file.Path) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with RepositoryListener

use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.

the class RetentionCountRepositoryPurgeTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME);
    List<RepositoryListener> listeners = Collections.singletonList(listener);
    ArtifactCleanupFeature acf = repoConfiguration.getFeature(ArtifactCleanupFeature.class).get();
    repoPurge = new RetentionCountRepositoryPurge(getRepository(), acf.getRetentionCount(), repositorySession, listeners);
}
Also used : RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature) Before(org.junit.Before)

Aggregations

RepositoryListener (org.apache.archiva.repository.events.RepositoryListener)10 Path (java.nio.file.Path)5 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)5 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)3 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)3 RepositoryStorageRuntimeException (org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException)3 ArtifactReference (org.apache.archiva.model.ArtifactReference)3 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)3 ArtifactCleanupFeature (org.apache.archiva.repository.features.ArtifactCleanupFeature)3 SimpleDateFormat (java.text.SimpleDateFormat)2 HashSet (java.util.HashSet)2 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)2 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)2 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)2 RepositoryStorageMetadataInvalidException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)2 RepositoryStorageMetadataNotFoundException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)2 VersionedReference (org.apache.archiva.model.VersionedReference)2 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)2 RepositoryException (org.apache.archiva.repository.RepositoryException)2 Before (org.junit.Before)2