Search in sources :

Example 6 with Registries

use of org.apache.directory.api.ldap.model.schema.registries.Registries in project directory-ldap-api by apache.

the class DefaultSchemaManager method loadDisabled.

/**
 * {@inheritDoc}
 */
@Override
public boolean loadDisabled(Schema... schemas) throws LdapException {
    // Work on a cloned and relaxed registries
    Registries clonedRegistries = cloneRegistries();
    // Accept the disabled schemas
    clonedRegistries.setDisabledAccepted(true);
    // Load the schemas
    for (Schema schema : schemas) {
        // Enable the Schema object before loading it
        schema.enable();
        load(clonedRegistries, schema);
    }
    clonedRegistries.clear();
    // Apply the change to the correct registries if no errors
    if (errors.isEmpty()) {
        // No error, we can enable the schema in the real registries
        for (Schema schema : schemas) {
            load(registries, schema);
        }
        return true;
    } else {
        for (Schema schema : schemas) {
            schema.disable();
        }
        return false;
    }
}
Also used : Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries)

Example 7 with Registries

use of org.apache.directory.api.ldap.model.schema.registries.Registries in project directory-ldap-api by apache.

the class DefaultSchemaManager method delete.

/**
 * {@inheritDoc}
 */
@Override
public boolean delete(SchemaObject schemaObject) throws LdapException {
    // First, clear the errors
    errors.clear();
    if (registries.isRelaxed()) {
        // Apply the addition right away
        registries.delete(errors, schemaObject);
        return errors.isEmpty();
    } else {
        // The new schemaObject's OID must exist
        if (!checkOidExist(schemaObject)) {
            Throwable error = new LdapProtocolErrorException(I18n.err(I18n.ERR_16039_OID_DOES_NOT_EXIST, schemaObject.getOid()));
            errors.add(error);
            return false;
        }
        // Get the SchemaObject to delete if it's not a LoadableSchemaObject
        SchemaObject toDelete = getSchemaObject(schemaObject);
        // First check that this SchemaObject does not have any referencing SchemaObjects
        Set<SchemaObjectWrapper> referencing = registries.getReferencing(toDelete);
        if ((referencing != null) && !referencing.isEmpty()) {
            String msg = I18n.err(I18n.ERR_16040_CANNOT_REMOVE_FROM_REGISTRY, schemaObject.getOid(), Strings.setToString(referencing));
            Throwable error = new LdapProtocolErrorException(msg);
            errors.add(error);
            return false;
        }
        String schemaName = getSchemaName(toDelete);
        // At this point, the deleted AttributeType may be referenced, 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_16041_CANNOT_DELETE_SCHEMA_OBJECT, schemaObject.getOid());
            LOG.info(msg);
            Throwable error = new LdapProtocolErrorException(msg);
            errors.add(error);
            return false;
        }
        if (schema.isEnabled() && schemaObject.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);
            }
            // Delete the SchemaObject from the cloned registries
            clonedRegistries.delete(errors, toDelete);
            // Remove the cloned registries
            clonedRegistries.clear();
            // If we didn't get any error, apply the deletion to the real retistries
            if (errors.isEmpty()) {
                // Apply the deletion to the real registries
                registries.delete(errors, toDelete);
                LOG.debug(I18n.msg(I18n.MSG_16022_REMOVED_FROM_ENABLED_SCHEMA, toDelete.getName(), schemaName));
                return true;
            } else {
                // We have some error : reject the deletion and get out
                LOG.info(I18n.msg(I18n.MSG_16023_CANNOT_DELETE_SCHEMAOBJECT, schemaObject.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, schemaObject);
            LOG.debug(I18n.msg(I18n.MSG_16024_REMOVED_FROM_DISABLED_SCHEMA, schemaObject.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) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries) SchemaObjectWrapper(org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException)

Example 8 with Registries

use of org.apache.directory.api.ldap.model.schema.registries.Registries in project directory-ldap-api by apache.

the class DefaultSchemaManager method loadWithDeps.

/**
 * {@inheritDoc}
 */
@Override
public boolean loadWithDeps(Schema... schemas) throws LdapException {
    boolean loaded = false;
    // Reset the errors if not null
    if (errors != null) {
        errors.clear();
    }
    // Work on a cloned and relaxed registries
    Registries clonedRegistries = cloneRegistries();
    clonedRegistries.setRelaxed();
    // Load the schemas
    for (Schema schema : schemas) {
        loadDepsFirst(clonedRegistries, schema);
    }
    // Build the cross references
    errors = clonedRegistries.buildReferences();
    if (errors.isEmpty()) {
        // Ok no errors. Check the registries now
        errors = clonedRegistries.checkRefInteg();
        if (errors.isEmpty()) {
            // We are golden : let's apply the schema in the real registries
            registries = clonedRegistries;
            registries.setStrict();
            loaded = true;
        }
    } else if (isStrict()) {
        // clear the cloned registries
        clonedRegistries.clear();
    } else {
        // Relaxed mode
        registries = clonedRegistries;
        registries.setRelaxed();
        loaded = true;
    }
    return loaded;
}
Also used : Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries)

Example 9 with Registries

use of org.apache.directory.api.ldap.model.schema.registries.Registries 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 10 with Registries

use of org.apache.directory.api.ldap.model.schema.registries.Registries in project directory-ldap-api by apache.

the class DefaultSchemaManager method load.

/**
 * {@inheritDoc}
 */
@Override
public boolean load(Schema... schemas) throws LdapException {
    if (schemas.length == 0) {
        return true;
    }
    boolean loaded = false;
    // Reset the errors if not null
    if (errors != null) {
        errors.clear();
    }
    // Work on a cloned and relaxed registries
    Registries clonedRegistries = cloneRegistries();
    clonedRegistries.setRelaxed();
    // Load the schemas
    for (Schema schema : schemas) {
        boolean singleSchemaLoaded = load(clonedRegistries, schema);
        // return false if the schema was not loaded in the first place
        if (!singleSchemaLoaded) {
            return false;
        }
    }
    // Build the cross references
    errors = clonedRegistries.buildReferences();
    if (errors.isEmpty()) {
        // Ok no errors. Check the registries now
        errors = clonedRegistries.checkRefInteg();
        if (errors.isEmpty()) {
            // We are golden : let's apply the schema in the real registries
            registries.setRelaxed();
            // Load the schemas
            for (Schema schema : schemas) {
                load(registries, schema);
                // Update the schema dependences if needed
                if (schema.getDependencies() != null) {
                    for (String dep : schema.getDependencies()) {
                        Set<String> deps = schemaDependencies.get(dep);
                        if (deps == null) {
                            deps = new HashSet<>();
                            deps.add(schema.getSchemaName());
                        }
                        // Replace the dependences
                        schemaDependencies.put(dep, deps);
                    }
                }
                // add the schema to the SchemaMap
                schemaMap.put(schema.getSchemaName(), schema);
            }
            // Build the cross references
            errors = registries.buildReferences();
            registries.setStrict();
            loaded = true;
        }
    }
    // clear the cloned registries
    clonedRegistries.clear();
    return loaded;
}
Also used : Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries)

Aggregations

Registries (org.apache.directory.api.ldap.model.schema.registries.Registries)12 Schema (org.apache.directory.api.ldap.model.schema.registries.Schema)9 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)5 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)3 SchemaObject (org.apache.directory.api.ldap.model.schema.SchemaObject)3 IOException (java.io.IOException)2 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)2 LoadableSchemaObject (org.apache.directory.api.ldap.model.schema.LoadableSchemaObject)2 ConnectException (java.net.ConnectException)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 DecoderException (org.apache.directory.api.asn1.DecoderException)1 MessageEncoderException (org.apache.directory.api.ldap.codec.api.MessageEncoderException)1 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)1 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)1 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)1 Value (org.apache.directory.api.ldap.model.entry.Value)1 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)1