Search in sources :

Example 11 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class InMemBundlePersistenceManager method loadReferencesTo.

/**
     * {@inheritDoc}
     */
public NodeReferences loadReferencesTo(NodeId id) throws NoSuchItemStateException, ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    if (!refsStore.containsKey(id)) {
        throw new NoSuchItemStateException(id.toString());
    }
    try {
        NodeReferences refs = new NodeReferences(id);
        Serializer.deserialize(refs, new ByteArrayInputStream(refsStore.get(id)));
        return refs;
    } catch (Exception e) {
        String msg = "failed to read references: " + id;
        log.error(msg, e);
        throw new ItemStateException(msg, e);
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 12 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class PersistenceCopier method copy.

/**
     * Copies the given node state and all associated property states
     * to the target persistence manager.
     *
     * @param sourceNode source node state
     * @throws RepositoryException if the copy operation fails
     */
private void copy(NodeState sourceNode) throws RepositoryException {
    try {
        ChangeLog changes = new ChangeLog();
        // Copy the node state
        NodeState targetNode = target.createNew(sourceNode.getNodeId());
        targetNode.setParentId(sourceNode.getParentId());
        targetNode.setNodeTypeName(sourceNode.getNodeTypeName());
        targetNode.setMixinTypeNames(sourceNode.getMixinTypeNames());
        targetNode.setPropertyNames(sourceNode.getPropertyNames());
        targetNode.setChildNodeEntries(sourceNode.getChildNodeEntries());
        if (target.exists(targetNode.getNodeId())) {
            changes.modified(targetNode);
        } else {
            changes.added(targetNode);
        }
        // Copy all associated property states
        for (Name name : sourceNode.getPropertyNames()) {
            PropertyId id = new PropertyId(sourceNode.getNodeId(), name);
            PropertyState sourceState = source.load(id);
            PropertyState targetState = target.createNew(id);
            targetState.setType(sourceState.getType());
            targetState.setMultiValued(sourceState.isMultiValued());
            InternalValue[] values = sourceState.getValues();
            if (sourceState.getType() == PropertyType.BINARY) {
                for (int i = 0; i < values.length; i++) {
                    InputStream stream = values[i].getStream();
                    try {
                        values[i] = InternalValue.create(stream, store);
                    } finally {
                        stream.close();
                    }
                }
            }
            targetState.setValues(values);
            if (target.exists(targetState.getPropertyId())) {
                changes.modified(targetState);
            } else {
                changes.added(targetState);
            }
        }
        // Copy all node references
        if (source.existsReferencesTo(sourceNode.getNodeId())) {
            changes.modified(source.loadReferencesTo(sourceNode.getNodeId()));
        } else if (target.existsReferencesTo(sourceNode.getNodeId())) {
            NodeReferences references = target.loadReferencesTo(sourceNode.getNodeId());
            references.clearAllReferences();
            changes.modified(references);
        }
        // Persist the copied states
        target.store(changes);
    } catch (IOException e) {
        throw new RepositoryException("Unable to copy binary values of " + sourceNode, e);
    } catch (ItemStateException e) {
        throw new RepositoryException("Unable to copy " + sourceNode, e);
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) InputStream(java.io.InputStream) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) ChangeLog(org.apache.jackrabbit.core.state.ChangeLog)

Example 13 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class BundleFsPersistenceManager method loadReferencesTo.

/**
     * {@inheritDoc}
     */
public synchronized NodeReferences loadReferencesTo(NodeId targetId) throws NoSuchItemStateException, ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    InputStream in = null;
    try {
        String path = buildNodeReferencesFilePath(null, targetId).toString();
        if (!itemFs.exists(path)) {
            // special case
            throw new NoSuchItemStateException(targetId.toString());
        }
        in = itemFs.getInputStream(path);
        NodeReferences refs = new NodeReferences(targetId);
        Serializer.deserialize(refs, in);
        return refs;
    } catch (NoSuchItemStateException e) {
        throw e;
    } catch (Exception e) {
        String msg = "failed to read references: " + targetId;
        BundleFsPersistenceManager.log.error(msg, e);
        throw new ItemStateException(msg, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) InputStream(java.io.InputStream) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) SQLException(java.sql.SQLException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 14 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class InternalVersionManagerBase method internalRemoveActivity.

/**
     * Removes the specified activity
     *
     * @param activity the activity to remove
     * @throws javax.jcr.RepositoryException if any other error occurs.
     */
protected void internalRemoveActivity(InternalActivityImpl activity) throws RepositoryException {
    WriteOperation operation = startWriteOperation();
    try {
        // check if the activity has any references in the workspaces
        NodeId nodeId = activity.getId();
        if (stateMgr.hasNodeReferences(nodeId)) {
            NodeReferences refs = stateMgr.getNodeReferences(nodeId);
            if (refs.hasReferences()) {
                throw new ReferentialIntegrityException("Unable to delete activity. still referenced.");
            }
        }
        // TODO:
        // check if the activity is used in anywhere in the version storage
        // and reject removal
        // remove activity and possible empty parent directories
        NodeStateEx act = getNodeStateEx(nodeId);
        NodeId parentId = act.getParentId();
        Name name = act.getName();
        while (parentId != null) {
            NodeStateEx parent = getNodeStateEx(parentId);
            parent.removeNode(name);
            parent.store();
            if (parent.getChildNodes().length == 0 && !parentId.equals(activitiesId)) {
                name = parent.getName();
                parentId = parent.getParentId();
            } else {
                parentId = null;
            }
        }
        operation.save();
    } catch (ItemStateException e) {
        log.error("Error while storing: " + e.toString());
    } finally {
        operation.close();
    }
}
Also used : ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) NodeId(org.apache.jackrabbit.core.id.NodeId) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) Name(org.apache.jackrabbit.spi.Name) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 15 with NodeReferences

