Search in sources :

Example 1 with LdapComparator

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;
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) MatchingRuleUse(org.apache.directory.api.ldap.model.schema.MatchingRuleUse) LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) NameForm(org.apache.directory.api.ldap.model.schema.NameForm) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) DitStructureRule(org.apache.directory.api.ldap.model.schema.DitStructureRule) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) DitContentRule(org.apache.directory.api.ldap.model.schema.DitContentRule) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule)

Example 2 with LdapComparator

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;
        }
    }
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) Value(org.apache.directory.api.ldap.model.entry.Value) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 3 with LdapComparator

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);
        }
    }
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule)

Example 4 with LdapComparator

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;
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

LdapComparator (org.apache.directory.api.ldap.model.schema.LdapComparator)4 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)3 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Value (org.apache.directory.api.ldap.model.entry.Value)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)1 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)1 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)1 LdapUnwillingToPerformException (org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException)1 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)1 DitContentRule (org.apache.directory.api.ldap.model.schema.DitContentRule)1 DitStructureRule (org.apache.directory.api.ldap.model.schema.DitStructureRule)1 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)1 LoadableSchemaObject (org.apache.directory.api.ldap.model.schema.LoadableSchemaObject)1 MatchingRuleUse (org.apache.directory.api.ldap.model.schema.MatchingRuleUse)1 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)1 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)1 NameForm (org.apache.directory.api.ldap.model.schema.NameForm)1 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)1