Search in sources :

Example 51 with LdapSchemaException

use of org.apache.directory.api.ldap.model.exception.LdapSchemaException 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)

Example 52 with LdapSchemaException

use of org.apache.directory.api.ldap.model.exception.LdapSchemaException in project directory-ldap-api by apache.

the class DefaultSchemaManager method add.

// -----------------------------------------------------------------------------------
// SchemaObject operations
// -----------------------------------------------------------------------------------
/**
 * {@inheritDoc}
 */
@Override
public boolean add(SchemaObject schemaObject) throws LdapException {
    // First, clear the errors
    errors.clear();
    // Clone the schemaObject
    SchemaObject copy = copy(schemaObject);
    if (copy == null) {
        return false;
    }
    if (registries.isRelaxed()) {
        // Apply the addition right away
        registries.add(errors, copy, true);
        return errors.isEmpty();
    } else {
        // The new schemaObject's OID must not already exist
        if (checkOidExist(copy)) {
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, I18n.err(I18n.ERR_16036_OID_NOT_UNIQUE, schemaObject.getOid()));
            ldapSchemaException.setSourceObject(schemaObject);
            errors.add(ldapSchemaException);
            return false;
        }
        // Build the new AttributeType from the given entry
        String schemaName = getSchemaName(copy);
        if (schemaName == null) {
            // The schema associated with the SchemaaObject does not exist. This is not valid.
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.NONEXISTENT_SCHEMA, I18n.err(I18n.ERR_16037_NON_EXISTING_SCHEMA, schemaObject.getOid(), copy.getSchemaName()));
            ldapSchemaException.setSourceObject(schemaObject);
            ldapSchemaException.setRelatedId(copy.getSchemaName());
            errors.add(ldapSchemaException);
            return false;
        }
        // At this point, the constructed AttributeType has not been checked against the
        // existing Registries. It may be broken (missing SUP, or such), it will be checked
        // there, if the schema and the AttributeType are both enabled.
        Schema schema = getLoadedSchema(schemaName);
        if (schema == null) {
            // The SchemaObject must be associated with an existing schema
            String msg = I18n.err(I18n.ERR_16038_NOT_ASSOCIATED_TO_A_SCHEMA, copy.getOid());
            LOG.info(msg);
            Throwable error = new LdapProtocolErrorException(msg);
            errors.add(error);
            return false;
        }
        if (schema.isEnabled() && copy.isEnabled()) {
            // As we may break the registries, work on a cloned registries
            Registries clonedRegistries = null;
            try {
                clonedRegistries = registries.clone();
            } catch (CloneNotSupportedException cnse) {
                throw new LdapOtherException(cnse.getMessage(), cnse);
            }
            // Inject the new SchemaObject in the cloned registries
            clonedRegistries.add(errors, copy, true);
            // Remove the cloned registries
            clonedRegistries.clear();
            // If we didn't get any error, apply the addition to the real retistries
            if (errors.isEmpty()) {
                // Copy again as the clonedRegistries clear has removed the previous copy
                copy = copy(schemaObject);
                // Apply the addition to the real registries
                registries.add(errors, copy, true);
                LOG.debug(I18n.msg(I18n.MSG_16019_ENABLED_SCHEMA_ADDED, copy.getName(), schemaName));
                return true;
            } else {
                // We have some error : reject the addition and get out
                LOG.info(I18n.msg(I18n.MSG_16020_CANNOT_LOAD_SCHEMAOBJECT, copy.getOid(), Strings.listToString(errors)));
                return false;
            }
        } else {
            // At least, we register the OID in the globalOidRegistry, and associates it with the
            // schema
            registries.associateWithSchema(errors, copy);
            LOG.debug(I18n.msg(I18n.MSG_16021_ADDED_INTO_DISABLED_SCHEMA, copy.getName(), schemaName));
            return errors.isEmpty();
        }
    }
}
Also used : SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException)

Example 53 with LdapSchemaException

use of org.apache.directory.api.ldap.model.exception.LdapSchemaException in project directory-ldap-api by apache.

the class SchemaEntityFactory method classLoadNormalizer.

/**
 * Class load a normalizer instances
 */
