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);
}
}
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);
}
}
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);
}
}
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();
}
}
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));
}
Aggregations