use of org.apache.jackrabbit.core.state.NodeReferences in project jackrabbit by apache.

the class PersistenceManagerTest method assertCreateUpdateDelete.

private void assertCreateUpdateDelete(PersistenceManager manager) throws Exception {
    NodeState node = new NodeState(NODE_ID, TEST, RepositoryImpl.ROOT_NODE_ID, ItemState.STATUS_NEW, true);
    node.addChildNodeEntry(TEST, CHILD_ID);
    node.addPropertyName(NameConstants.JCR_PRIMARYTYPE);
    node.addPropertyName(TEST);
    NodeState child = new NodeState(CHILD_ID, TEST, NODE_ID, ItemState.STATUS_NEW, true);
    child.addPropertyName(NameConstants.JCR_PRIMARYTYPE);
    PropertyState property = new PropertyState(PROPERTY_ID, ItemState.STATUS_NEW, true);
    property.setType(PropertyType.REFERENCE);
    property.setValues(new InternalValue[] { InternalValue.create(CHILD_ID) });
    NodeReferences references = new NodeReferences(CHILD_ID);
    references.addReference(PROPERTY_ID);
    ChangeLog create = new ChangeLog();
    create.added(node);
    create.added(child);
    create.added(property);
    create.modified(references);
    manager.store(create);
    assertTrue(manager.exists(NODE_ID));
    assertTrue(manager.exists(CHILD_ID));
    assertTrue(manager.exists(PROPERTY_ID));
    assertTrue(manager.existsReferencesTo(CHILD_ID));
    assertEquals(node, manager.load(NODE_ID));
    assertEquals(child, manager.load(CHILD_ID));
    assertEquals(property, manager.load(PROPERTY_ID));
    assertEquals(references, manager.loadReferencesTo(CHILD_ID));
    references.removeReference(PROPERTY_ID);
    node.setStatus(ItemState.STATUS_EXISTING);
    ChangeLog update = new ChangeLog();
    update.modified(references);
    node.removePropertyName(TEST);
    update.deleted(property);
    update.modified(node);
    manager.store(update);
    assertTrue(manager.exists(NODE_ID));
    assertTrue(manager.exists(CHILD_ID));
    assertFalse(manager.exists(PROPERTY_ID));
    assertFalse(manager.existsReferencesTo(CHILD_ID));
    assertEquals(node, manager.load(NODE_ID));
    assertEquals(child, manager.load(CHILD_ID));
    ChangeLog delete = new ChangeLog();
    delete.deleted(child);
    delete.deleted(node);
    manager.store(delete);
    assertFalse(manager.exists(NODE_ID));
    assertFalse(manager.exists(CHILD_ID));
    assertFalse(manager.exists(PROPERTY_ID));
    assertFalse(manager.existsReferencesTo(CHILD_ID));
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) ChangeLog(org.apache.jackrabbit.core.state.ChangeLog) PropertyState(org.apache.jackrabbit.core.state.PropertyState)

Aggregations

NodeReferences (org.apache.jackrabbit.core.state.NodeReferences)15 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)13 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)10 RepositoryException (javax.jcr.RepositoryException)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 PropertyId (org.apache.jackrabbit.core.id.PropertyId)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 NodeId (org.apache.jackrabbit.core.id.NodeId)4 SQLException (java.sql.SQLException)3 FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)3 ChangeLog (org.apache.jackrabbit.core.state.ChangeLog)3 NodeState (org.apache.jackrabbit.core.state.NodeState)3 Name (org.apache.jackrabbit.spi.Name)3 FilterInputStream (java.io.FilterInputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ResultSet (java.sql.ResultSet)2 InvalidItemStateException (javax.jcr.InvalidItemStateException)2 ReferentialIntegrityException (javax.jcr.ReferentialIntegrityException)2 PropertyState (org.apache.jackrabbit.core.state.PropertyState)2