use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class XMLPersistenceManager method store.
/**
* {@inheritDoc}
*/
protected void store(NodeState state) throws ItemStateException {
if (!initialized) {
throw new IllegalStateException("not initialized");
}
NodeId id = state.getNodeId();
String nodeFilePath = buildNodeFilePath(id);
FileSystemResource nodeFile = new FileSystemResource(itemStateFS, nodeFilePath);
try {
nodeFile.makeParentDirs();
OutputStream os = nodeFile.getOutputStream();
Writer writer = null;
try {
String encoding = DEFAULT_ENCODING;
try {
writer = new BufferedWriter(new OutputStreamWriter(os, encoding));
} catch (UnsupportedEncodingException e) {
// should never get here!
OutputStreamWriter osw = new OutputStreamWriter(os);
encoding = osw.getEncoding();
writer = new BufferedWriter(osw);
}
String parentId = (state.getParentId() == null) ? "" : state.getParentId().toString();
String encodedNodeType = Text.encodeIllegalXMLCharacters(state.getNodeTypeName().toString());
writer.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
writer.write("<" + NODE_ELEMENT + " " + UUID_ATTRIBUTE + "=\"" + id + "\" " + PARENTUUID_ATTRIBUTE + "=\"" + parentId + "\" " + MODCOUNT_ATTRIBUTE + "=\"" + state.getModCount() + "\" " + NODETYPE_ATTRIBUTE + "=\"" + encodedNodeType + "\">\n");
// mixin types
writer.write("\t<" + MIXINTYPES_ELEMENT + ">\n");
for (Name mixin : state.getMixinTypeNames()) {
writer.write("\t\t<" + MIXINTYPE_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(mixin.toString()) + "\"/>\n");
}
writer.write("\t</" + MIXINTYPES_ELEMENT + ">\n");
// properties
writer.write("\t<" + PROPERTIES_ELEMENT + ">\n");
for (Name propName : state.getPropertyNames()) {
writer.write("\t\t<" + PROPERTY_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(propName.toString()) + "\">\n");
// @todo serialize type, definition id and values
writer.write("\t\t</" + PROPERTY_ELEMENT + ">\n");
}
writer.write("\t</" + PROPERTIES_ELEMENT + ">\n");
// child nodes
writer.write("\t<" + NODES_ELEMENT + ">\n");
for (ChildNodeEntry entry : state.getChildNodeEntries()) {
writer.write("\t\t<" + NODE_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(entry.getName().toString()) + "\" " + UUID_ATTRIBUTE + "=\"" + entry.getId() + "\">\n");
writer.write("\t\t</" + NODE_ELEMENT + ">\n");
}
writer.write("\t</" + NODES_ELEMENT + ">\n");
writer.write("</" + NODE_ELEMENT + ">\n");
} finally {
writer.close();
}
} catch (Exception e) {
String msg = "failed to write node state: " + id;
log.debug(msg);
throw new ItemStateException(msg, e);
}
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class ConsistencyCheck method getPath.
/**
* Returns the path for <code>node</code>. If an error occurs this method
* returns the uuid of the node.
*
* @param node the node to retrieve the path from
* @return the path of the node or its uuid.
*/
private String getPath(NodeState node) {
// remember as fallback
String uuid = node.getNodeId().toString();
StringBuilder path = new StringBuilder();
List<ChildNodeEntry> elements = new ArrayList<ChildNodeEntry>();
try {
while (node.getParentId() != null) {
NodeId parentId = node.getParentId();
NodeState parent = (NodeState) stateMgr.getItemState(parentId);
ChildNodeEntry entry = parent.getChildNodeEntry(node.getNodeId());
if (entry == null) {
log.warn("Failed to build path: abandoned child {} of node {}. " + "Please run a repository consistency check", node.getNodeId(), parentId);
return uuid;
}
elements.add(entry);
node = parent;
}
for (int i = elements.size() - 1; i > -1; i--) {
ChildNodeEntry entry = elements.get(i);
path.append('/').append(entry.getName().getLocalName());
if (entry.getIndex() > 1) {
path.append('[').append(entry.getIndex()).append(']');
}
}
if (path.length() == 0) {
path.append('/');
}
return path.toString();
} catch (ItemStateException e) {
return uuid;
}
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class InternalVersionManagerBase method getVersionHistoryInfoForNode.
/**
* Returns information about the version history of the specified node
* or <code>null</code> when unavailable.
*
* @param node node whose version history should be returned
* @return identifiers of the version history and root version nodes
* @throws RepositoryException if an error occurs
*/
public VersionHistoryInfo getVersionHistoryInfoForNode(NodeState node) throws RepositoryException {
VersionHistoryInfo info = null;
VersioningLock.ReadLock lock = acquireReadLock();
try {
String uuid = node.getNodeId().toString();
Name name = getName(uuid);
NodeStateEx parent = getParentNode(getHistoryRoot(), uuid, null);
if (parent != null && parent.hasNode(name)) {
NodeStateEx history = parent.getNode(name, 1);
if (history == null) {
throw new InconsistentVersioningState("Unexpected failure to get child node " + name + " on parent node " + parent.getNodeId());
}
ChildNodeEntry rootv = history.getState().getChildNodeEntry(JCR_ROOTVERSION, 1);
if (rootv == null) {
throw new InconsistentVersioningState("missing child node entry for " + JCR_ROOTVERSION + " on version history node " + history.getNodeId(), history.getNodeId(), null);
}
info = new VersionHistoryInfo(history.getNodeId(), rootv.getId());
}
} finally {
lock.release();
}
return info;
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class InternalVersionHistoryImpl method init.
/**
* Initializes the history and loads all internal caches
*
* @throws RepositoryException if an error occurs
*/
private synchronized void init() throws RepositoryException {
nameCache.clear();
versionCache.clear();
labelCache.clear();
// get id
historyId = node.getNodeId();
// get versionable id
versionableId = NodeId.valueOf(node.getPropertyValue(NameConstants.JCR_VERSIONABLEUUID).toString());
// get label node
labelNode = node.getNode(NameConstants.JCR_VERSIONLABELS, 1);
// init label cache
try {
PropertyState[] labels = labelNode.getProperties();
for (PropertyState pState : labels) {
if (pState.getType() == PropertyType.REFERENCE) {
Name labelName = pState.getName();
NodeId id = pState.getValues()[0].getNodeId();
if (node.getState().hasChildNodeEntry(id)) {
labelCache.put(labelName, node.getState().getChildNodeEntry(id).getName());
} else {
log.warn("Error while resolving label reference. Version missing: " + id);
}
}
}
} catch (ItemStateException e) {
throw new RepositoryException(e);
}
// get root version
rootVersion = createVersionInstance(NameConstants.JCR_ROOTVERSION);
// get version entries
for (ChildNodeEntry child : node.getState().getChildNodeEntries()) {
if (child.getName().equals(NameConstants.JCR_VERSIONLABELS)) {
continue;
}
nameCache.put(child.getName(), child.getId());
}
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class NodeImpl method getPrimaryPath.
/**
* {@inheritDoc}
*
* Overridden to return a different path for shareable nodes.
*
* TODO SN: copies functionality in that is already available in
* HierarchyManagerImpl, namely composing a path by
* concatenating the parent path + this node's name and index:
* rather use hierarchy manager to do this
*/
@Override
public Path getPrimaryPath() throws RepositoryException {
if (!isShareable()) {
return super.getPrimaryPath();
}
NodeId parentId = getParentId();
NodeImpl parentNode = (NodeImpl) getParent();
Path parentPath = parentNode.getPrimaryPath();
PathBuilder builder = new PathBuilder(parentPath);
ChildNodeEntry entry = parentNode.getNodeState().getChildNodeEntry(getNodeId());
if (entry == null) {
String msg = "failed to build path of " + id + ": " + parentId + " has no child entry for " + id;
log.debug(msg);
throw new ItemNotFoundException(msg);
}
// add to path
if (entry.getIndex() == 1) {
builder.addLast(entry.getName());
} else {
builder.addLast(entry.getName(), entry.getIndex());
}
return builder.getPath();
}
Aggregations