Search in sources :

Example 6 with NodeTypeRegistry

use of org.apache.jackrabbit.core.nodetype.NodeTypeRegistry in project jackrabbit by apache.

the class BatchedItemOperations method isShareable.

/**
     * Determines whether the specified node is <i>shareable</i>, i.e.
     * whether the mixin type <code>mix:shareable</code> is either
     * directly assigned or indirectly inherited.
     *
     * @param state node state to check
     * @return true if the specified node is <i>shareable</i>, false otherwise.
     * @throws RepositoryException if an error occurs
     */
private boolean isShareable(NodeState state) throws RepositoryException {
    // shortcut: check some wellknown built-in types first
    Name primary = state.getNodeTypeName();
    Set<Name> mixins = state.getMixinTypeNames();
    if (mixins.contains(NameConstants.MIX_SHAREABLE)) {
        return true;
    }
    try {
        NodeTypeRegistry registry = context.getNodeTypeRegistry();
        EffectiveNodeType type = registry.getEffectiveNodeType(primary, mixins);
        return type.includesNodeType(NameConstants.MIX_REFERENCEABLE);
    } catch (NodeTypeConflictException ntce) {
        String msg = "internal error: failed to build effective node type for node " + state.getNodeId();
        log.debug(msg);
        throw new RepositoryException(msg, ntce);
    }
}
Also used : EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Example 7 with NodeTypeRegistry

use of org.apache.jackrabbit.core.nodetype.NodeTypeRegistry in project jackrabbit by apache.

the class AddMixinOperation method perform.

public Object perform(SessionContext context) throws RepositoryException {
    int permissions = Permission.NODE_TYPE_MNGMT;
    // is required in addition.
    if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
        permissions |= Permission.VERSION_MNGMT;
    }
    context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
    NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
    NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
    if (!mixin.isMixin()) {
        throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
    }
    Name primaryTypeName = node.getNodeState().getNodeTypeName();
    NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
    if (primaryType.isDerivedFrom(mixinName)) {
        // new mixin is already included in primary type
        return this;
    }
    // build effective node type of mixin's & primary type in order
    // to detect conflicts
    NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
    EffectiveNodeType entExisting;
    try {
        // existing mixin's
        Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
        // build effective node type representing primary type including
        // existing mixin's
        entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
        if (entExisting.includesNodeType(mixinName)) {
            // new mixin is already included in existing mixin type(s)
            return this;
        }
        // add new mixin
        mixins.add(mixinName);
        // try to build new effective node type (will throw in case
        // of conflicts)
        ntReg.getEffectiveNodeType(primaryTypeName, mixins);
    } catch (NodeTypeConflictException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
    // try to revert the changes in case an exception occurs
    try {
        // modify the state of this node
        NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
        // add mixin name
        Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
        mixins.add(mixinName);
        thisState.setMixinTypeNames(mixins);
        // set jcr:mixinTypes property
        node.setMixinTypesProperty(mixins);
        // add 'auto-create' properties defined in mixin type
        for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
            PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
            // make sure that the property is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
            }
        }
        // recursively add 'auto-create' child nodes defined in mixin type
        for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
            NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
            // make sure that the child node is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
            }
        }
    } catch (RepositoryException re) {
        // try to undo the modifications by removing the mixin
        try {
            node.removeMixin(mixinName);
        } catch (RepositoryException re1) {
        // silently ignore & fall through
        }
        throw re;
    }
    return this;
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) HashSet(java.util.HashSet)

Example 8 with NodeTypeRegistry

use of org.apache.jackrabbit.core.nodetype.NodeTypeRegistry 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);
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) InvalidItemStateException(javax.jcr.InvalidItemStateException) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) RepositoryException(javax.jcr.RepositoryException) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ItemState(org.apache.jackrabbit.core.state.ItemState) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 9 with NodeTypeRegistry

use of org.apache.jackrabbit.core.nodetype.NodeTypeRegistry in project jackrabbit by apache.

the class ItemManager method getDefinition.

