Search in sources :

Example 46 with Schema

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

the class DefaultSchemaManager method toArray.

/**
 * Transform a String[] array of schema to a Schema[]
 */
private Schema[] toArray(String... schemas) throws LdapException {
    Schema[] schemaArray = new Schema[schemas.length];
    int n = 0;
    for (String schemaName : schemas) {
        Schema schema = schemaMap.get(schemaName);
        if (schema != null) {
            schemaArray[n++] = schema;
        } else {
            throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, I18n.err(I18n.ERR_11001, schemaName));
        }
    }
    return schemaArray;
}
Also used : LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema)

Example 47 with Schema

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

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

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

the class DefaultSchemaManager method loadDepsFirstRelaxed.

/**
 * Recursive method which loads schema's with their dependent schemas first
 * and tracks what schemas it has seen so the recursion does not go out of
 * control with dependency cycle detection.
 *
 * @param schema the current schema we are attempting to load
 * @throws Exception if there is a cycle detected and/or another
 * failure results while loading, producing and or registering schema objects
 */
private void loadDepsFirstRelaxed(Schema schema) throws LdapException {
    if (schema == null) {
        LOG.info(I18n.msg(I18n.MSG_16013_SCHEMA_IS_NULL));
        return;
    }
    if (schema.isDisabled() && !registries.isDisabledAccepted()) {
        LOG.info(I18n.msg(I18n.MSG_16017_UNACCEPTED_DISABLED_SCHEMA));
        return;
    }
    String schemaName = schema.getSchemaName();
    if (registries.isSchemaLoaded(schemaName)) {
        LOG.info(I18n.msg(I18n.MSG_16018_SCHEMA_ALREADY_LOADED, schema.getSchemaName()));
        return;
    }
    String[] deps = schema.getDependencies();
    // if no deps then load this guy and return
    if ((deps == null) || (deps.length == 0)) {
        load(registries, schema);
        return;
    }
    /*
         * We got deps and need to load them before this schema.  We go through
         * all deps loading them with their deps first if they have not been
         * loaded.
         */
    for (String depName : deps) {
        if (registries.isSchemaLoaded(schemaName)) {
            // The schema is already loaded. Loop on the next schema
            continue;
        } else {
            // Call recursively this method
            Schema schemaDep = schema.getSchemaLoader().getSchema(depName);
            loadDepsFirstRelaxed(schemaDep);
        }
    }
    // Now load the current schema
    load(registries, schema);
}
Also used : Schema(org.apache.directory.api.ldap.model.schema.registries.Schema)

Example 50 with Schema

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

the class DefaultSchemaManager method load.

/**
 * Load the schema in the registries. We will load everything accordingly to the two flags :
 * - isRelaxed
 * - disabledAccepted
 */
private boolean load(Registries registries, Schema schema) throws LdapException {
    if (schema == null) {
        LOG.info(I18n.msg(I18n.MSG_16013_SCHEMA_IS_NULL));
        return false;
    }
    // First avoid loading twice the same schema
    if (registries.isSchemaLoaded(schema.getSchemaName())) {
        return true;
    }
    if (schema.isDisabled()) {
        if (registries.isDisabledAccepted()) {
            LOG.info(I18n.msg(I18n.MSG_16014_LOADING_DISABLED_SCHEMA, schema.getSchemaName(), schema));
            registries.schemaLoaded(schema);
            addSchemaObjects(schema, registries);
        } else {
            return false;
        }
    } else {
        LOG.info(I18n.msg(I18n.MSG_16015_LOADING_ENABLED_SCHEMA, schema.getSchemaName(), schema));
        // Check that the dependencies, if any, are correct
        if (schema.getDependencies() != null) {
            for (String dependency : schema.getDependencies()) {
                Schema dependencySchema = schemaMap.get(dependency);
                if (dependencySchema == null) {
                    // The dependency has not been loaded.
                    String msg = I18n.err(I18n.ERR_16035_CANNOT_LOAD_SCHEMA, schema.getSchemaName());
                    LOG.info(msg);
                    Throwable error = new LdapProtocolErrorException(msg);
                    errors.add(error);
                    return false;
                }
                // If the dependency is disabled, then enable it
                if (dependencySchema.isDisabled()) {
                    dependencySchema.enable();
                    if (!load(registries, dependencySchema)) {
                        dependencySchema.disable();
                        return false;
                    }
                }
            }
        }
        registries.schemaLoaded(schema);
        addSchemaObjects(schema, registries);
    }
    return true;
}
Also used : LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema)

Aggregations

Schema (org.apache.directory.api.ldap.model.schema.registries.Schema)74 ArrayList (java.util.ArrayList)34 Entry (org.apache.directory.api.ldap.model.entry.Entry)34 DefaultSchema (org.apache.directory.api.ldap.model.schema.registries.DefaultSchema)28 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)26 LdifReader (org.apache.directory.api.ldap.model.ldif.LdifReader)25 File (java.io.File)13 SchemaObject (org.apache.directory.api.ldap.model.schema.SchemaObject)13 SchemaObjectWrapper (org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper)13 URL (java.net.URL)12 LdapUnwillingToPerformException (org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException)12 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)11 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)9 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)9 Registries (org.apache.directory.api.ldap.model.schema.registries.Registries)9 AttributesFactory (org.apache.directory.api.ldap.model.schema.AttributesFactory)8 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)7 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)7 Test (org.junit.Test)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6