Package org.modeshape.jcr.cache

Examples of org.modeshape.jcr.cache.SessionCache$SaveContext


        }
        JcrAccessControlList acl = (JcrAccessControlList)policy;
        Map<String, Set<String>> privilegesByPrincipalName = privilegesByPrincipalName(acl);
        try {
            CachedNode cacheNode = session.cachedNode(session.pathFactory().create(absPath), false);
            SessionCache cache = session.cache();
            MutableCachedNode mutableNode = cache.mutable(cacheNode.getKey());
            MutableCachedNode.PermissionChanges permissionChanges = mutableNode.setPermissions(cache, privilegesByPrincipalName);
            session.aclAdded(permissionChanges.addedPrincipalsCount());
            session.aclRemoved(permissionChanges.removedPrincipalsCount());
        } catch (UnsupportedOperationException e) {
            throw new RepositoryException(e);
View Full Code Here


            throw new AccessDeniedException();
        }

        try {
            CachedNode cacheNode = session.cachedNode(session.pathFactory().create(absPath), false);
            SessionCache cache = session.cache();
            MutableCachedNode mutableNode = cache.mutable(cacheNode.getKey());
            MutableCachedNode.PermissionChanges permissionChanges = mutableNode.removeACL(cache);
            session.aclRemoved(permissionChanges.removedPrincipalsCount());
        } catch (UnsupportedOperationException e) {
            throw new RepositoryException(e);
        }
