use of org.apache.jackrabbit.core.state.ItemState in project jackrabbit by apache.
the class SearchIndex method checkPendingJournalChanges.
/**
* In the case of an initial index build operation, this checks if there are
* some new nodes pending in the journal and tries to preemptively delete
* them, to keep the index consistent.
*
* See JCR-3162
*
* @param context
* @throws IOException
*/
private void checkPendingJournalChanges(QueryHandlerContext context) {
ClusterNode cn = context.getClusterNode();
if (cn == null) {
return;
}
List<NodeId> addedIds = new ArrayList<NodeId>();
long rev = cn.getRevision();
List<ChangeLogRecord> changes = getChangeLogRecords(rev, context.getWorkspace());
Iterator<ChangeLogRecord> iterator = changes.iterator();
while (iterator.hasNext()) {
ChangeLogRecord record = iterator.next();
for (ItemState state : record.getChanges().addedStates()) {
if (!state.isNode()) {
continue;
}
addedIds.add((NodeId) state.getId());
}
}
if (!addedIds.isEmpty()) {
Collection<NodeState> empty = Collections.emptyList();
try {
updateNodes(addedIds.iterator(), empty.iterator());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
use of org.apache.jackrabbit.core.state.ItemState in project jackrabbit by apache.
the class HierarchyManagerImpl method getShareRelativeDepth.
/**
* {@inheritDoc}
*/
public int getShareRelativeDepth(NodeId ancestor, ItemId descendant) throws ItemNotFoundException, RepositoryException {
if (ancestor.equals(descendant)) {
return 0;
}
int depth = 1;
try {
ItemState state = getItemState(descendant);
Set<NodeId> parentIds = getParentIds(state, true);
while (parentIds.size() > 0) {
if (parentIds.contains(ancestor)) {
return depth;
}
depth++;
Set<NodeId> grandparentIds = new LinkedHashSet<NodeId>();
for (NodeId parentId : parentIds) {
state = getItemState(parentId);
grandparentIds.addAll(getParentIds(state, true));
}
parentIds = grandparentIds;
}
// not an ancestor
return -1;
} catch (NoSuchItemStateException nsise) {
String msg = "failed to determine degree of relationship of " + ancestor + " and " + descendant;
log.debug(msg);
throw new ItemNotFoundException(msg, nsise);
} catch (ItemStateException ise) {
String msg = "failed to determine degree of relationship of " + ancestor + " and " + descendant;
log.debug(msg);
throw new RepositoryException(msg, ise);
}
}
use of org.apache.jackrabbit.core.state.ItemState in project jackrabbit by apache.
the class ItemManager method getDefinition.
NodeDefinitionImpl getDefinition(NodeState state) throws RepositoryException {
if (state.getId().equals(sessionContext.getRootNodeId())) {
// special handling required for root node
return rootNodeDef;
}
NodeId parentId = state.getParentId();
if (parentId == null) {
// removed state has parentId set to null
// get from overlayed state
ItemState overlaid = state.getOverlayedState();
if (overlaid != null) {
parentId = overlaid.getParentId();
} else {
throw new InvalidItemStateException("Could not find parent of node " + state.getNodeId());
}
}
NodeState parentState = null;
try {
// access the parent state circumventing permission check, since
// read permission on the parent isn't required in order to retrieve
// a node's definition. see also JCR-2418
ItemData parentData = getItemData(parentId, null, false);
parentState = (NodeState) parentData.getState();
if (state.getParentId() == null) {
// that used to be the actual parent
if (parentState.getStatus() == ItemState.STATUS_NEW) {
// force getting parent from attic
parentState = null;
} else {
parentState = (NodeState) parentState.getOverlayedState();
}
}
} catch (ItemNotFoundException e) {
// parent probably removed, get it from attic. see below
}
if (parentState == null) {
try {
// use overlayed state if available
parentState = (NodeState) sism.getAttic().getItemState(parentId).getOverlayedState();
} catch (ItemStateException ex) {
throw new RepositoryException(ex);
}
}
// get child node entry
ChildNodeEntry cne = parentState.getChildNodeEntry(state.getNodeId());
if (cne == null) {
throw new InvalidItemStateException("Could not find child " + state.getNodeId() + " of node " + parentState.getNodeId());
}
NodeTypeRegistry ntReg = sessionContext.getNodeTypeRegistry();
try {
EffectiveNodeType ent = ntReg.getEffectiveNodeType(parentState.getNodeTypeName(), parentState.getMixinTypeNames());
QNodeDefinition def;
try {
def = ent.getApplicableChildNodeDef(cne.getName(), state.getNodeTypeName(), ntReg);
} catch (ConstraintViolationException e) {
// fallback to child node definition of a nt:unstructured
ent = ntReg.getEffectiveNodeType(NameConstants.NT_UNSTRUCTURED);
def = ent.getApplicableChildNodeDef(cne.getName(), state.getNodeTypeName(), ntReg);
log.warn("Fallback to nt:unstructured due to unknown child " + "node definition for type '" + state.getNodeTypeName() + "'");
}
return sessionContext.getNodeTypeManager().getNodeDefinition(def);
} catch (NodeTypeConflictException e) {
throw new RepositoryException(e);
}
}
use of org.apache.jackrabbit.core.state.ItemState in project jackrabbit by apache.
the class ItemManager method getItemData.
/**
* Retrieves the data of the item with given <code>id</code>. If the
* specified item doesn't exist an <code>ItemNotFoundException</code> will
* be thrown.
* If <code>permissionCheck</code> is <code>true</code> and the item exists
* but the current session is not granted read access an
* <code>AccessDeniedException</code> will be thrown.
*
* @param itemId id of item to be retrieved
* @param path The path of the item to retrieve the data for or
* <code>null</code>. In the latter case the id (instead of the path) is
* used to test if READ permission is granted.
* @param permissionCheck
* @return the ItemData for the item identified by the given itemId.
* @throws ItemNotFoundException if no item with given <code>id</code> exists
* @throws AccessDeniedException if the current session is not allowed to
* read the said item
* @throws RepositoryException if another error occurs
*/
ItemData getItemData(ItemId itemId, Path path, boolean permissionCheck) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
ItemData data = retrieveItem(itemId);
if (data == null) {
// not yet in cache, need to create instance:
// - retrieve item state
// - create instance of item data
// NOTE: permission check & caching within createItemData
ItemState state;
try {
state = sism.getItemState(itemId);
} catch (NoSuchItemStateException nsise) {
throw new ItemNotFoundException(itemId.toString(), nsise);
} catch (ItemStateException ise) {
String msg = "failed to retrieve item state of item " + itemId;
log.error(msg, ise);
throw new RepositoryException(msg, ise);
}
// create item data including: perm check and caching.
data = createItemData(state, path, permissionCheck);
} else {
// permission is granted.
if (permissionCheck && !canRead(data, path)) {
// item exists but read-perm has been revoked in the mean time.
// -> remove from cache
evictItems(itemId);
throw new AccessDeniedException("cannot read item " + data.getId());
}
}
return data;
}
use of org.apache.jackrabbit.core.state.ItemState in project jackrabbit by apache.
the class ItemManager method stateDiscarded.
/**
* {@inheritDoc}
*/
public void stateDiscarded(ItemState discarded) {
ItemData data = retrieveItem(discarded.getId());
if (data != null && data.getState() == discarded) {
if (discarded.isTransient()) {
switch(discarded.getStatus()) {
/**
* persistent item that has been transiently removed
*/
case ItemState.STATUS_EXISTING_REMOVED:
case ItemState.STATUS_EXISTING_MODIFIED:
ItemState persistentState = discarded.getOverlayedState();
// the state is a transient wrapper for the underlying
// persistent state, therefore restore the persistent state
// and resurrect this item instance if necessary
SessionItemStateManager stateMgr = sessionContext.getItemStateManager();
stateMgr.disconnectTransientItemState(discarded);
data.setState(persistentState);
return;
/**
* persistent item that has been transiently modified or
* removed and the underlying persistent state has been
* externally destroyed since the transient
* modification/removal.
*/
case ItemState.STATUS_STALE_DESTROYED:
/**
* first notify the listeners that this instance has been
* permanently invalidated
*/
itemDestroyed(discarded.getId(), data);
// now set state of this instance to 'destroyed'
data.setStatus(ItemImpl.STATUS_DESTROYED);
data.setState(null);
return;
/**
* new item that has been transiently added
*/
case ItemState.STATUS_NEW:
/**
* first notify the listeners that this instance has been
* permanently invalidated
*/
itemDestroyed(discarded.getId(), data);
// now set state of this instance to 'destroyed'
// finally dispose state
data.setStatus(ItemImpl.STATUS_DESTROYED);
data.setState(null);
return;
}
}
/**
* first notify the listeners that this instance has been
* invalidated
*/
itemInvalidated(discarded.getId(), data);
// now render this instance 'invalid'
data.setStatus(ItemImpl.STATUS_INVALIDATED);
}
}
Aggregations