From 0a91f5af5295a2d0f5cfdad0d529d4b5d532c181 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 10 Feb 2015 15:34:15 +0000
Subject: [PATCH] OPENDJ-1716 Various PluggableBackend/Storage refactorings

---
 opendj3-server-dev/src/server/org/opends/server/backends/BackupBackend.java |  239 +++++++++++++++++++----------------------------------------
 1 files changed, 76 insertions(+), 163 deletions(-)

diff --git a/opendj3-server-dev/src/server/org/opends/server/backends/BackupBackend.java b/opendj3-server-dev/src/server/org/opends/server/backends/BackupBackend.java
index 4594400..84c8da5 100644
--- a/opendj3-server-dev/src/server/org/opends/server/backends/BackupBackend.java
+++ b/opendj3-server-dev/src/server/org/opends/server/backends/BackupBackend.java
@@ -28,6 +28,7 @@
 
 import static org.opends.messages.BackendMessages.*;
 import static org.opends.server.config.ConfigConstants.*;
+import static org.opends.server.schema.BooleanSyntax.*;
 import static org.opends.server.util.ServerConstants.*;
 import static org.opends.server.util.StaticUtils.*;
 
@@ -52,7 +53,6 @@
 import org.opends.server.core.ModifyDNOperation;
 import org.opends.server.core.ModifyOperation;
 import org.opends.server.core.SearchOperation;
-import org.opends.server.schema.BooleanSyntax;
 import org.opends.server.schema.GeneralizedTimeSyntax;
 import org.opends.server.types.*;
 
@@ -558,14 +558,10 @@
     LinkedHashMap<AttributeType,List<Attribute>> userAttrs =
          new LinkedHashMap<AttributeType,List<Attribute>>(3);
 
-    ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(t, v));
-    userAttrs.put(t, attrList);
+    userAttrs.put(t, asList(t, v));
 
     t = DirectoryServer.getAttributeType(ATTR_BACKUP_BACKEND_DN, true);
-    attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(t, ByteString.valueOf(backupDirectory.getConfigEntryDN().toString())));
-    userAttrs.put(t, attrList);
+    userAttrs.put(t, asList(t, ByteString.valueOf(backupDirectory.getConfigEntryDN().toString())));
 
     Entry e = new Entry(entryDN, ocMap, userAttrs, opAttrs);
     e.processVirtualAttributes();
@@ -590,28 +586,24 @@
           throws DirectoryException
   {
     // First, get the backup ID from the entry DN.
-    AttributeType idType = DirectoryServer.getAttributeType(ATTR_BACKUP_ID,
-        true);
+    AttributeType idType = DirectoryServer.getAttributeType(ATTR_BACKUP_ID, true);
     ByteString idValue = entryDN.rdn().getAttributeValue(idType);
     if (idValue == null) {
-      LocalizableMessage message = ERR_BACKUP_NO_BACKUP_ID_IN_DN.get(entryDN);
-      throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
+      throw newConstraintViolation(ERR_BACKUP_NO_BACKUP_ID_IN_DN.get(entryDN));
     }
     String backupID = idValue.toString();
 
     // Next, get the backup directory from the parent DN.
     DN parentDN = entryDN.getParentDNInSuffix();
     if (parentDN == null) {
-      LocalizableMessage message = ERR_BACKUP_NO_BACKUP_PARENT_DN.get(entryDN);
-      throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
+      throw newConstraintViolation(ERR_BACKUP_NO_BACKUP_PARENT_DN.get(entryDN));
     }
 
     AttributeType t = DirectoryServer.getAttributeType(
         ATTR_BACKUP_DIRECTORY_PATH, true);
     ByteString v = parentDN.rdn().getAttributeValue(t);
     if (v == null) {
-      LocalizableMessage message = ERR_BACKUP_NO_BACKUP_DIR_IN_DN.get(entryDN);
-      throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
+      throw newConstraintViolation(ERR_BACKUP_NO_BACKUP_DIR_IN_DN.get(entryDN));
     }
 
     BackupDirectory backupDirectory;
@@ -620,8 +612,7 @@
     } catch (ConfigException ce) {
       logger.traceException(ce);
 
-      LocalizableMessage message = ERR_BACKUP_INVALID_BACKUP_DIRECTORY.get(entryDN, ce.getMessageObject());
-      throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
+      throw newConstraintViolation(ERR_BACKUP_INVALID_BACKUP_DIRECTORY.get(entryDN, ce.getMessageObject()));
     } catch (Exception e) {
       logger.traceException(e);
 
@@ -655,40 +646,21 @@
     LinkedHashMap<AttributeType, List<Attribute>> userAttrs =
       new LinkedHashMap<AttributeType, List<Attribute>>();
 
-    ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(idType, idValue));
-    userAttrs.put(idType, attrList);
+    userAttrs.put(idType, asList(idType, idValue));
 
     backupInfo.getBackupDirectory();