View Full Code Here

     */
    private JcrAccessControlList findAccessList( String absPath,
                                                 boolean searchParents ) throws PathNotFoundException, RepositoryException {
        // this will not load any nodes in the JCR session, but might load the entire hierarchy in the node cache
        CachedNode startingNode = session.cachedNode(session.pathFactory().create(absPath), false);
        SessionCache sessionCache = session.cache();
        Map<String, Set<String>> permissions = startingNode.getPermissions(sessionCache);
        CachedNode node = startingNode;
        if (permissions == null && searchParents) {
            // walk up the hierarchy until we get a set of permissions or we reach the root or a missing parent
            while (permissions == null) {
                NodeKey parentKey = node.getParentKey(sessionCache);
                if (parentKey == null) {
                    break;
                }
                node = sessionCache.getNode(parentKey);
                if (node == null) {
                    break;
                }
                permissions = node.getPermissions(sessionCache);
            }
View Full Code Here

        }
    }

    private void loadStoredProjections( boolean validate ) {
        assert !initialized;
        SessionCache systemSession = repository.createSystemSession(repository.context(), false);

        CachedNode systemNode = getSystemNode(systemSession);
        ChildReference federationNodeRef = systemNode.getChildReferences(systemSession).getChild(ModeShapeLexicon.FEDERATION);
        if (federationNodeRef != null) {
            Collection<Projection> newProjections = loadStoredProjections(systemSession, federationNodeRef, validate);
View Full Code Here

                // then update the internal (parent) node and remove its external child
                NodeKey projectedNodeKey = new NodeKey(projectedNodeKeyString);

                String wsName = workspaceNameByKey.get(projectedNodeKey.getWorkspaceKey());
                if (!StringUtil.isBlank(wsName)) {
                    SessionCache sessionCache = repository.repositoryCache().createSession(repository.context(), wsName, false);
                    MutableCachedNode parentNode = sessionCache.mutable(projectedNodeKey);
                    parentNode.removeFederatedSegment(externalNodeKeyString);
                    sessionCache.save();
                }
            }
        }

        if (!invalidProjections.isEmpty()) {
View Full Code Here

    private void storeProjection( Projection projection ) {
        PropertyFactory propertyFactory = repository.context().getPropertyFactory();

        // we need to store the projection mappings so that we don't loose that information
        SessionCache systemSession = repository.createSystemSession(repository.context(), false);
        NodeKey systemNodeKey = getSystemNode(systemSession).getKey();
        MutableCachedNode systemNode = systemSession.mutable(systemNodeKey);
        ChildReference federationNodeRef = systemNode.getChildReferences(systemSession).getChild(ModeShapeLexicon.FEDERATION);

        if (federationNodeRef == null) {
            // there isn't a federation node present, so we need to add it
            try {
                Property primaryType = propertyFactory.create(JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.FEDERATION);
                systemNode.createChild(systemSession, systemNodeKey.withId("mode:federation"), ModeShapeLexicon.FEDERATION,
                                       primaryType);
                systemSession.save();
                federationNodeRef = systemNode.getChildReferences(systemSession).getChild(ModeShapeLexicon.FEDERATION);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        NodeKey federationNodeKey = federationNodeRef.getKey();
        MutableCachedNode federationNode = systemSession.mutable(federationNodeKey);

        Property primaryType = propertyFactory.create(JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.PROJECTION);
        Property externalNodeKeyProp = propertyFactory.create(ModeShapeLexicon.EXTERNAL_NODE_KEY, projection.getExternalNodeKey());

        Property projectedNodeKeyProp = propertyFactory.create(ModeShapeLexicon.PROJECTED_NODE_KEY,
                                                               projection.getProjectedNodeKey());
        Property alias = propertyFactory.create(ModeShapeLexicon.PROJECTION_ALIAS, projection.getAlias());
        federationNode.createChild(systemSession, federationNodeKey.withRandomId(), ModeShapeLexicon.PROJECTION, primaryType,
                                   externalNodeKeyProp, projectedNodeKeyProp, alias);

        systemSession.save();
    }
View Full Code Here

     * This method removes a persisted projection definition, but does not update the {@link #snapshot} instance.
     *
     * @param externalNodeKey the external key for the projection
     */
    private void removeStoredProjection( String externalNodeKey ) {
        SessionCache systemSession = repository.createSystemSession(repository.context(), false);
        NodeKey systemNodeKey = getSystemNode(systemSession).getKey();
        MutableCachedNode systemNode = systemSession.mutable(systemNodeKey);
        ChildReference federationNodeRef = systemNode.getChildReferences(systemSession).getChild(ModeShapeLexicon.FEDERATION);

        // if we're removing a projection, one had to be stored previously, so there should be a federation node present
        assert federationNodeRef != null;

        NodeKey federationNodeKey = federationNodeRef.getKey();
        MutableCachedNode federationNode = systemSession.mutable(federationNodeKey);
        ChildReferences federationChildRefs = federationNode.getChildReferences(systemSession);

        int projectionsCount = federationChildRefs.getChildCount(ModeShapeLexicon.PROJECTION);

        for (int i = 1; i <= projectionsCount; i++) {
            ChildReference projectionRef = federationChildRefs.getChild(ModeShapeLexicon.PROJECTION, i);
            NodeKey projectionRefKey = projectionRef.getKey();
            CachedNode storedProjection = systemSession.getNode(projectionRefKey);
            String storedProjectionExternalNodeKey = storedProjection.getProperty(ModeShapeLexicon.EXTERNAL_NODE_KEY,
                                                                                  systemSession).getFirstValue().toString();
            assert storedProjectionExternalNodeKey != null;
            if (storedProjectionExternalNodeKey.equals(externalNodeKey)) {
                federationNode.removeChild(systemSession, projectionRefKey);
                systemSession.destroy(projectionRefKey);
                systemSession.save();
                break;
            }
        }
    }
View Full Code Here

     * Refresh the locks from the stored representation.
     */
    protected void refreshFromSystem() {
        try {
            // Re-read and re-register all of the namespaces ...
            SessionCache systemCache = repository.createSystemSession(repository.context(), true);
            SystemContent system = new SystemContent(systemCache);
            CachedNode locks = system.locksNode();
            for (ChildReference ref : locks.getChildReferences(systemCache)) {
                CachedNode node = systemCache.getNode(ref);
                if (node == null) {
                    logger.warn(JcrI18n.lockNotFound, ref.getKey());
                    continue;
                }
                ModeShapeLock lock = new ModeShapeLock(node, systemCache);
View Full Code Here

            DateTimeFactory dates = context.getValueFactories().getDateFactory();
            DateTime now = dates.create();
            DateTime newExpiration = dates.create(now, RepositoryConfiguration.LOCK_EXTENSION_INTERVAL_IN_MILLIS);

            PropertyFactory propertyFactory = context.getPropertyFactory();
            SessionCache systemSession = repository.createSystemSession(context, false);
            SystemContent systemContent = new SystemContent(systemSession);

            Map<String, List<NodeKey>> lockedNodesByWorkspaceName = new HashMap<>();
            // Iterate over the locks ...
            MutableCachedNode locksNode = systemContent.mutableLocksNode();
            for (ChildReference ref : locksNode.getChildReferences(systemSession)) {
                NodeKey lockKey = ref.getKey();
                CachedNode lockNode = systemSession.getNode(lockKey);
                if (lockNode == null) {
                    //it may happen that another thread has performed a session.logout which means the lock might have been removed
                    continue;
                }
                ModeShapeLock lock = new ModeShapeLock(lockNode, systemSession);
                NodeKey lockedNodeKey = lock.getLockedNodeKey();
                if (lock.isSessionScoped() && activeSessionIds.contains(lock.getLockingSessionId())) {
                    //for active session locks belonging to the sessions of this process, we want to extend the expiration date
                    //so that other processes in a cluster can tell that this lock is still active
                    MutableCachedNode mutableLockNode = systemSession.mutable(lockKey);
                    Property prop = propertyFactory.create(ModeShapeLexicon.EXPIRATION_DATE, newExpiration);
                    mutableLockNode.setProperty(systemSession, prop);
                    //reflect the change in the expiry date in the internal map
                    this.locksByNodeKey.replace(lockedNodeKey, lock.withExpiryTime(newExpiration));

                    continue;
                }

                //if it's not an active session lock, we always check the expiry date
                DateTime expirationDate = firstDate(lockNode.getProperty(ModeShapeLexicon.EXPIRATION_DATE, systemSession));
                if (expirationDate.isBefore(now)) {
                    //remove the lock from the system area
                    systemContent.removeLock(lock);
                    //register the target node which needs cleaning
                    List<NodeKey> lockedNodes = lockedNodesByWorkspaceName.get(lock.getWorkspaceName());
                    if (lockedNodes == null) {
                        lockedNodes = new ArrayList<>();
                        lockedNodesByWorkspaceName.put(lock.getWorkspaceName(), lockedNodes);
                    }
                    lockedNodes.add(lockedNodeKey);
                }
            }
            //persist all the changes to the locks from the system area
            systemSession.save();

            //update each of nodes which has been unlocked
            for (String workspaceName : lockedNodesByWorkspaceName.keySet()) {
                SessionCache internalSession = repository.repositoryCache().createSession(context, workspaceName, false);
                for (NodeKey lockedNodeKey : lockedNodesByWorkspaceName.get(workspaceName)) {
                    //clear the internal cache
                    this.locksByNodeKey.remove(lockedNodeKey);

                    CachedNode lockedNode = internalSession.getWorkspace().getNode(lockedNodeKey);
                    if (lockedNode != null) {
                        MutableCachedNode mutableLockedNode = internalSession.mutable(lockedNodeKey);
                        mutableLockedNode.removeProperty(internalSession, JcrLexicon.LOCK_IS_DEEP);
                        mutableLockedNode.removeProperty(internalSession, JcrLexicon.LOCK_OWNER);
                        mutableLockedNode.unlock();
                    }
                }
                internalSession.save();
            }
        } catch (Throwable t) {
            logger.error(t, JcrI18n.errorCleaningUpLocks, repository.name());
        }
    }
View Full Code Here

            expirationTimeInMillis = TimeUnit.MILLISECONDS.convert(timeoutHint, TimeUnit.SECONDS);
        }
        DateTime expirationDate = dateFactory.create().plus(expirationTimeInMillis, TimeUnit.MILLISECONDS);

        // Create a new lock ...
        SessionCache systemSession = repository.createSystemSession(context, false);
        SystemContent system = new SystemContent(systemSession);
        NodeKey nodeKey = node.getKey();
        NodeKey lockKey = generateLockKey(system.locksKey(), nodeKey);
        String token = generateLockToken();
        ModeShapeLock lock = new ModeShapeLock(nodeKey, lockKey, session.workspaceName(), owner, token, isDeep, isSessionScoped,
                                               session.sessionId(), expirationDate);

        if (isDeep) {
            NodeCache cache = session.cache();
            CachedNode locked = findLockedNodeAtOrBelow(node, cache);
            if (locked != null) {
                String nodePath = session.stringFactory().create(node.getPath(cache));
                String descendantPath = session.stringFactory().create(locked.getPath(cache));
                throw new LockException(JcrI18n.descendantAlreadyLocked.text(nodePath, descendantPath));
            }
        }

        ModeShapeLock existing = locksByNodeKey.putIfAbsent(nodeKey, lock);
        if (existing != null) {
            NodeCache cache = session.cache();
            CachedNode locked = cache.getNode(existing.getLockedNodeKey());
            String lockedPath = session.stringFactory().create(locked.getPath(cache));
            throw new LockException(JcrI18n.alreadyLocked.text(lockedPath));
        }

        try {
            // Store the lock within the system area ...
            system.storeLock(lock);

            // Update the persistent node ...
            SessionCache lockingSession = session.spawnSessionCache(false);
            MutableCachedNode lockedNode = lockingSession.mutable(nodeKey);

            PropertyFactory propertyFactory = session.propertyFactory();
            lockedNode.setProperty(lockingSession, propertyFactory.create(JcrLexicon.LOCK_OWNER, owner));
            lockedNode.setProperty(lockingSession, propertyFactory.create(JcrLexicon.LOCK_IS_DEEP, isDeep));
            lockedNode.lock(isSessionScoped);

            // Now save both sessions. This will fail with a LockFailureException if the locking failed ...
            lockingSession.save(systemSession, null);
        } catch (LockFailureException e) {
            // Someone must have snuck in and locked the node, and we just didn't receive notification of it yet ...
            String location = nodeKey.toString();
            try {
                location = session.node(nodeKey, null).getPath();
View Full Code Here

TOP

Related Classes of org.modeshape.jcr.cache.SessionCache$SaveContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.