use of org.apache.directory.api.ldap.model.schema.LdapComparator in project directory-ldap-api by apache.
the class Registries method unregister.
/**
* Unregister a SchemaObject from the registries
*
* @param schemaObject The SchemaObject we want to deregister
* @throws LdapException If the removal failed
*/
private SchemaObject unregister(List<Throwable> errors, SchemaObject schemaObject) throws LdapException {
LOG.debug("Unregistering {}:{}", schemaObject.getObjectType(), schemaObject.getOid());
// Check that the SchemaObject is present in the registries
if (!(schemaObject instanceof LoadableSchemaObject) && !globalOidRegistry.contains(schemaObject.getOid())) {
String msg = I18n.err(I18n.ERR_13751_UNREGISTERING_FAILED_NOT_PRESENT, schemaObject.getObjectType(), schemaObject.getOid());
LOG.error(msg);
throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg);
}
SchemaObject unregistered;
// First call the specific registry's register method
switch(schemaObject.getObjectType()) {
case ATTRIBUTE_TYPE:
unregistered = attributeTypeRegistry.unregister((AttributeType) schemaObject);
break;
case COMPARATOR:
unregistered = comparatorRegistry.unregister((LdapComparator<?>) schemaObject);
break;
case DIT_CONTENT_RULE:
unregistered = ditContentRuleRegistry.unregister((DitContentRule) schemaObject);
break;
case DIT_STRUCTURE_RULE:
unregistered = ditStructureRuleRegistry.unregister((DitStructureRule) schemaObject);
break;
case LDAP_SYNTAX:
unregistered = ldapSyntaxRegistry.unregister((LdapSyntax) schemaObject);
break;
case MATCHING_RULE:
unregistered = matchingRuleRegistry.unregister((MatchingRule) schemaObject);
break;
case MATCHING_RULE_USE:
unregistered = matchingRuleUseRegistry.unregister((MatchingRuleUse) schemaObject);
break;
case NAME_FORM:
unregistered = nameFormRegistry.unregister((NameForm) schemaObject);
break;
case NORMALIZER:
unregistered = normalizerRegistry.unregister((Normalizer) schemaObject);
break;
case OBJECT_CLASS:
unregistered = objectClassRegistry.unregister((ObjectClass) schemaObject);
break;
case SYNTAX_CHECKER:
unregistered = syntaxCheckerRegistry.unregister((SyntaxChecker) schemaObject);
break;
default:
throw new IllegalArgumentException(I18n.err(I18n.ERR_13718_UNEXPECTED_SCHEMA_OBJECT_TYPE, schemaObject.getObjectType()));
}
return unregistered;
}
use of org.apache.directory.api.ldap.model.schema.LdapComparator in project directory-ldap-api by apache.
the class Ava method compareTo.
/**
* @see Comparable#compareTo(Object)
*/
@Override
public int compareTo(Ava that) {
if (that == null) {
return 1;
}
int comp;
if (schemaManager == null) {
// Compare the ATs
comp = normType.compareTo(that.normType);
if (comp != 0) {
return comp;
}
// and compare the values
if (value == null) {
if (that.value == null) {
return 0;
} else {
return -1;
}
} else {
if (that.value == null) {
return 1;
} else {
comp = value.compareTo((Value) that.value);
return comp;
}
}
} else {
if (that.schemaManager == null) {
// Problem : we will apply the current Ava SchemaManager to the given Ava
try {
that.apply(schemaManager);
} catch (LdapInvalidDnException lide) {
return 1;
}
}
// First compare the AT OID
comp = attributeType.getOid().compareTo(that.attributeType.getOid());
if (comp != 0) {
return comp;
}
// Now, compare the two values using the ordering matchingRule comparator, if any
MatchingRule orderingMR = attributeType.getOrdering();
if (orderingMR != null) {
LdapComparator<Object> comparator = (LdapComparator<Object>) orderingMR.getLdapComparator();
if (comparator != null) {
comp = value.compareTo(that.value);
return comp;
} else {
comp = compareValues(that);
return comp;
}
} else {
comp = compareValues(that);
return comp;
}
}
}
use of org.apache.directory.api.ldap.model.schema.LdapComparator in project directory-ldap-api by apache.
the class Value method equals.
/**
* We compare two values using their Comparator, if any.
*
* @see Object#equals(Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof String) {
String other = (String) obj;
if (!isHR) {
return false;
}
if (attributeType == null) {
if (upValue != null) {
return upValue.equals(other);
} else {
return obj == null;
}
} else {
// We have an AttributeType, we use the associated comparator
try {
LdapComparator<String> comparator = (LdapComparator<String>) getLdapComparator();
Normalizer normalizer = null;
if (attributeType.getEquality() != null) {
normalizer = attributeType.getEquality().getNormalizer();
}
if (normalizer == null) {
if (comparator == null) {
return normValue.equals(other);
} else {
return comparator.compare(normValue, other) == 0;
}
}
String thisNormValue = normValue;
String otherNormValue = normalizer.normalize(other);
// Compare normalized values
if (comparator == null) {
return thisNormValue.equals(otherNormValue);
} else {
return comparator.compare(thisNormValue, otherNormValue) == 0;
}
} catch (LdapException ne) {
return false;
}
}
}
if (!(obj instanceof Value)) {
return false;
}
Value other = (Value) obj;
// Check if the values aren't of the same type
if (isHR != other.isHR) {
// Both values must be HR or not HR
return false;
}
if (!isHR) {
// Shortcut for binary values
return Arrays.equals(bytes, other.bytes);
}
// HR values
if (bytes == null) {
return other.bytes == null;
}
// Special case
if (other.bytes == null) {
return false;
}
// Not null, but empty. We try to avoid a spurious String Preparation
if (bytes.length == 0) {
return other.bytes.length == 0;
} else if (other.bytes.length == 0) {
return false;
}
// Ok, now, let's see if we have an AttributeType at all. If both have one,
// and if they aren't equal, then we get out. If one of them has an AttributeType and
// not the other, we will assume that this is the AttributeType to use.
MatchingRule equalityMR;
if (attributeType == null) {
if (other.attributeType != null) {
// Use the Other value AT
equalityMR = other.attributeType.getEquality();
// We may not have an Equality MR, and in tjis case, we compare the bytes
if (equalityMR == null) {
return Arrays.equals(bytes, other.bytes);
}
LdapComparator<Object> ldapComparator = equalityMR.getLdapComparator();
if (ldapComparator == null) {
// This is an error !
LOG.error(I18n.err(I18n.ERR_13249_NO_COMPARATOR_FOR_AT, other.attributeType));
return false;
}
return ldapComparator.compare(normValue, other.normValue) == 0;
} else {
// or the bytes otherwise.
if (upValue != null) {
return upValue.equals(other.upValue);
} else {
return Arrays.equals(bytes, other.bytes);
}
}
} else {
if (other.attributeType != null) {
// Both attributeType must be equal
if (!attributeType.equals(other.attributeType)) {
return false;
}
// We have an AttributeType, we use the associated comparator
try {
LdapComparator<String> comparator = (LdapComparator<String>) getLdapComparator();
if (other.attributeType.getEquality() == null) {
// No equality ? Default to comparing using a String comparator
return stringComparator.compare(normValue, other.normValue) == 0;
}
Normalizer normalizer = other.attributeType.getEquality().getNormalizer();
if (normalizer == null) {
if (comparator == null) {
return normValue.equals(other.normValue);
} else {
return comparator.compare(normValue, other.normValue) == 0;
}
}
String thisNormValue = normalizer.normalize(normValue);
// Compare normalized values
if (comparator == null) {
return thisNormValue.equals(other.normValue);
} else {
return comparator.compare(thisNormValue, other.normValue) == 0;
}
} catch (LdapException ne) {
return false;
}
}
// No attributeType
if (normValue == null) {
return other.normValue == null;
} else {
return normValue.equals(other.normValue);
}
}
}
use of org.apache.directory.api.ldap.model.schema.LdapComparator in project directory-ldap-api by apache.
the class SchemaEntityFactory method classLoadComparator.
/**
* Class load a comparator instances
*/
private LdapComparator<?> classLoadComparator(SchemaManager schemaManager, String oid, String className, Attribute byteCode) throws LdapException {
// Try to class load the comparator
LdapComparator<?> comparator;
Class<?> clazz;
String byteCodeStr = StringConstants.EMPTY;
if (byteCode == null) {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException cnfe) {
LOG.error(I18n.err(I18n.ERR_16056_CANNOT_FIND_CMP_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16057_CANNOT_FIND_CMP_CLASS, cnfe.getMessage()));
}
} else {
classLoader.setAttribute(byteCode);
try {
clazz = classLoader.loadClass(className);
} catch (ClassNotFoundException cnfe) {
LOG.error(I18n.err(I18n.ERR_16058_CANNOT_LOAD_CMP_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16059_CANNOT_LOAD_CMP_CLASS, cnfe.getMessage()));
}
byteCodeStr = new String(Base64.encode(byteCode.getBytes()));
}
// or we have one which takes an OID. Lets try the one with an OID argument first
try {
Constructor<?> constructor = clazz.getConstructor(new Class[] { String.class });
try {
comparator = (LdapComparator<?>) constructor.newInstance(new Object[] { oid });
} catch (InvocationTargetException ite) {
LOG.error(I18n.err(I18n.ERR_16060_CANNOT_INVOKE_CMP_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16061_CANNOT_INVOKE_CMP_CLASS, ite.getMessage()));
} catch (InstantiationException ie) {
LOG.error(I18n.err(I18n.ERR_16062_CANNOT_INST_CMP_CTOR_CLASS, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16063_CANNOT_INST_CMP_CLASS, ie.getMessage()));
} catch (IllegalAccessException ie) {
LOG.error(I18n.err(I18n.ERR_16064_CANNOT_ACCESS_CMP_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16065_CANNOT_ACCESS_CMP_CLASS, ie.getMessage()));
}
} catch (NoSuchMethodException nsme) {
// the one we got in the Comparator entry
try {
clazz.getConstructor();
} catch (NoSuchMethodException nsme2) {
LOG.error(I18n.err(I18n.ERR_16066_CANNOT_FIND_CMP_CTOR_METH_CLASS, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16067_CANNOT_FIND_CMP_CTOR_METH, nsme2.getMessage()));
}
try {
comparator = (LdapComparator<?>) clazz.newInstance();
} catch (InstantiationException ie) {
LOG.error(I18n.err(I18n.ERR_16062_CANNOT_INST_CMP_CTOR_CLASS, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16063_CANNOT_INST_CMP_CLASS, ie.getMessage()));
} catch (IllegalAccessException iae) {
LOG.error(I18n.err(I18n.ERR_16064_CANNOT_ACCESS_CMP_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16065_CANNOT_ACCESS_CMP_CLASS, iae.getMessage()));
}
if (!comparator.getOid().equals(oid)) {
String msg = I18n.err(I18n.ERR_16021_DIFFERENT_COMPARATOR_OID, oid, comparator.getOid());
throw new LdapInvalidAttributeValueException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg, nsme);
}
}
// Update the loadable fields
comparator.setBytecode(byteCodeStr);
comparator.setFqcn(className);
// Inject the SchemaManager for the comparator who needs it
comparator.setSchemaManager(schemaManager);
return comparator;
}
Aggregations