-    attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(t, v));
-    userAttrs.put(t, attrList);
+    userAttrs.put(t, asList(t, v));
 
     Date backupDate = backupInfo.getBackupDate();
     if (backupDate != null) {
       t = DirectoryServer.getAttributeType(ATTR_BACKUP_DATE, true);
-      attrList = new ArrayList<Attribute>(1);
-      attrList.add(Attributes.create(t, ByteString.valueOf(GeneralizedTimeSyntax.format(backupDate))));
-      userAttrs.put(t, attrList);
+      userAttrs.put(t,
+          asList(t, ByteString.valueOf(GeneralizedTimeSyntax.format(backupDate))));
     }
 
-    t = DirectoryServer.getAttributeType(ATTR_BACKUP_COMPRESSED, true);
-    attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(t, BooleanSyntax
-        .createBooleanValue(backupInfo.isCompressed())));
-    userAttrs.put(t, attrList);
-
-    t = DirectoryServer.getAttributeType(ATTR_BACKUP_ENCRYPTED, true);
-    attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(t, BooleanSyntax
-        .createBooleanValue(backupInfo.isEncrypted())));
-    userAttrs.put(t, attrList);
-
-    t = DirectoryServer.getAttributeType(ATTR_BACKUP_INCREMENTAL, true);
-    attrList = new ArrayList<Attribute>(1);
-    attrList.add(Attributes.create(t, BooleanSyntax
-        .createBooleanValue(backupInfo.isIncremental())));
-    userAttrs.put(t, attrList);
+    putBoolean(userAttrs, ATTR_BACKUP_COMPRESSED, backupInfo.isCompressed());
+    putBoolean(userAttrs, ATTR_BACKUP_ENCRYPTED, backupInfo.isEncrypted());
+    putBoolean(userAttrs, ATTR_BACKUP_INCREMENTAL, backupInfo.isIncremental());
 
     HashSet<String> dependencies = backupInfo.getDependencies();
     if (dependencies != null && !dependencies.isEmpty()) {
@@ -697,34 +669,26 @@
       for (String s : dependencies) {
         builder.add(s);
       }
-      attrList = new ArrayList<Attribute>(1);
+      ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
       attrList.add(builder.toAttribute());
       userAttrs.put(t, attrList);
     }
 
     byte[] signedHash = backupInfo.getSignedHash();
     if (signedHash != null) {
-      t = DirectoryServer.getAttributeType(ATTR_BACKUP_SIGNED_HASH, true);
-      attrList = new ArrayList<Attribute>(1);
-      attrList.add(Attributes.create(t, ByteString.wrap(signedHash)));
-      userAttrs.put(t, attrList);
+      putByteString(userAttrs, ATTR_BACKUP_SIGNED_HASH, signedHash);
     }
 
     byte[] unsignedHash = backupInfo.getUnsignedHash();
     if (unsignedHash != null) {
-      t = DirectoryServer.getAttributeType(ATTR_BACKUP_UNSIGNED_HASH, true);
-      attrList = new ArrayList<Attribute>(1);
-      attrList.add(Attributes.create(t, ByteString.wrap(unsignedHash)));
-      userAttrs.put(t, attrList);
+      putByteString(userAttrs, ATTR_BACKUP_UNSIGNED_HASH, unsignedHash);
     }
 
     HashMap<String, String> properties = backupInfo.getBackupProperties();
     if (properties != null && !properties.isEmpty()) {
       for (Map.Entry<String, String> e : properties.entrySet()) {
         t = DirectoryServer.getAttributeType(toLowerCase(e.getKey()), true);
-        attrList = new ArrayList<Attribute>(1);
-        attrList.add(Attributes.create(t, ByteString.valueOf(e.getValue())));
-        userAttrs.put(t, attrList);
+        userAttrs.put(t, asList(t, ByteString.valueOf(e.getValue())));
       }
     }
 