private Normalizer classLoadNormalizer(SchemaManager schemaManager, String oid, String className, Attribute byteCode) throws LdapException {
    // Try to class load the normalizer
    Class<?> clazz;
    Normalizer normalizer;
    String byteCodeStr = StringConstants.EMPTY;
    if (byteCode == null) {
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException cnfe) {
            LOG.error(I18n.err(I18n.ERR_16068_CANNOT_FIND_NORM_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16069_CANNOT_FIND_NORM_CLASS, cnfe.getMessage()));
        }
    } else {
        classLoader.setAttribute(byteCode);
        try {
            clazz = classLoader.loadClass(className);
        } catch (ClassNotFoundException cnfe) {
            LOG.error(I18n.err(I18n.ERR_16070_CANNOT_LOAD_NORM_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16071_CANNOT_LOAD_NORM_CLASS, cnfe.getMessage()));
        }
        byteCodeStr = new String(Base64.encode(byteCode.getBytes()));
    }
    // Create the normalizer instance
    try {
        normalizer = (Normalizer) clazz.newInstance();
    } catch (InstantiationException ie) {
        LOG.error(I18n.err(I18n.ERR_16072_CANNOT_INST_NORM_CTOR_CLASS, className));
        throw new LdapSchemaException(I18n.err(I18n.ERR_16073_CANNOT_INST_NORM_CLASS, ie.getMessage()));
    } catch (IllegalAccessException iae) {
        LOG.error(I18n.err(I18n.ERR_16074_CANNOT_ACCESS_NORM_CTOR_CLASS, className));
        throw new LdapSchemaException(I18n.err(I18n.ERR_16075_CANNOT_ACCESS_NORM_CTOR, iae.getMessage()));
    }
    // Update the common fields
    normalizer.setBytecode(byteCodeStr);
    normalizer.setFqcn(className);
    // Inject the new OID, as the loaded normalizer might have its own
    normalizer.setOid(oid);
    // Inject the SchemaManager for the normalizer who needs it
    normalizer.setSchemaManager(schemaManager);
    return normalizer;
}
Also used : Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException)

Example 54 with LdapSchemaException

use of org.apache.directory.api.ldap.model.exception.LdapSchemaException in project directory-ldap-api by apache.

the class ObjectClassHelper method buildMust.

/**
 * Build and check the MUST AT for this ObjectClass.
 */
private static void buildMust(ObjectClass objectClass, List<Throwable> errors, Registries registries) {
    AttributeTypeRegistry atRegistry = registries.getAttributeTypeRegistry();
    List<String> mustAttributeTypeOids = objectClass.getMustAttributeTypeOids();
    if (mustAttributeTypeOids != null) {
        objectClass.getMustAttributeTypes().clear();
        for (String mustAttributeTypeName : mustAttributeTypeOids) {
            try {
                AttributeType attributeType = atRegistry.lookup(mustAttributeTypeName);
                if (attributeType.isCollective()) {
                    // Collective Attributes are not allowed in MAY or MUST
                    String msg = I18n.err(I18n.ERR_13778_COLLECTIVE_NOT_ALLOWED_IN_MUST, mustAttributeTypeName, objectClass.getOid());
                    LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OC_COLLECTIVE_NOT_ALLOWED_IN_MUST, msg);
                    ldapSchemaException.setSourceObject(objectClass);
                    ldapSchemaException.setRelatedId(mustAttributeTypeName);
                    errors.add(ldapSchemaException);
                    LOG.info(msg);
                    continue;
                }
                if (objectClass.getMustAttributeTypes().contains(attributeType)) {
                    // Already registered : this is an error
                    String msg = I18n.err(I18n.ERR_13772_CANNOT_REGISTER_DUPLICATE_AT_IN_MUST, objectClass.getOid(), mustAttributeTypeName);
                    LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OC_DUPLICATE_AT_IN_MUST, msg);
                    ldapSchemaException.setSourceObject(objectClass);
                    ldapSchemaException.setRelatedId(mustAttributeTypeName);
                    errors.add(ldapSchemaException);
                    LOG.info(msg);
                    continue;
                }
                // Check that the MUST AT is not also present in the MAY AT
                if (objectClass.getMayAttributeTypes().contains(attributeType)) {
                    // Already registered : this is an error
                    String msg = I18n.err(I18n.ERR_13773_CANNOT_REGISTER_DUPLICATE_AT_IN_MAY_AND_MUST, objectClass.getOid(), mustAttributeTypeName);
                    LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OC_DUPLICATE_AT_IN_MAY_AND_MUST, msg);
                    ldapSchemaException.setSourceObject(objectClass);
                    ldapSchemaException.setRelatedId(mustAttributeTypeName);
                    errors.add(ldapSchemaException);
                    LOG.info(msg);
                    continue;
                }
                objectClass.getMustAttributeTypes().add(attributeType);
            } catch (LdapException ne) {
                // Cannot find the AT
                String msg = I18n.err(I18n.ERR_13774_CANNOT_REGISTER_AT_IN_MUST_DOES_NOT_EXIST, objectClass.getOid(), mustAttributeTypeName);
                LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OC_NONEXISTENT_MUST_AT, msg, ne);
                ldapSchemaException.setSourceObject(objectClass);
                ldapSchemaException.setRelatedId(mustAttributeTypeName);
                errors.add(ldapSchemaException);
                LOG.info(msg);
                continue;
            }
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) AttributeTypeRegistry(org.apache.directory.api.ldap.model.schema.registries.AttributeTypeRegistry)

Aggregations

LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)54 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)34 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)34 Test (org.junit.Test)34 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)19 MutableObjectClass (org.apache.directory.api.ldap.model.schema.MutableObjectClass)16 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)11 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)10 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)5 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)5 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)5 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)3 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)3 AttributeTypeRegistry (org.apache.directory.api.ldap.model.schema.registries.AttributeTypeRegistry)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2 Method (java.lang.reflect.Method)1 DecoderException (org.apache.directory.api.asn1.DecoderException)1 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)1 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)1