use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class DefinitionValidator method validateNodeTypeDef.
/**
*
* @param ntDef
* @param validatedDefs Map of nodetype names and nodetype definitions
* that are known to be valid or are already registered. This map is used to
* validated dependencies and check for circular inheritance
* @return
* @throws InvalidNodeTypeDefinitionException
* @throws RepositoryException
*/
public EffectiveNodeType validateNodeTypeDef(QNodeTypeDefinition ntDef, Map<Name, QNodeTypeDefinition> validatedDefs) throws InvalidNodeTypeDefinitionException, RepositoryException {
/**
* the effective (i.e. merged and resolved) node type resulting from
* the specified node type definition;
* the effective node type will finally be created after the definition
* has been verified and checked for conflicts etc.; in some cases it
* will be created already at an earlier stage during the validation
* of child node definitions
*/
EffectiveNodeType ent = null;
Name name = ntDef.getName();
if (name == null) {
String msg = "no name specified";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
checkNamespace(name);
// validate supertypes
Name[] supertypes = ntDef.getSupertypes();
if (supertypes.length > 0) {
for (int i = 0; i < supertypes.length; i++) {
checkNamespace(supertypes[i]);
/**
* simple check for infinite recursion
* (won't trap recursion on a deeper inheritance level)
*/
if (name.equals(supertypes[i])) {
String msg = "[" + name + "] invalid supertype: " + supertypes[i] + " (infinite recursion))";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
/* compare to given nt-name set and not to registered nodetypes */
if (!validatedDefs.containsKey(supertypes[i])) {
String msg = "[" + name + "] invalid supertype: " + supertypes[i];
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
}
/**
* check for circularity in inheritance chain
* ('a' extends 'b' extends 'a')
*/
Stack<Name> inheritanceChain = new Stack<Name>();
inheritanceChain.push(name);
checkForCircularInheritance(supertypes, inheritanceChain, validatedDefs);
}
/**
* build effective (i.e. merged and resolved) node type from supertypes
* and check for conflicts
*/
if (supertypes.length > 0) {
try {
EffectiveNodeType est = entProvider.getEffectiveNodeType(supertypes, validatedDefs);
// make sure that all primary types except nt:base extend from nt:base
if (!ntDef.isMixin() && !NameConstants.NT_BASE.equals(ntDef.getName()) && !est.includesNodeType(NameConstants.NT_BASE)) {
String msg = "[" + name + "] all primary node types except" + " nt:base itself must be (directly or indirectly) derived from nt:base";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
} catch (ConstraintViolationException e) {
String msg = "[" + name + "] failed to validate supertypes";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
} catch (NoSuchNodeTypeException e) {
String msg = "[" + name + "] failed to validate supertypes";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
}
} else {
// no supertypes specified: has to be either a mixin type or nt:base
if (!ntDef.isMixin() && !NameConstants.NT_BASE.equals(ntDef.getName())) {
String msg = "[" + name + "] all primary node types except nt:base itself must be (directly or indirectly) derived from nt:base";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
}
checkNamespace(ntDef.getPrimaryItemName());
// validate property definitions
QPropertyDefinition[] pda = ntDef.getPropertyDefs();
for (int i = 0; i < pda.length; i++) {
QPropertyDefinition pd = pda[i];
/**
* sanity check:
* make sure declaring node type matches name of node type definition
*/
if (!name.equals(pd.getDeclaringNodeType())) {
String msg = "[" + name + "#" + pd.getName() + "] invalid declaring node type specified";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
checkNamespace(pd.getName());
// check that auto-created properties specify a name
if (pd.definesResidual() && pd.isAutoCreated()) {
String msg = "[" + name + "#" + pd.getName() + "] auto-created properties must specify a name";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
// check that auto-created properties specify a type
if (pd.getRequiredType() == PropertyType.UNDEFINED && pd.isAutoCreated()) {
String msg = "[" + name + "#" + pd.getName() + "] auto-created properties must specify a type";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
/* check default values:
* make sure type of value is consistent with required property type
* Note: default internal values are built from the required type,
* thus check for match with pd.getRequiredType is redundant.
*/
QValue[] defVals = pd.getDefaultValues();
/* check that default values satisfy value constraints.
* Note however, that no check is performed if autocreated property-
* definitions define a default value. JSR170 does not require this.
*/
ValueConstraint.checkValueConstraints(pd, defVals);
/* ReferenceConstraint:
* the specified node type must be registered, with one notable
* exception: the node type just being registered
*/
QValueConstraint[] constraints = pd.getValueConstraints();
if (constraints != null && constraints.length > 0) {
if (pd.getRequiredType() == PropertyType.REFERENCE) {
for (QValueConstraint constraint : constraints) {
// TODO improve. don't rely on a specific factory impl
Name ntName = NameFactoryImpl.getInstance().create(constraint.getString());
/* compare to given ntd map and not registered nts only */
if (!name.equals(ntName) && !validatedDefs.containsKey(ntName)) {
String msg = "[" + name + "#" + pd.getName() + "] invalid REFERENCE value constraint '" + ntName + "' (unknown node type)";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
}
}
}
}
// validate child-node definitions
QNodeDefinition[] cnda = ntDef.getChildNodeDefs();
for (int i = 0; i < cnda.length; i++) {
QNodeDefinition cnd = cnda[i];
/* make sure declaring node type matches name of node type definition */
if (!name.equals(cnd.getDeclaringNodeType())) {
String msg = "[" + name + "#" + cnd.getName() + "] invalid declaring node type specified";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
checkNamespace(cnd.getName());
// check that auto-created child-nodes specify a name
if (cnd.definesResidual() && cnd.isAutoCreated()) {
String msg = "[" + name + "#" + cnd.getName() + "] auto-created child-nodes must specify a name";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
// check that auto-created child-nodes specify a default primary type
if (cnd.getDefaultPrimaryType() == null && cnd.isAutoCreated()) {
String msg = "[" + name + "#" + cnd.getName() + "] auto-created child-nodes must specify a default primary type";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
// check default primary type
Name dpt = cnd.getDefaultPrimaryType();
checkNamespace(dpt);
boolean referenceToSelf = false;
EffectiveNodeType defaultENT = null;
if (dpt != null) {
// check if this node type specifies itself as default primary type
if (name.equals(dpt)) {
referenceToSelf = true;
}
/**
* the default primary type must be registered, with one notable
* exception: the node type just being registered
*/
if (!name.equals(dpt) && !validatedDefs.containsKey(dpt)) {
String msg = "[" + name + "#" + cnd.getName() + "] invalid default primary type '" + dpt + "'";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
/**
* build effective (i.e. merged and resolved) node type from
* default primary type and check for conflicts
*/
try {
if (!referenceToSelf) {
defaultENT = entProvider.getEffectiveNodeType(new Name[] { dpt }, validatedDefs);
} else {
/**
* the default primary type is identical with the node
* type just being registered; we have to instantiate it
* 'manually'
*/
ent = entProvider.getEffectiveNodeType(ntDef, validatedDefs);
defaultENT = ent;
}
if (cnd.isAutoCreated()) {
/**
* check for circularity through default primary types
* of auto-created child nodes (node type 'a' defines
* auto-created child node with default primary type 'a')
*/
Stack<Name> definingNTs = new Stack<Name>();
definingNTs.push(name);
checkForCircularNodeAutoCreation(defaultENT, definingNTs, validatedDefs);
}
} catch (ConstraintViolationException e) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate default primary type";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
} catch (NoSuchNodeTypeException e) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate default primary type";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
}
}
// check required primary types
Name[] reqTypes = cnd.getRequiredPrimaryTypes();
if (reqTypes != null && reqTypes.length > 0) {
for (int n = 0; n < reqTypes.length; n++) {
Name rpt = reqTypes[n];
checkNamespace(rpt);
referenceToSelf = false;
/**
* check if this node type specifies itself as required
* primary type
*/
if (name.equals(rpt)) {
referenceToSelf = true;
}
/**
* the required primary type must be registered, with one
* notable exception: the node type just being registered
*/
if (!name.equals(rpt) && !validatedDefs.containsKey(rpt)) {
String msg = "[" + name + "#" + cnd.getName() + "] invalid required primary type: " + rpt;
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
/**
* check if default primary type satisfies the required
* primary type constraint
*/
if (defaultENT != null && !defaultENT.includesNodeType(rpt)) {
String msg = "[" + name + "#" + cnd.getName() + "] default primary type does not satisfy required primary type constraint " + rpt;
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg);
}
/**
* build effective (i.e. merged and resolved) node type from
* required primary type constraint and check for conflicts
*/
try {
if (!referenceToSelf) {
entProvider.getEffectiveNodeType(new Name[] { rpt }, validatedDefs);
} else {
/**
* the required primary type is identical with the
* node type just being registered; we have to
* instantiate it 'manually'
*/
if (ent == null) {
ent = entProvider.getEffectiveNodeType(ntDef, validatedDefs);
}
}
} catch (ConstraintViolationException e) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate required primary type constraint";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
} catch (NoSuchNodeTypeException e) {
String msg = "[" + name + "#" + cnd.getName() + "] failed to validate required primary type constraint";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
}
}
}
}
/**
* now build effective (i.e. merged and resolved) node type from
* this node type definition; this will potentially detect more
* conflicts or problems
*/
if (ent == null) {
try {
ent = entProvider.getEffectiveNodeType(ntDef, validatedDefs);
} catch (ConstraintViolationException e) {
String msg = "[" + name + "] failed to resolve node type definition";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
} catch (NoSuchNodeTypeException e) {
String msg = "[" + name + "] failed to resolve node type definition";
log.debug(msg);
throw new InvalidNodeTypeDefinitionException(msg, e);
}
}
return ent;
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class EffectiveNodeTypeImpl method internalMerge.
/**
* Internal helper method which merges another <code>EffectiveNodeType</code>
* instance with <i>this</i> instance.
* <p>
* Warning: This instance might be in an inconsistent state if an exception
* is thrown.
*
* @param other
* @param supertype true if the merge is a result of inheritance, i.e. <code>other</code>
* represents one or more supertypes of this instance; otherwise false, i.e.
* the merge is the result of an explicit aggregation
* @throws ConstraintViolationException
*/
synchronized void internalMerge(EffectiveNodeTypeImpl other, boolean supertype) throws ConstraintViolationException {
Name[] nta = other.getAllNodeTypes();
int includedCount = 0;
for (int i = 0; i < nta.length; i++) {
if (includesNodeType(nta[i])) {
// redundant node type
log.debug("node type '" + nta[i] + "' is already contained.");
includedCount++;
}
}
if (includedCount == nta.length) {
// total overlap, ignore
return;
}
// named item definitions
QItemDefinition[] defs = other.getNamedItemDefs();
for (int i = 0; i < defs.length; i++) {
QItemDefinition qDef = defs[i];
if (includesNodeType(qDef.getDeclaringNodeType())) {
// ignore redundant definitions
continue;
}
Name name = qDef.getName();
List<QItemDefinition> existingDefs = namedItemDefs.get(name);
if (existingDefs != null) {
if (existingDefs.size() > 0) {
// there already exists at least one definition with that name
for (int j = 0; j < existingDefs.size(); j++) {
QItemDefinition qItemDef = existingDefs.get(j);
// make sure none of them is auto-create
if (qDef.isAutoCreated() || qItemDef.isAutoCreated()) {
// conflict
String msg = "The item definition for '" + name + "' in node type '" + qDef.getDeclaringNodeType() + "' conflicts with the one of node type '" + qItemDef.getDeclaringNodeType() + "': name collision with auto-create definition";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
// check ambiguous definitions
if (qDef.definesNode() == qItemDef.definesNode()) {
if (!qDef.definesNode()) {
// property definition
QPropertyDefinition pd = (QPropertyDefinition) qDef;
QPropertyDefinition epd = (QPropertyDefinition) qItemDef;
// compare type & multiValued flag
if (pd.getRequiredType() == epd.getRequiredType() && pd.isMultiple() == epd.isMultiple()) {
// conflict
String msg = "The property definition for '" + name + "' in node type '" + qDef.getDeclaringNodeType() + "' conflicts with the one of node type '" + qItemDef.getDeclaringNodeType() + "': ambiguous property definition. " + "they must differ in required type " + "or cardinality.";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
} else {
// child node definition
// conflict
String msg = "The child node definition for '" + name + "' in node type '" + qDef.getDeclaringNodeType() + "' conflicts with the one of node type '" + qItemDef.getDeclaringNodeType() + "': ambiguous child node definition. name must differ.";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
}
}
} else {
existingDefs = new ArrayList<QItemDefinition>();
namedItemDefs.put(name, existingDefs);
}
existingDefs.add(qDef);
}
// residual item definitions
defs = other.getUnnamedItemDefs();
for (int i = 0; i < defs.length; i++) {
QItemDefinition qDef = defs[i];
if (includesNodeType(qDef.getDeclaringNodeType())) {
// ignore redundant definitions
continue;
}
for (QItemDefinition existing : unnamedItemDefs) {
// compare with existing definition
if (qDef.definesNode() == existing.definesNode()) {
if (!qDef.definesNode()) {
// property definition
QPropertyDefinition pd = (QPropertyDefinition) qDef;
QPropertyDefinition epd = (QPropertyDefinition) existing;
// compare type & multiValued flag
if (pd.getRequiredType() == epd.getRequiredType() && pd.isMultiple() == epd.isMultiple() && pd.getOnParentVersion() == epd.getOnParentVersion()) {
// conflict
// TODO: need to take more aspects into account
// TODO: getMatchingPropDef needs to check this as well
String msg = "A property definition in node type '" + qDef.getDeclaringNodeType() + "' conflicts with node type '" + existing.getDeclaringNodeType() + "': ambiguous residual property definition";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
} else {
// child node definition
QNodeDefinition nd = (QNodeDefinition) qDef;
QNodeDefinition end = (QNodeDefinition) existing;
// compare required & default primary types
if (Arrays.equals(nd.getRequiredPrimaryTypes(), end.getRequiredPrimaryTypes()) && (nd.getDefaultPrimaryType() == null ? end.getDefaultPrimaryType() == null : nd.getDefaultPrimaryType().equals(end.getDefaultPrimaryType()))) {
// conflict
String msg = "A child node definition in node type '" + qDef.getDeclaringNodeType() + "' conflicts with node type '" + existing.getDeclaringNodeType() + "': ambiguous residual child node definition";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
}
}
unnamedItemDefs.add(qDef);
}
for (int i = 0; i < nta.length; i++) {
allNodeTypes.add(nta[i]);
}
if (supertype) {
// implicit merge as result of inheritance
// add other merged node types as supertypes
nta = other.getMergedNodeTypes();
for (int i = 0; i < nta.length; i++) {
inheritedNodeTypes.add(nta[i]);
}
// add supertypes of other merged node types as supertypes
nta = other.getInheritedNodeTypes();
for (int i = 0; i < nta.length; i++) {
inheritedNodeTypes.add(nta[i]);
}
} else {
// explicit merge
// merge with other merged node types
nta = other.getMergedNodeTypes();
for (int i = 0; i < nta.length; i++) {
mergedNodeTypes.add(nta[i]);
}
// add supertypes of other merged node types as supertypes
nta = other.getInheritedNodeTypes();
for (int i = 0; i < nta.length; i++) {
inheritedNodeTypes.add(nta[i]);
}
}
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class ItemDefinitionProviderImpl method getQPropertyDefinition.
/**
*
* @param ent
* @param name
* @param type
* @return
* @throws ConstraintViolationException
*/
private static QPropertyDefinition getQPropertyDefinition(EffectiveNodeType ent, Name name, int type) throws ConstraintViolationException {
// try named property definitions first
QPropertyDefinition[] defs = ent.getNamedQPropertyDefinitions(name);
QPropertyDefinition match = getMatchingPropDef(defs, type);
if (match != null) {
return match;
}
// no item with that name defined;
// try residual property definitions
defs = ent.getUnnamedQPropertyDefinitions();
match = getMatchingPropDef(defs, type);
if (match != null) {
return match;
}
// no applicable definition found
throw new ConstraintViolationException("no matching property definition found for " + name);
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class ItemDefinitionProviderImpl method getQPropertyDefinition.
public QPropertyDefinition getQPropertyDefinition(Name[] parentNodeTypeNames, Name propertyName, int propertyType, boolean isMultiValued, PropertyId propertyId) throws RepositoryException {
QPropertyDefinition definition;
try {
EffectiveNodeType ent = entProvider.getEffectiveNodeType(parentNodeTypeNames);
definition = getQPropertyDefinition(ent, propertyName, propertyType, isMultiValued, true);
} catch (RepositoryException e) {
log.debug("Cannot determine property definition of {}: {}", propertyId, e);
definition = getPropertyDefinition(service, sessionInfo, propertyId);
}
return definition;
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class ItemDefinitionProviderImpl method getQPropertyDefinition.
/**
*
* @param ent
* @param name
* @param type
* @param multiValued
* @return
* @throws ConstraintViolationException
*/
private static QPropertyDefinition getQPropertyDefinition(EffectiveNodeType ent, Name name, int type, boolean multiValued, boolean throwWhenAmbiguous) throws ConstraintViolationException {
// try named property definitions first
QPropertyDefinition[] defs = ent.getNamedQPropertyDefinitions(name);
QPropertyDefinition match = getMatchingPropDef(defs, type, multiValued, throwWhenAmbiguous);
if (match != null) {
return match;
}
// no item with that name defined;
// try residual property definitions
defs = ent.getUnnamedQPropertyDefinitions();
match = getMatchingPropDef(defs, type, multiValued, throwWhenAmbiguous);
if (match != null) {
return match;
}
// no applicable definition found
throw new ConstraintViolationException("no matching property definition found for " + name);
}
Aggregations