use of org.apache.directory.api.ldap.model.exception.LdapOtherException 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();
}
}
}
Aggregations