mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
02.22.2016 f4f852981fb7aed262eae385aec5bcba8dfe5049
SchemaBackend.java: code cleanups

In replaceEntry(), extracted methods addAttribute() and deleteAttribute()
Extracted methods is02ConfigLdif(), addIfNotNull() and addAllNonSchemaConfigAttributes()
1 files modified
215 ■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java 215 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
@@ -743,6 +743,60 @@
      switch (m.getModificationType().asEnum())
      {
        case ADD:
          addAttribute(newSchema, a, modifiedSchemaFiles);
          break;
        case DELETE:
          deleteAttribute(newSchema, a, mods, pos, modifiedSchemaFiles);
          break;
        case REPLACE:
          if (!m.isInternal()
              && !modifyOperation.isSynchronizationOperation())
          {
            throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                ERR_SCHEMA_INVALID_MODIFICATION_TYPE.get(m.getModificationType()));
          }
          else  if (SchemaConfigManager.isSchemaAttribute(a))
          {
          logger.error(ERR_SCHEMA_INVALID_REPLACE_MODIFICATION, a.getAttributeDescription());
          }
          else
          {
            // If this is not a Schema attribute, we put it
            // in the extraAttribute map. This in fact acts as a replace.
            newSchema.addExtraAttribute(at.getNameOrOID(), a);
            modifiedSchemaFiles.add(FILE_USER_SCHEMA_ELEMENTS);
          }
          break;
        default:
        throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
              ERR_SCHEMA_INVALID_MODIFICATION_TYPE.get(m.getModificationType()));
      }
    }
    // If we've gotten here, then everything looks OK, re-write all the
    // modified Schema Files.
    updateSchemaFiles(newSchema, modifiedSchemaFiles);
    // Finally set DirectoryServer to use the new Schema.
    DirectoryServer.setSchema(newSchema);
    DN authzDN = modifyOperation.getAuthorizationDN();
    if (authzDN == null)
    {
      authzDN = DN.rootDN();
    }
    modifiersName = ByteString.valueOfUtf8(authzDN.toString());
    modifyTimestamp = GeneralizedTimeSyntax.createGeneralizedTimeValue(
                           System.currentTimeMillis());
  }
  private void addAttribute(Schema newSchema, Attribute a, Set<String> modifiedSchemaFiles) throws DirectoryException
  {
    AttributeType at = a.getAttributeDescription().getAttributeType();
          if (at.equals(attributeTypesType))
          {
            for (ByteString v : a)
@@ -815,10 +869,12 @@
            LocalizableMessage message = ERR_SCHEMA_MODIFY_UNSUPPORTED_ATTRIBUTE_TYPE.get(a.getAttributeDescription());
            throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
          }
  }
          break;
        case DELETE:
  private void deleteAttribute(Schema newSchema, Attribute a, List<Modification> mods, int pos,
      Set<String> modifiedSchemaFiles) throws DirectoryException
  {
    AttributeType at = a.getAttributeDescription().getAttributeType();
          if (a.isEmpty())
          {
            LocalizableMessage message = ERR_SCHEMA_MODIFY_DELETE_NO_VALUES.get(a.getAttributeDescription());
@@ -898,51 +954,6 @@
            LocalizableMessage message = ERR_SCHEMA_MODIFY_UNSUPPORTED_ATTRIBUTE_TYPE.get(a.getAttributeDescription());
            throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
          }
          break;
        case REPLACE:
          if (!m.isInternal()
              && !modifyOperation.isSynchronizationOperation())
          {
            throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                ERR_SCHEMA_INVALID_MODIFICATION_TYPE.get(m.getModificationType()));
          }
          else  if (SchemaConfigManager.isSchemaAttribute(a))
          {
          logger.error(ERR_SCHEMA_INVALID_REPLACE_MODIFICATION, a.getAttributeDescription());
          }
          else
          {
            // If this is not a Schema attribute, we put it
            // in the extraAttribute map. This in fact acts as a replace.
            newSchema.addExtraAttribute(at.getNameOrOID(), a);
            modifiedSchemaFiles.add(FILE_USER_SCHEMA_ELEMENTS);
          }
          break;
        default:
        throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
              ERR_SCHEMA_INVALID_MODIFICATION_TYPE.get(m.getModificationType()));
      }
    }
    // If we've gotten here, then everything looks OK, re-write all the
    // modified Schema Files.
    updateSchemaFiles(newSchema, modifiedSchemaFiles);
    // Finally set DirectoryServer to use the new Schema.
    DirectoryServer.setSchema(newSchema);
    DN authzDN = modifyOperation.getAuthorizationDN();
    if (authzDN == null)
    {
      authzDN = DN.rootDN();
    }
    modifiersName = ByteString.valueOfUtf8(authzDN.toString());
    modifyTimestamp = GeneralizedTimeSyntax.createGeneralizedTimeValue(
                           System.currentTimeMillis());
  }
  /**
@@ -955,8 +966,7 @@
   *
   * @throws DirectoryException  When the new file cannot be written.
   */
  private void updateSchemaFiles(
               Schema newSchema, TreeSet<String> modifiedSchemaFiles)
  private void updateSchemaFiles(Schema newSchema, TreeSet<String> modifiedSchemaFiles)
          throws DirectoryException
  {
    // We'll re-write all
@@ -1354,7 +1364,7 @@
   *                              schema.
   */
  private void removeObjectClass(ObjectClass objectClass, Schema schema,
                                 ArrayList<Modification> modifications,
                                 List<Modification> modifications,
                                 int currentPosition,
                                 Set<String> modifiedSchemaFiles)
          throws DirectoryException
@@ -1552,7 +1562,7 @@
   *                              the provided name form from the server schema.
   */
  private void removeNameForm(NameForm nameForm, Schema schema,
                              ArrayList<Modification> modifications,
                              List<Modification> modifications,
                              int currentPosition,
                              Set<String> modifiedSchemaFiles)
          throws DirectoryException
@@ -1914,7 +1924,7 @@
   */
  private void removeDITStructureRule(DITStructureRule ditStructureRule,
                                      Schema schema,
                                      ArrayList<Modification> modifications,
                                      List<Modification> modifications,
                                      int currentPosition,
                                      Set<String> modifiedSchemaFiles)
          throws DirectoryException
@@ -2943,14 +2953,10 @@
      // or modified in the schema
      for (ByteString v : a)
      {
        // Parse the attribute type.
        AttributeType attrType = schema.parseAttributeType(v.toString());
        String schemaFile = getSchemaFile(attrType);
        if (CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
        if (is02ConfigLdif(schemaFile))
        {
          // Don't import the file containing the definitions of the
          // Schema elements used for configuration because these
          // definitions may vary between versions of OpenDJ.
          continue;
        }
@@ -2962,11 +2968,7 @@
          if (hasDefinitionChanged(schema, attrType))
          {
            newSchema.registerAttributeType(attrType, schemaFile, true);
            if (schemaFile != null)
            {
              modifiedSchemaFiles.add(schemaFile);
            }
            addIfNotNull(modifiedSchemaFiles, schemaFile);
          }
        }
        catch (Exception e)
@@ -2981,21 +2983,15 @@
    for (AttributeType removeType : newSchema.getAttributeTypes())
    {
      String schemaFile = getSchemaFile(removeType);
      if (CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile) || CORE_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
      if (is02ConfigLdif(schemaFile) || CORE_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
      {
        // Don't import the file containing the definitions of the
        // Schema elements used for configuration because these
        // definitions may vary between versions of OpenDJ.
        // Also never delete anything from the core schema file.
        continue;
      }
      if (!oidList.contains(removeType.getOID()))
      {
        newSchema.deregisterAttributeType(removeType);
        if (schemaFile != null)
        {
          modifiedSchemaFiles.add(schemaFile);
        }
        addIfNotNull(modifiedSchemaFiles, schemaFile);
      }
    }
@@ -3010,11 +3006,8 @@
        // appear in the new config schema.
        ObjectClass newObjectClass = newSchema.parseObjectClass(v.toString());
        String schemaFile = getSchemaFile(newObjectClass);
        if (CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
        if (is02ConfigLdif(schemaFile))
        {
          // Don't import the file containing the definitions of the
          // Schema elements used for configuration because these
          // definitions may vary between versions of OpenDJ.
          continue;
        }
@@ -3026,11 +3019,7 @@
          if (hasDefinitionChanged(schema, newObjectClass))
          {
            newSchema.registerObjectClass(newObjectClass, schemaFile, true);
            if (schemaFile != null)
            {
              modifiedSchemaFiles.add(schemaFile);
            }
            addIfNotNull(modifiedSchemaFiles, schemaFile);
          }
        }
        catch (Exception e)
@@ -3045,21 +3034,14 @@
    for (ObjectClass removeClass : newSchema.getObjectClasses())
    {
      String schemaFile = getSchemaFile(removeClass);
      if (CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
      if (is02ConfigLdif(schemaFile))
      {
        // Don't import the file containing the definition of the
        // Schema elements used for configuration because these
        // definitions may vary between versions of OpenDJ.
        continue;
      }
      if (!oidList.contains(removeClass.getOID()))
      {
        newSchema.deregisterObjectClass(removeClass);
        if (schemaFile != null)
        {
          modifiedSchemaFiles.add(schemaFile);
        }
        addIfNotNull(modifiedSchemaFiles, schemaFile);
      }
    }
@@ -3072,6 +3054,23 @@
    }
  }
  /**
   * Do not import the file containing the definitions of the Schema elements used for configuration
   * because these definitions may vary between versions of OpenDJ.
   */
  private boolean is02ConfigLdif(String schemaFile)
  {
    return CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile);
  }
  private <T> void addIfNotNull(Collection<T> col, T element)
  {
    if (element != null)
    {
      col.add(element);
    }
  }
  private boolean hasDefinitionChanged(Schema schema, AttributeType newAttrType)
  {
    AttributeType oldAttrType = schema.getAttributeType(newAttrType.getOID());
@@ -3144,28 +3143,8 @@
    try
    {
      Entry configEntry = DirectoryServer.getConfigEntry(configEntryDN);
      for (List<Attribute> attrs :
           configEntry.getUserAttributes().values())
      {
        for (Attribute a : attrs)
        {
          if (! isSchemaConfigAttribute(a))
          {
            newUserAttrs.add(a);
          }
        }
      }
      for (List<Attribute> attrs :
           configEntry.getOperationalAttributes().values())
      {
        for (Attribute a : attrs)
        {
          if (! isSchemaConfigAttribute(a))
          {
            newUserAttrs.add(a);
          }
        }
      }
      addAllNonSchemaConfigAttributes(newUserAttrs, configEntry.getUserAttributes().values());
      addAllNonSchemaConfigAttributes(newUserAttrs, configEntry.getOperationalAttributes().values());
    }
    catch (ConfigException e)
    {
@@ -3235,6 +3214,20 @@
    return ccr;
  }
  private void addAllNonSchemaConfigAttributes(List<Attribute> newUserAttrs, Collection<List<Attribute>> attributes)
  {
    for (List<Attribute> attrs : attributes)
    {
      for (Attribute a : attrs)
      {
        if (!isSchemaConfigAttribute(a))
        {
          newUserAttrs.add(a);
        }
      }
    }
  }
  /**
   * Indicates whether to treat common schema attributes like user attributes
   * rather than operational attributes.