PropertyDefinitionImpl getDefinition(PropertyState state) throws RepositoryException {
    // see also: JCR-2408
    if (state.getStatus() == ItemState.STATUS_EXISTING_REMOVED && state.getName().equals(NameConstants.JCR_UUID)) {
        NodeTypeRegistry ntReg = sessionContext.getNodeTypeRegistry();
        QPropertyDefinition def = ntReg.getEffectiveNodeType(NameConstants.MIX_REFERENCEABLE).getApplicablePropertyDef(state.getName(), state.getType());
        return sessionContext.getNodeTypeManager().getPropertyDefinition(def);
    }
    try {
        // retrieve parent in 2 steps in order to avoid the check for
        // read permissions on the parent which isn't required in order
        // to read the property's definition. see also JCR-2418.
        ItemData parentData = getItemData(state.getParentId(), null, false);
        NodeImpl parent = (NodeImpl) createItemInstance(parentData);
        return parent.getApplicablePropertyDefinition(state.getName(), state.getType(), state.isMultiValued(), true);
    } catch (ItemNotFoundException e) {
    // parent probably removed, get it from attic
    }
    try {
        NodeState parent = (NodeState) sism.getAttic().getItemState(state.getParentId()).getOverlayedState();
        NodeTypeRegistry ntReg = sessionContext.getNodeTypeRegistry();
        EffectiveNodeType ent = ntReg.getEffectiveNodeType(parent.getNodeTypeName(), parent.getMixinTypeNames());
        QPropertyDefinition def;
        try {
            def = ent.getApplicablePropertyDef(state.getName(), state.getType(), state.isMultiValued());
        } catch (ConstraintViolationException e) {
            ent = ntReg.getEffectiveNodeType(NameConstants.NT_UNSTRUCTURED);
            def = ent.getApplicablePropertyDef(state.getName(), state.getType(), state.isMultiValued());
            log.warn("Fallback to nt:unstructured due to unknown property " + "definition for '" + state.getName() + "'");
        }
        return sessionContext.getNodeTypeManager().getPropertyDefinition(def);
    } catch (ItemStateException e) {
        throw new RepositoryException(e);
    } catch (NodeTypeConflictException e) {
        throw new RepositoryException(e);
    }
}
Also used : EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeState(org.apache.jackrabbit.core.state.NodeState) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 10 with NodeTypeRegistry

use of org.apache.jackrabbit.core.nodetype.NodeTypeRegistry in project jackrabbit by apache.

the class NodeImpl method isNodeType.

/**
     * Same as {@link Node#isNodeType(String)} except that it takes a
     * <code>Name</code> instead of a <code>String</code>.
     *
     * @param ntName name of node type
     * @return <code>true</code> if this node is of the specified node type;
     *         otherwise <code>false</code>
     */
public boolean isNodeType(Name ntName) throws RepositoryException {
    // first do trivial checks without using type hierarchy
    Name primary = data.getNodeState().getNodeTypeName();
    if (ntName.equals(primary)) {
        return true;
    }
    Set<Name> mixins = data.getNodeState().getMixinTypeNames();
    if (mixins.contains(ntName)) {
        return true;
    }
    // check effective node type
    try {
        NodeTypeRegistry registry = sessionContext.getNodeTypeRegistry();
        EffectiveNodeType type = registry.getEffectiveNodeType(primary, mixins);
        return type.includesNodeType(ntName);
    } catch (NodeTypeConflictException e) {
        String msg = "Failed to build effective node type for " + this;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
}
Also used : EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Aggregations

NodeTypeRegistry (org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)10 Name (org.apache.jackrabbit.spi.Name)10 RepositoryException (javax.jcr.RepositoryException)9 NodeTypeConflictException (org.apache.jackrabbit.core.nodetype.NodeTypeConflictException)9 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)7 NodeTypeManagerImpl (org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl)6 NodeState (org.apache.jackrabbit.core.state.NodeState)6 HashSet (java.util.HashSet)5 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)5 InvalidItemStateException (javax.jcr.InvalidItemStateException)4 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)4 NodeDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl)4 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)4 ArrayList (java.util.ArrayList)3 Value (javax.jcr.Value)3 ValueFormatException (javax.jcr.ValueFormatException)3 PropertyId (org.apache.jackrabbit.core.id.PropertyId)3 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)3 PropertyState (org.apache.jackrabbit.core.state.PropertyState)3