Search in sources :

Example 21 with MetadataRepositoryException

use of org.apache.archiva.metadata.repository.MetadataRepositoryException in project archiva by apache.

the class JcrMetadataRepository method removeArtifact.

@Override
public void removeArtifact(String repositoryId, String namespace, String projectId, String projectVersion, String id) throws MetadataRepositoryException {
    try {
        Node root = getJcrSession().getRootNode();
        String path = getArtifactPath(repositoryId, namespace, projectId, projectVersion, id);
        if (root.hasNode(path)) {
            root.getNode(path).remove();
        }
        // remove version
        path = getProjectPath(repositoryId, namespace, projectId);
        Node nodeAtPath = root.getNode(path);
        for (Node node : JcrUtils.getChildNodes(nodeAtPath)) {
            if (// 
            node.isNodeType(PROJECT_VERSION_NODE_TYPE) && StringUtils.equals(node.getName(), projectVersion)) {
                node.remove();
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Node(javax.jcr.Node) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 22 with MetadataRepositoryException

use of org.apache.archiva.metadata.repository.MetadataRepositoryException in project archiva by apache.

the class JcrMetadataRepository method removeArtifact.

@Override
public void removeArtifact(ArtifactMetadata artifactMetadata, String baseVersion) throws MetadataRepositoryException {
    String repositoryId = artifactMetadata.getRepositoryId();
    try {
        Node root = getJcrSession().getRootNode();
        String path = getProjectVersionPath(repositoryId, artifactMetadata.getNamespace(), artifactMetadata.getProject(), baseVersion);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            for (Node n : JcrUtils.getChildNodes(node)) {
                if (n.isNodeType(ARTIFACT_NODE_TYPE)) {
                    if (n.hasProperty("version")) {
                        String version = n.getProperty("version").getString();
                        if (StringUtils.equals(version, artifactMetadata.getVersion())) {
                            n.remove();
                        }
                    }
                }
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Node(javax.jcr.Node) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 23 with MetadataRepositoryException

use of org.apache.archiva.metadata.repository.MetadataRepositoryException in project archiva by apache.

the class JcrMetadataRepository method getMetadataFacet.

@Override
public MetadataFacet getMetadataFacet(String repositoryId, String facetId, String name) throws MetadataRepositoryException {
    MetadataFacet metadataFacet = null;
    try {
        Node root = getJcrSession().getRootNode();
        Node node = root.getNode(getFacetPath(repositoryId, facetId, name));
        if (metadataFacetFactories == null) {
            return metadataFacet;
        }
        MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get(facetId);
        if (metadataFacetFactory != null) {
            metadataFacet = metadataFacetFactory.createMetadataFacet(repositoryId, name);
            Map<String, String> map = new HashMap<>();
            for (Property property : JcrUtils.getProperties(node)) {
                String p = property.getName();
                if (!p.startsWith("jcr:")) {
                    map.put(p, property.getString());
                }
            }
            metadataFacet.fromProperties(map);
        }
    } catch (PathNotFoundException e) {
    // ignored - the facet doesn't exist, so return null
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return metadataFacet;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) HashMap(java.util.HashMap) Node(javax.jcr.Node) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) Property(javax.jcr.Property) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory)

Example 24 with MetadataRepositoryException

use of org.apache.archiva.metadata.repository.MetadataRepositoryException in project archiva by apache.

the class JcrRepositorySessionFactory method initialize.

public void initialize() {
    // skip initialisation if not jcr
    if (repositorySessionFactoryBean != null && !StringUtils.equals(repositorySessionFactoryBean.getId(), "jcr")) {
        return;
    }
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    metadataFacetFactories = applicationContext.getBeansOfType(MetadataFacetFactory.class);
    // olamy with spring the "id" is now "metadataFacetFactory#hint"
    // whereas was only hint with plexus so let remove  metadataFacetFactory#
    Map<String, MetadataFacetFactory> cleanedMetadataFacetFactories = new HashMap<>(metadataFacetFactories.size());
    for (Map.Entry<String, MetadataFacetFactory> entry : metadataFacetFactories.entrySet()) {
        cleanedMetadataFacetFactories.put(StringUtils.substringAfterLast(entry.getKey(), "#"), entry.getValue());
    }
    metadataFacetFactories = cleanedMetadataFacetFactories;
    JcrMetadataRepository metadataRepository = null;
    try {
        repositoryFactory = new RepositoryFactory();
        // FIXME this need to be configurable
        Path directoryPath = Paths.get(System.getProperty("appserver.base"), "data/jcr");
        repositoryFactory.setRepositoryPath(directoryPath);
        try {
            repository = repositoryFactory.createRepository();
        } catch (InvalidFileStoreVersionException | IOException e) {
            logger.error("Repository creation failed {}", e.getMessage());
            throw new RuntimeException("Fatal error. Could not create metadata repository.");
        }
        metadataRepository = new JcrMetadataRepository(metadataFacetFactories, repository);
        JcrMetadataRepository.initialize(metadataRepository.getJcrSession());
    } catch (RepositoryException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        if (metadataRepository != null) {
            try {
                metadataRepository.close();
            } catch (MetadataRepositoryException e) {
                logger.error("Close of metadata repository failed {}", e.getMessage());
            }
        }
    }
    stopWatch.stop();
    logger.info("time to initialize JcrRepositorySessionFactory: {}", stopWatch.getTime());
}
Also used : Path(java.nio.file.Path) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) HashMap(java.util.HashMap) RepositoryException(javax.jcr.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) StopWatch(org.apache.commons.lang.time.StopWatch) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory) InvalidFileStoreVersionException(org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with MetadataRepositoryException

use of org.apache.archiva.metadata.repository.MetadataRepositoryException 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)

Aggregations

MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)65 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)24 RepositoryException (javax.jcr.RepositoryException)22 Node (javax.jcr.Node)21 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)19 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)18 Path (java.nio.file.Path)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)10 HashMap (java.util.HashMap)8 RepositoryException (org.apache.archiva.repository.RepositoryException)8 Map (java.util.Map)6 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)6 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)6 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)6 Date (java.util.Date)5 Query (javax.jcr.query.Query)5 QueryResult (javax.jcr.query.QueryResult)5 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)5