@@ -733,7 +697,30 @@
     return e;
   }
 
+  private void putByteString(LinkedHashMap<AttributeType, List<Attribute>> userAttrs, String attrName, byte[] value)
+  {
+    AttributeType t;
+    t = DirectoryServer.getAttributeType(attrName, true);
+    userAttrs.put(t, asList(t, ByteString.wrap(value)));
+  }
 
+  private void putBoolean(LinkedHashMap<AttributeType, List<Attribute>> attrsMap, String attrName, boolean value)
+  {
+    AttributeType t = DirectoryServer.getAttributeType(attrName, true);
+    attrsMap.put(t, asList(t, createBooleanValue(value)));
+  }
+
+  private ArrayList<Attribute> asList(AttributeType attrType, ByteString value)
+  {
+    final ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
+    attrList.add(Attributes.create(attrType, value));
+    return attrList;
+  }
+
+  private DirectoryException newConstraintViolation(LocalizableMessage message)
+  {
+    return new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
+  }
 
   /** {@inheritDoc} */
   @Override
@@ -841,38 +828,8 @@
 
           if (scope != SearchScope.SINGLE_LEVEL)
           {
-            List<Attribute> attrList =
-                 backupDirEntry.getAttribute(backupPathType);
-            if (attrList != null && !attrList.isEmpty())
-            {
-              for (ByteString v : attrList.get(0))
-              {
-                try
-                {
-                  File subtreeDir = new File(v.toString());
-                  BackupDirectory backupDirectory = backupDirectories.get(subtreeDir).getBackupDirectory();
-                  AttributeType idType =
-                       DirectoryServer.getAttributeType(ATTR_BACKUP_ID,
-                                                        true);
-                  for (String backupID : backupDirectory.getBackups().keySet())
-                  {
-                    DN backupEntryDN = makeChildDN(backupDirDN, idType,
-                                                   backupID);
-                    Entry backupEntry = getBackupEntry(backupEntryDN);
-                    if (filter.matchesEntry(backupEntry))
-                    {
-                      searchOperation.returnEntry(backupEntry, null);
-                    }
-                  }
-                }
-                catch (Exception e)
-                {
-                  logger.traceException(e);
-
-                  continue;
-                }
-              }
-            }
+            List<Attribute> attrList = backupDirEntry.getAttribute(backupPathType);
+            returnEntries(searchOperation, backupDirDN, filter, attrList);
           }
         }
       }
@@ -893,36 +850,7 @@
         AttributeType t =
              DirectoryServer.getAttributeType(ATTR_BACKUP_DIRECTORY_PATH, true);
         List<Attribute> attrList = backupDirEntry.getAttribute(t);
-        if (attrList != null && !attrList.isEmpty())
-        {
-          for (ByteString v : attrList.get(0))
-          {
-            try
-            {
-              File dir = new File(v.toString());
-              BackupDirectory backupDirectory = backupDirectories.get(dir).getBackupDirectory();
-              AttributeType idType =
-                   DirectoryServer.getAttributeType(ATTR_BACKUP_ID,
-                                                    true);
-              for (String backupID : backupDirectory.getBackups().keySet())
-              {
-                DN backupEntryDN = makeChildDN(baseDN, idType,
-                                               backupID);
-                Entry backupEntry = getBackupEntry(backupEntryDN);
-                if (filter.matchesEntry(backupEntry))
-                {
-                  searchOperation.returnEntry(backupEntry, null);
-                }
-              }
-            }
-            catch (Exception e)
-            {
-              logger.traceException(e);
-
-              continue;
-            }
-          }
-        }
+        returnEntries(searchOperation, baseDN, filter, attrList);
       }
     }
     else
