Search in sources :

Example 46 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class MongoDocumentStore method findAndModify.

@SuppressWarnings("unchecked")
@CheckForNull
private <T extends Document> T findAndModify(Collection<T> collection, UpdateOp updateOp, boolean upsert, boolean checkConditions) {
    DBCollection dbCollection = getDBCollection(collection);
    // make sure we don't modify the original updateOp
    updateOp = updateOp.copy();
    DBObject update = createUpdate(updateOp, false);
    Lock lock = null;
    if (collection == Collection.NODES) {
        lock = nodeLocks.acquire(updateOp.getId());
    }
    final Stopwatch watch = startWatch();
    boolean newEntry = false;
    try {
        // get modCount of cached document
        Long modCount = null;
        T cachedDoc = null;
        if (collection == Collection.NODES) {
            cachedDoc = (T) nodesCache.getIfPresent(updateOp.getId());
            if (cachedDoc != null) {
                modCount = cachedDoc.getModCount();
            }
        }
        // if we have a matching modCount
        if (modCount != null) {
            QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
            query.and(Document.MOD_COUNT).is(modCount);
            WriteResult result = dbCollection.update(query.get(), update);
            if (result.getN() > 0) {
                // success, update cached document
                if (collection == Collection.NODES) {
                    NodeDocument newDoc = (NodeDocument) applyChanges(collection, cachedDoc, updateOp);
                    nodesCache.put(newDoc);
                }
                // return previously cached document
                return cachedDoc;
            }
        }
        // conditional update failed or not possible
        // perform operation and get complete document
        QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
        DBObject oldNode = dbCollection.findAndModify(query.get(), null, null, /*sort*/
        false, /*remove*/
        update, false, /*returnNew*/
        upsert);
        if (oldNode == null) {
            newEntry = true;
        }
        if (checkConditions && oldNode == null) {
            return null;
        }
        T oldDoc = convertFromDBObject(collection, oldNode);
        if (oldDoc != null) {
            if (collection == Collection.NODES) {
                NodeDocument newDoc = (NodeDocument) applyChanges(collection, oldDoc, updateOp);
                nodesCache.put(newDoc);
                updateLocalChanges(newDoc);
            }
            oldDoc.seal();
        } else if (upsert) {
            if (collection == Collection.NODES) {
                NodeDocument doc = (NodeDocument) collection.newDocument(this);
                UpdateUtils.applyChanges(doc, updateOp);
                nodesCache.putIfAbsent(doc);
                updateLocalChanges(doc);
            }
        } else {
        // updateOp without conditions and not an upsert
        // this means the document does not exist
        }
        return oldDoc;
    } catch (Exception e) {
        throw handleException(e, collection, updateOp.getId());
    } finally {
        if (lock != null) {
            lock.unlock();
        }
        stats.doneFindAndModify(watch.elapsed(TimeUnit.NANOSECONDS), collection, updateOp.getId(), newEntry, true, 0);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) BulkWriteResult(com.mongodb.BulkWriteResult) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Lock(java.util.concurrent.locks.Lock) CheckForNull(javax.annotation.CheckForNull)

Example 47 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class MongoDocumentStore method findUncachedWithRetry.

/**
     * Finds a document and performs a number of retries if the read fails with
     * an exception.
     *
     * @param collection the collection to read from.
     * @param key the key of the document to find.
     * @param docReadPref the read preference.
     * @param retries the number of retries. Must not be negative.
     * @param <T> the document type of the given collection.
     * @return the document or {@code null} if the document doesn't exist.
     */
@CheckForNull
private <T extends Document> T findUncachedWithRetry(Collection<T> collection, String key, DocumentReadPreference docReadPref, int retries) {
    checkArgument(retries >= 0, "retries must not be negative");
    if (key.equals("0:/")) {
        LOG.trace("root node");
    }
    int numAttempts = retries + 1;
    MongoException ex = null;
    for (int i = 0; i < numAttempts; i++) {
        if (i > 0) {
            LOG.warn("Retrying read of " + key);
        }
        try {
            return findUncached(collection, key, docReadPref);
        } catch (MongoException e) {
            ex = e;
        }
    }
    if (ex != null) {
        throw ex;
    } else {
        // impossible to get here
        throw new IllegalStateException();
    }
}
Also used : MongoException(com.mongodb.MongoException) CheckForNull(javax.annotation.CheckForNull)

Example 48 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class MemoryDocumentStore method internalCreateOrUpdate.

@CheckForNull
private <T extends Document> T internalCreateOrUpdate(Collection<T> collection, UpdateOp update, boolean checkConditions) {
    ConcurrentSkipListMap<String, T> map = getMap(collection);
    T oldDoc;
    Lock lock = rwLock.writeLock();
    lock.lock();
    try {
        // get the node if it's there
        oldDoc = map.get(update.getId());
        T doc = collection.newDocument(this);
        if (oldDoc == null) {
            if (!update.isNew()) {
                throw new DocumentStoreException("Document does not exist: " + update.getId());
            }
        } else {
            oldDoc.deepCopy(doc);
        }
        if (checkConditions && !checkConditions(doc, update.getConditions())) {
            return null;
        }
        // update the document
        UpdateUtils.applyChanges(doc, update);
        doc.seal();
        map.put(update.getId(), doc);
        return oldDoc;
    } finally {
        lock.unlock();
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) CheckForNull(javax.annotation.CheckForNull)

Example 49 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class AccessControlImporter method getACL.

@CheckForNull
private JackrabbitAccessControlList getACL(Tree tree) throws RepositoryException {
    String nodeName = tree.getName();
    JackrabbitAccessControlList acList = null;
    if (!tree.isRoot()) {
        Tree parent = tree.getParent();
        if (AccessControlConstants.REP_POLICY.equals(nodeName) && ntMgr.isNodeType(tree, AccessControlConstants.NT_REP_ACL)) {
            String path = parent.getPath();
            acList = getACL(path);
        } else if (AccessControlConstants.REP_REPO_POLICY.equals(nodeName) && ntMgr.isNodeType(tree, AccessControlConstants.NT_REP_ACL) && parent.isRoot()) {
            acList = getACL((String) null);
        }
    }
    if (acList != null) {
        // clear all existing entries
        for (AccessControlEntry ace : acList.getAccessControlEntries()) {
            acList.removeAccessControlEntry(ace);
        }
    }
    return acList;
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) AccessControlEntry(javax.jcr.security.AccessControlEntry) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) CheckForNull(javax.annotation.CheckForNull)

Example 50 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class LoginModuleImpl method getUserAuthentication.

@CheckForNull
private Authentication getUserAuthentication(@Nullable String loginName) {
    SecurityProvider securityProvider = getSecurityProvider();
    Root root = getRoot();
    if (securityProvider != null && root != null) {
        UserConfiguration uc = securityProvider.getConfiguration(UserConfiguration.class);
        UserAuthenticationFactory factory = uc.getParameters().getConfigValue(UserConstants.PARAM_USER_AUTHENTICATION_FACTORY, null, UserAuthenticationFactory.class);
        if (factory != null) {
            return factory.getAuthentication(uc, root, loginName);
        } else {
            log.error("No user authentication factory configured in user configuration.");
        }
    }
    return null;
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) UserAuthenticationFactory(org.apache.jackrabbit.oak.spi.security.user.UserAuthenticationFactory) SecurityProvider(org.apache.jackrabbit.oak.spi.security.SecurityProvider) UserConfiguration(org.apache.jackrabbit.oak.spi.security.user.UserConfiguration) CheckForNull(javax.annotation.CheckForNull)

Aggregations

CheckForNull (javax.annotation.CheckForNull)149 IOException (java.io.IOException)18 Tree (org.apache.jackrabbit.oak.api.Tree)16 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)12 ArrayList (java.util.ArrayList)9 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)9 Stopwatch (com.google.common.base.Stopwatch)8 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)8 Date (java.util.Date)7 SnapshotDto (org.sonar.db.component.SnapshotDto)7 Period (org.sonar.server.computation.task.projectanalysis.period.Period)7 File (java.io.File)6 SQLException (java.sql.SQLException)6 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)6 ExecutionException (java.util.concurrent.ExecutionException)5 ValidationModel (org.apache.sling.validation.model.ValidationModel)5 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)4 Root (org.apache.jackrabbit.oak.api.Root)4 Utils.resolveCommitRevision (org.apache.jackrabbit.oak.plugins.document.util.Utils.resolveCommitRevision)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4