Java Code Examples for javax.jcr.RepositoryException#getMessage()

The following examples show how to use javax.jcr.RepositoryException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeProject(RepositorySession session, String repositoryId, String namespace, String projectId)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String namespacePath = getNamespacePath(repositoryId, namespace);

        if (root.hasNode(namespacePath)) {
            Iterator<Node> nodeIterator = JcrUtils.getChildNodes(root.getNode(namespacePath)).iterator();
            while (nodeIterator.hasNext()) {
                Node node = nodeIterator.next();
                if (node.isNodeType(org.apache.archiva.metadata.repository.jcr.JcrConstants.PROJECT_MIXIN_TYPE) && projectId.equals(node.getName())) {
                    node.remove();
                }
            }

        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }

}
 
Example 2
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeNamespace(RepositorySession session, String repositoryId, String projectId)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getNamespacePath(repositoryId, projectId);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            if (node.isNodeType(NAMESPACE_MIXIN_TYPE)) {
                node.remove();
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 3
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeMetadataFacet(RepositorySession session, String repositoryId, String facetId, String name)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getFacetPath(repositoryId, facetId, name);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            do {
                // also remove empty container nodes
                Node parent = node.getParent();
                node.remove();
                node = parent;
            }
            while (!node.hasNodes());
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 4
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private QueryResult queryArtifactByDateRange(Session jcrSession, String repositoryId,
                                             ZonedDateTime startTime, ZonedDateTime endTime,
                                             QueryParameter queryParameter) throws MetadataRepositoryException {
    String q = buildArtifactByDateRangeQuery(repositoryId, startTime, endTime, queryParameter).toString();

    try {
        Query query = jcrSession.getWorkspace().getQueryManager().createQuery(q, Query.JCR_SQL2);
        query.setOffset(queryParameter.getOffset());
        query.setLimit(queryParameter.getLimit());
        ValueFactory valueFactory = jcrSession.getValueFactory();
        if (startTime != null) {
            query.bindValue("start", valueFactory.createValue(createCalendar(startTime.withZoneSameInstant(ModelInfo.STORAGE_TZ))));
        }
        if (endTime != null) {
            query.bindValue("end", valueFactory.createValue(createCalendar(endTime.withZoneSameInstant(ModelInfo.STORAGE_TZ))));
        }
        return query.execute();
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 5
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repoId, ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);

    List<ArtifactMetadata> artifacts;
    try {
        QueryResult result = queryArtifactByDateRange(jcrSession, repoId, startTime, endTime, queryParameter);

        artifacts = new ArrayList<>();
        for (Node n : JcrUtils.getNodes(result)) {
            artifacts.add(getArtifactFromNode(repoId, n));
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return artifacts;
}
 
Example 6
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public List<ArtifactMetadata> getArtifactsByChecksum(RepositorySession session, String repositoryId, String checksum)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    List<ArtifactMetadata> artifacts;

    String q = getArtifactQuery(repositoryId).append(" AND ([artifact].[checksums/*/value] = $checksum)").toString();

    try {
        Query query = jcrSession.getWorkspace().getQueryManager().createQuery(q, Query.JCR_SQL2);
        ValueFactory valueFactory = jcrSession.getValueFactory();
        query.bindValue("checksum", valueFactory.createValue(checksum));
        QueryResult result = query.execute();

        artifacts = new ArrayList<>();
        for (Node n : JcrUtils.getNodes(result)) {
            artifacts.add(getArtifactFromNode(repositoryId, n));
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return artifacts;
}
 
Example 7
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeProjectVersion(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {

        String path = getProjectPath(repoId, namespace, projectId);
        Node root = jcrSession.getRootNode();

        Node nodeAtPath = root.getNode(path);

        for (Node node : JcrUtils.getChildNodes(nodeAtPath)) {
            if (node.isNodeType(PROJECT_VERSION_NODE_TYPE) && StringUtils.equals(projectVersion,
                    node.getName())) {
                node.remove();
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 8
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
public List<ArtifactMetadata> runJcrQuery(final Session jcrSession, final String repositoryId, final String qParam,
                                          final Map<String, String> bindingParam, final boolean checkPath)
        throws MetadataRepositoryException {

    String q = qParam;
    List<ArtifactMetadata> artifacts;
    if (repositoryId != null && checkPath) {
        q += " AND ISDESCENDANTNODE(artifact,'/" + getRepositoryContentPath(repositoryId) + "')";
    }

    log.info("Running JCR Query: {}", q);

    try {
        QueryResult result = runNativeJcrQuery(jcrSession, q, bindingParam);
        artifacts = new ArrayList<>();
        RowIterator rows = result.getRows();
        while (rows.hasNext()) {
            Row row = rows.nextRow();
            Node node = row.getNode("artifact");
            artifacts.add(getArtifactFromNode(repositoryId, node));
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    log.info("Artifacts found {}", artifacts.size());
    for (ArtifactMetadata meta : artifacts) {
        log.info("Artifact: " + meta.getVersion() + " " + meta.getFacetList());
    }
    return artifacts;
}
 
Example 9
Source File: JcrRepositorySession.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void save( ) throws MetadataSessionException
{
    super.save( );
    try
    {
        jcrSession.save();
    }
    catch ( RepositoryException e )
    {
        throw new MetadataSessionException( e.getMessage( ), e );
    }
}
 
Example 10
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeFacetFromArtifact(RepositorySession session, String repositoryId, String namespace, String project, String projectVersion,
                                    MetadataFacet metadataFacet)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getProjectVersionPath(repositoryId, namespace, project, projectVersion);

        if (root.hasNode(path)) {
            Node node = root.getNode(path);

            for (Node n : JcrUtils.getChildNodes(node)) {
                if (n.isNodeType(ARTIFACT_NODE_TYPE)) {
                    ArtifactMetadata artifactMetadata = getArtifactFromNode(repositoryId, n);
                    log.debug("artifactMetadata: {}", artifactMetadata);
                    MetadataFacet metadataFacetToRemove = artifactMetadata.getFacet(metadataFacet.getFacetId());
                    if (metadataFacetToRemove != null && metadataFacet.equals(metadataFacetToRemove)) {
                        n.remove();
                    }
                }
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 11
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeArtifact(RepositorySession session, String repositoryId, String namespace, String projectId, String projectVersion,
                           String id)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.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);
    }
}
 
Example 12
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeTimestampedArtifact(RepositorySession session, ArtifactMetadata artifactMetadata, String baseVersion)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    String repositoryId = artifactMetadata.getRepositoryId();

    try {
        Node root = jcrSession.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);
    }


}
 
Example 13
Source File: JcrRepositorySession.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void refreshAndDiscard() throws MetadataSessionException {
    try {
        jcrSession.refresh(false);
    } catch (RepositoryException e) {
        throw new MetadataSessionException(e.getMessage(), e);
    }
}
 
Example 14
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeRepository(RepositorySession session, String repositoryId)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getRepositoryPath(repositoryId);
        if (root.hasNode(path)) {
            root.getNode(path).remove();
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 15
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
public QueryResult runNativeJcrQuery(final Session jcrSession, final String q, final Map<String, String> bindingParam, long offset, long maxEntries)
        throws MetadataRepositoryException {
    Map<String, String> bindings;
    if (bindingParam == null) {
        bindings = new HashMap<>();
    } else {
        bindings = bindingParam;
    }

    try {
        log.debug("Query: offset={}, limit={}, query={}", offset, maxEntries, q);
        Query query = jcrSession.getWorkspace().getQueryManager().createQuery(q, Query.JCR_SQL2);
        query.setLimit(maxEntries);
        query.setOffset(offset);
        ValueFactory valueFactory = jcrSession.getValueFactory();
        for (Entry<String, String> entry : bindings.entrySet()) {
            log.debug("Binding: {}={}", entry.getKey(), entry.getValue());
            Value value = valueFactory.createValue(entry.getValue());
            log.debug("Binding value {}={}", entry.getKey(), value);
            query.bindValue(entry.getKey(), value);
        }
        long start = System.currentTimeMillis();
        log.debug("Execute query {}", query);
        QueryResult result = query.execute();
        long end = System.currentTimeMillis();
        log.info("JCR Query ran in {} milliseconds: {}", end - start, q);
        return result;
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 16
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private void updateProject(Session jcrSession, String repositoryId, String namespace, String projectId)
        throws MetadataRepositoryException {
    updateNamespace(jcrSession, repositoryId, namespace);

    try {
        getOrAddProjectNode(jcrSession, repositoryId, namespace, projectId);
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 17
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeMetadataFacets(RepositorySession session, String repositoryId, String facetId)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getFacetPath(repositoryId, facetId);
        if (root.hasNode(path)) {
            root.getNode(path).remove();
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 18
Source File: JcrRepositorySession.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void refresh() throws MetadataSessionException {
    try {
        jcrSession.refresh(true);
    } catch (RepositoryException e) {
        throw new MetadataSessionException(e.getMessage(), e);
    }
}
 
Example 19
Source File: JcrRepositorySession.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void revert( ) throws MetadataSessionException
{
    super.revert( );
    try
    {
        jcrSession.refresh( false );
    }
    catch ( RepositoryException e )
    {
        throw new MetadataSessionException( e.getMessage( ), e );
    }
}
 
Example 20
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 4 votes vote down vote up
@Override
    public void populateStatistics(RepositorySession repositorySession, MetadataRepository repository, String repositoryId,
                                   RepositoryStatistics repositoryStatistics)
            throws MetadataRepositoryException {
        if (!(repository instanceof JcrMetadataRepository)) {
            throw new MetadataRepositoryException(
                    "The statistics population is only possible for JcrMetdataRepository implementations");
        }
        Session session = getSession(repositorySession);
        // TODO: these may be best as running totals, maintained by observations on the properties in JCR

        try {
            QueryManager queryManager = session.getWorkspace().getQueryManager();

            // TODO: Check, if this is still the case - Switched to Jackrabbit OAK with archiva 3.0
            // Former statement: JCR-SQL2 query will not complete on a large repo in Jackrabbit 2.2.0 - see JCR-2835
            //    Using the JCR-SQL2 variants gives
            //      "org.apache.lucene.search.BooleanQuery$TooManyClauses: maxClauseCount is set to 1024"
//            String whereClause = "WHERE ISDESCENDANTNODE([/repositories/" + repositoryId + "/content])";
//            Query query = queryManager.createQuery( "SELECT size FROM [archiva:artifact] " + whereClause,
//                                                    Query.JCR_SQL2 );
            String whereClause = "WHERE ISDESCENDANTNODE([/repositories/" + repositoryId + "/content])";
            Query query = queryManager.createQuery("SELECT type,size FROM [" + ARTIFACT_NODE_TYPE + "] " + whereClause, Query.JCR_SQL2);

            QueryResult queryResult = query.execute();

            Map<String, Integer> totalByType = new HashMap<>();
            long totalSize = 0, totalArtifacts = 0;
            for (Row row : JcrUtils.getRows(queryResult)) {
                Node n = row.getNode();
                log.debug("Result node {}", n);
                totalSize += row.getValue("size").getLong();

                String type;
                if (n.hasNode(MavenArtifactFacet.FACET_ID)) {
                    Node facetNode = n.getNode(MavenArtifactFacet.FACET_ID);
                    type = facetNode.getProperty("type").getString();
                } else {
                    type = "Other";
                }
                Integer prev = totalByType.get(type);
                totalByType.put(type, prev != null ? prev + 1 : 1);

                totalArtifacts++;
            }

            repositoryStatistics.setTotalArtifactCount(totalArtifacts);
            repositoryStatistics.setTotalArtifactFileSize(totalSize);
            for (Map.Entry<String, Integer> entry : totalByType.entrySet()) {
                log.info("Setting count for type: {} = {}", entry.getKey(), entry.getValue());
                repositoryStatistics.setTotalCountForType(entry.getKey(), entry.getValue());
            }

            // The query ordering is a trick to ensure that the size is correct, otherwise due to lazy init it will be -1
//            query = queryManager.createQuery( "SELECT * FROM [archiva:project] " + whereClause, Query.JCR_SQL2 );
            query = queryManager.createQuery("SELECT * FROM [archiva:project] " + whereClause + " ORDER BY [jcr:score]",
                    Query.JCR_SQL2);
            repositoryStatistics.setTotalProjectCount(query.execute().getRows().getSize());

//            query = queryManager.createQuery(
//                "SELECT * FROM [archiva:namespace] " + whereClause + " AND namespace IS NOT NULL", Query.JCR_SQL2 );
            query = queryManager.createQuery(
                    "SELECT * FROM [archiva:namespace] " + whereClause + " AND namespace IS NOT NULL ORDER BY [jcr:score]",
                    Query.JCR_SQL2);
            repositoryStatistics.setTotalGroupCount(query.execute().getRows().getSize());
        } catch (RepositoryException e) {
            throw new MetadataRepositoryException(e.getMessage(), e);
        }
    }