@@ -952,7 +880,37 @@
     }
   }
 
+  private void returnEntries(SearchOperation searchOperation, DN baseDN, SearchFilter filter, List<Attribute> attrList)
+  {
+    if (attrList != null && !attrList.isEmpty())
+    {
+      for (ByteString v : attrList.get(0))
+      {
+        try
+        {
+          File dir = new File(v.toString());
+          BackupDirectory backupDirectory = backupDirectories.get(dir).getBackupDirectory();
+          AttributeType idType = DirectoryServer.getAttributeType(ATTR_BACKUP_ID, true);
 
+          for (String backupID : backupDirectory.getBackups().keySet())
+          {
+            DN backupEntryDN = makeChildDN(baseDN, idType, backupID);
+            Entry backupEntry = getBackupEntry(backupEntryDN);
+            if (filter.matchesEntry(backupEntry))
+            {
+              searchOperation.returnEntry(backupEntry, null);
+            }
+          }
+        }
+        catch (Exception e)
+        {
+          logger.traceException(e);
+
+          continue;
+        }
+      }
+    }
+  }
 
   /** {@inheritDoc} */
   @Override
@@ -970,14 +928,11 @@
 
   /** {@inheritDoc} */
   @Override
-  public boolean supportsLDIFExport()
+  public boolean supports(BackendOperation backendOperation)
   {
-    // We do not support LDIF exports.
     return false;
   }
 
-
-
   /** {@inheritDoc} */
   @Override
   public void exportLDIF(LDIFExportConfig exportConfig)
@@ -987,17 +942,6 @@
         ERR_BACKEND_IMPORT_AND_EXPORT_NOT_SUPPORTED.get(getBackendID()));
   }
 
-
-
-  /** {@inheritDoc} */
-  @Override
-  public boolean supportsLDIFImport()
-  {
-    return false;
-  }
-
-
-
   /** {@inheritDoc} */
   @Override
   public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
@@ -1007,15 +951,6 @@
         ERR_BACKEND_IMPORT_AND_EXPORT_NOT_SUPPORTED.get(getBackendID()));
   }
 
-
-
-  /** {@inheritDoc} */
-  @Override
-  public boolean supportsBackup()
-  {
-    return false;
-  }
-
   /** {@inheritDoc} */
   @Override
   public void createBackup(BackupConfig backupConfig)
@@ -1025,8 +960,6 @@
         ERR_BACKEND_BACKUP_AND_RESTORE_NOT_SUPPORTED.get(getBackendID()));
   }
 
-
-
   /** {@inheritDoc} */
   @Override
   public void removeBackup(BackupDirectory backupDirectory,
@@ -1037,17 +970,6 @@
         ERR_BACKEND_BACKUP_AND_RESTORE_NOT_SUPPORTED.get(getBackendID()));
   }
 
-
-
-  /** {@inheritDoc} */
-  @Override
-  public boolean supportsRestore()
-  {
-    return false;
-  }
-
-
-
   /** {@inheritDoc} */
   @Override
   public void restoreBackup(RestoreConfig restoreConfig)
@@ -1057,8 +979,6 @@
         ERR_BACKEND_BACKUP_AND_RESTORE_NOT_SUPPORTED.get(getBackendID()));
   }
 
-
-
   /** {@inheritDoc} */
   @Override
   public boolean isConfigurationChangeAcceptable(
@@ -1070,8 +990,6 @@
     return true;
   }
 
-
-
   /** {@inheritDoc} */
   @Override
   public ConfigChangeResult applyConfigurationChange(BackupBackendCfg cfg)
@@ -1090,8 +1008,6 @@
     return ccr;
   }
 
-
-
   /**
    * Create a new child DN from a given parent DN.  The child RDN is formed
    * from a given attribute type and string value.
@@ -1107,12 +1023,9 @@
     return parentDN.child(RDN.create(rdnAttrType, attrValue));
   }
 
-
-
   /** {@inheritDoc} */
   @Override
   public void preloadEntryCache() throws UnsupportedOperationException {
     throw new UnsupportedOperationException("Operation not supported.");
   }
 }
-

--
Gitblit v1.10.0