From bc1073c3489843f707cedcf7f1823c75ed5d8ba4 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 12 Oct 2015 13:04:59 +0000
Subject: [PATCH] code cleanups

---
 opendj-server-legacy/src/main/java/org/opends/server/protocols/internal/SearchRequest.java |   92 ++++++++---------
 opendj-server-legacy/src/main/java/org/opends/server/backends/NullBackend.java             |  161 ++++++++++---------------------
 2 files changed, 95 insertions(+), 158 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/NullBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/NullBackend.java
index feb818d..6920247 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/NullBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/NullBackend.java
@@ -70,27 +70,23 @@
 import org.opends.server.util.LDIFWriter;
 
 /**
- * This class implements /dev/null like backend for development and
- * testing. The following behaviors of this backend implementation
- * should be noted:
+ * This class implements /dev/null like backend for development and testing.
+ * The following behaviors of this backend implementation should be noted:
  * <ul>
  * <li>All read operations return success but no data.
  * <li>All write operations return success but do nothing.
  * <li>Bind operations fail with invalid credentials.
  * <li>Compare operations are only possible on objectclass and return
- * true for the following objeclasses only: top, nullbackendobject,
+ * true for the following objectClasses only: top, nullbackendobject,
  * extensibleobject. Otherwise comparison result is false or comparison
  * fails altogether.
  * <li>Controls are supported although this implementation does not
  * provide any specific emulation for controls. Generally known request
- * controls are accepted and default response controls returned where
- * applicable.
+ * controls are accepted and default response controls returned where applicable.
  * <li>Searches within this backend are always considered indexed.
  * <li>Backend Import is supported by iterating over ldif reader on a
- * single thread and issuing add operations which essentially do nothing
- * at all.
- * <li>Backend Export is supported but does nothing producing an empty
- * ldif.
+ * single thread and issuing add operations which essentially do nothing at all.
+ * <li>Backend Export is supported but does nothing producing an empty ldif.
  * <li>Backend Backup and Restore are not supported.
  * </ul>
  * This backend implementation is for development and testing only, does
@@ -101,8 +97,6 @@
 {
   private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
-
-
   /** The base DNs for this backend. */
   private DN[] baseDNs;
 
@@ -120,8 +114,6 @@
   /** The map of null entry object classes. */
   private Map<ObjectClass,String> objectClasses;
 
-
-
   /**
    * Creates a new backend with the provided information.  All backend
    * implementations must implement a default constructor that use
@@ -145,20 +137,16 @@
     this.baseDNs = baseDNs;
   }
 
-  /** {@inheritDoc} */
   @Override
   public void configureBackend(BackendCfg config, ServerContext serverContext) throws ConfigException
   {
     if (config != null)
     {
       BackendCfg cfg = config;
-      DN[] cfgBaseDNs = new DN[cfg.getBaseDN().size()];
-      cfg.getBaseDN().toArray(cfgBaseDNs);
-      setBaseDNs(cfgBaseDNs);
+      setBaseDNs(cfg.getBaseDN().toArray(new DN[cfg.getBaseDN().size()]));
     }
   }
 
-  /** {@inheritDoc} */
   @Override
   public synchronized void openBackend() throws ConfigException, InitializationException
   {
@@ -176,8 +164,7 @@
       {
         logger.traceException(e);
 
-        LocalizableMessage message = ERR_BACKEND_CANNOT_REGISTER_BASEDN.get(
-            dn, getExceptionMessage(e));
+        LocalizableMessage message = ERR_BACKEND_CANNOT_REGISTER_BASEDN.get(dn, getExceptionMessage(e));
         throw new InitializationException(message, e);
       }
     }
@@ -212,7 +199,6 @@
     objectClasses.put(extOC, extOCName);
   }
 
-  /** {@inheritDoc} */
   @Override
   public synchronized void closeBackend()
   {
@@ -229,21 +215,18 @@
     }
   }
 
-  /** {@inheritDoc} */
   @Override
   public DN[] getBaseDNs()
   {
     return baseDNs;
   }
 
-  /** {@inheritDoc} */
   @Override
   public long getEntryCount()
   {
     return -1;
   }
 
-  /** {@inheritDoc} */
   @Override
   public boolean isIndexed(AttributeType attributeType, IndexType indexType)
   {
@@ -251,109 +234,86 @@
     return true;
   }
 
-  /** {@inheritDoc} */
   @Override
-  public ConditionResult hasSubordinates(DN entryDN)
-         throws DirectoryException
+  public ConditionResult hasSubordinates(DN entryDN) throws DirectoryException
   {
     return ConditionResult.UNDEFINED;
   }
 
-  /** {@inheritDoc} */
   @Override
   public long getNumberOfEntriesInBaseDN(DN baseDN) throws DirectoryException
   {
     throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, ERR_NUM_SUBORDINATES_NOT_SUPPORTED.get());
   }
 
-  /** {@inheritDoc} */
   @Override
   public long getNumberOfChildren(DN parentDN) throws DirectoryException
   {
     throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, ERR_NUM_SUBORDINATES_NOT_SUPPORTED.get());
   }
 
-  /** {@inheritDoc} */
   @Override
   public Entry getEntry(DN entryDN)
   {
     return new Entry(null, objectClasses, null, null);
   }
 
-  /** {@inheritDoc} */
   @Override
   public boolean entryExists(DN entryDN)
   {
     return false;
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void addEntry(Entry entry, AddOperation addOperation)
-         throws DirectoryException
+  public void addEntry(Entry entry, AddOperation addOperation) throws DirectoryException
   {
     return;
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void deleteEntry(DN entryDN, DeleteOperation deleteOperation)
-         throws DirectoryException
+  public void deleteEntry(DN entryDN, DeleteOperation deleteOperation) throws DirectoryException
   {
     return;
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void replaceEntry(Entry oldEntry, Entry newEntry,
-      ModifyOperation modifyOperation) throws DirectoryException
+  public void replaceEntry(Entry oldEntry, Entry newEntry, ModifyOperation modifyOperation) throws DirectoryException
   {
     return;
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void renameEntry(DN currentDN, Entry entry,
-    ModifyDNOperation modifyDNOperation)
-         throws DirectoryException
+  public void renameEntry(DN currentDN, Entry entry, ModifyDNOperation modifyDNOperation) throws DirectoryException
   {
     return;
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void search(SearchOperation searchOperation)
-         throws DirectoryException
+  public void search(SearchOperation searchOperation) throws DirectoryException
   {
     PagedResultsControl pageRequest =
         searchOperation.getRequestControl(PagedResultsControl.DECODER);
 
     if (pageRequest != null) {
       // Indicate no more pages.
-      PagedResultsControl control;
-      control =
+      PagedResultsControl control =
           new PagedResultsControl(pageRequest.isCritical(), 0, null);
       searchOperation.getResponseControls().add(control);
     }
-
-    return;
   }
 
-  /** {@inheritDoc} */
   @Override
   public Set<String> getSupportedControls()
   {
     return supportedControls;
   }
 
-  /** {@inheritDoc} */
   @Override
   public Set<String> getSupportedFeatures()
   {
     return Collections.emptySet();
   }
 
-  /** {@inheritDoc} */
   @Override
   public boolean supports(BackendOperation backendOperation)
   {
@@ -368,44 +328,24 @@
     }
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void exportLDIF(LDIFExportConfig exportConfig)
-         throws DirectoryException
+  public void exportLDIF(LDIFExportConfig exportConfig) throws DirectoryException
   {
-    LDIFWriter ldifWriter;
-
-    try {
-      ldifWriter = new LDIFWriter(exportConfig);
+    try (LDIFWriter ldifWriter = new LDIFWriter(exportConfig))
+    {
+      // just create it to see if it fails
     } catch (Exception e) {
       logger.traceException(e);
 
-      LocalizableMessage message = LocalizableMessage.raw(e.getMessage());
-      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
-        message);
+      throw newDirectoryException(e);
     }
-
-    close(ldifWriter);
   }
 
-  /** {@inheritDoc} */
   @Override
   public LDIFImportResult importLDIF(LDIFImportConfig importConfig, ServerContext serverContext)
       throws DirectoryException
   {
-    LDIFReader reader;
-    try
-    {
-      reader = new LDIFReader(importConfig);
-    }
-    catch (Exception e)
-    {
-      LocalizableMessage message = LocalizableMessage.raw(e.getMessage());
-      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
-                                   message);
-    }
-
-    try
+    try (LDIFReader reader = getReader(importConfig))
     {
       while (true)
       {
@@ -420,16 +360,11 @@
         }
         catch (LDIFException le)
         {
-          if (! le.canContinueReading())
-          {
-            LocalizableMessage message = LocalizableMessage.raw(le.getMessage());
-            throw new DirectoryException(
-              DirectoryServer.getServerErrorResultCode(),message);
-          }
-          else
+          if (le.canContinueReading())
           {
             continue;
           }
+          throw newDirectoryException(le);
         }
 
         try
@@ -452,41 +387,49 @@
     }
     catch (Exception e)
     {
-      LocalizableMessage message = LocalizableMessage.raw(e.getMessage());
-      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
-                                   message);
+      throw newDirectoryException(e);
     }
-    finally
+  }
+
+  private DirectoryException newDirectoryException(Exception e)
+  {
+    LocalizableMessage message = LocalizableMessage.raw(e.getMessage());
+    return new DirectoryException(DirectoryServer.getServerErrorResultCode(), message, e);
+  }
+
+  private LDIFReader getReader(LDIFImportConfig importConfig) throws DirectoryException
+  {
+    try
     {
-      reader.close();
+      return new LDIFReader(importConfig);
+    }
+    catch (Exception e)
+    {
+      throw newDirectoryException(e);
     }
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void createBackup(BackupConfig backupConfig)
-         throws DirectoryException
+  public void createBackup(BackupConfig backupConfig) throws DirectoryException
   {
-    LocalizableMessage message = LocalizableMessage.raw("The null backend does not support backup operation");
-    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
+    throw unwillingToPerformOperation("backup");
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void removeBackup(BackupDirectory backupDirectory,
-                           String backupID)
-         throws DirectoryException
+  public void removeBackup(BackupDirectory backupDirectory, String backupID) throws DirectoryException
   {
-    LocalizableMessage message = LocalizableMessage.raw("The null backend does not support remove backup operation");
-    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
+    throw unwillingToPerformOperation("remove backup");
   }
 
-  /** {@inheritDoc} */
   @Override
-  public void restoreBackup(RestoreConfig restoreConfig)
-         throws DirectoryException
+  public void restoreBackup(RestoreConfig restoreConfig) throws DirectoryException
   {
-    LocalizableMessage message = LocalizableMessage.raw("The null backend does not support restore operation");
-    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
+    throw unwillingToPerformOperation("restore");
+  }
+
+  private DirectoryException unwillingToPerformOperation(String operationName)
+  {
+    String msg = "The null backend does not support " + operationName + " operation";
+    return new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, LocalizableMessage.raw(msg));
   }
 }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/protocols/internal/SearchRequest.java b/opendj-server-legacy/src/main/java/org/opends/server/protocols/internal/SearchRequest.java
index 17b279c..1dca2b1 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/protocols/internal/SearchRequest.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/protocols/internal/SearchRequest.java
@@ -41,12 +41,10 @@
 /**
  * Search request implementation.
  *
- * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl
+ * @see org.forgerock.opendj.ldap.requests.SearchRequest
  */
 public final class SearchRequest extends AbstractRequestImpl {
-    /**
-     * Use a LinkedHashSet to return the attributes in the same order as requested by the user.
-     */
+    /** Use a LinkedHashSet to return the attributes in the same order as requested by the user. */
     private final Set<String> attributes = new LinkedHashSet<>();
     private DereferenceAliasesPolicy dereferenceAliasesPolicy = DereferenceAliasesPolicy.NEVER;
     private SearchFilter filter;
@@ -78,7 +76,7 @@
      * @param attributeDescriptions
      *          the attribute descriptions
      * @return the current object
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#addAttribute(String...)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#addAttribute(String...)
      */
     public SearchRequest addAttribute(final String... attributeDescriptions) {
         for (final String attributeDescription : attributeDescriptions) {
@@ -88,7 +86,7 @@
     }
 
     /**
-     * To be added to {@link org.forgerock.opendj.ldap.requests.SearchRequestImpl}?
+     * To be added to {@link org.forgerock.opendj.ldap.requests.SearchRequest}?
      *
      * @param attributeDescriptions
      *          the attribute descriptions
@@ -105,7 +103,7 @@
      * To be removed.
      *
      * @return the attributes
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getAttributes()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getAttributes()
      */
     public Set<String> getAttributes() {
         return attributes;
@@ -115,7 +113,7 @@
      * To be removed.
      *
      * @return the dereference aliases policy
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getDereferenceAliasesPolicy()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getDereferenceAliasesPolicy()
      */
     public DereferenceAliasesPolicy getDereferenceAliasesPolicy() {
         return dereferenceAliasesPolicy;
@@ -125,7 +123,7 @@
      * To be removed.
      *
      * @return the search filter
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getFilter()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getFilter()
      */
     public SearchFilter getFilter() {
         return filter;
@@ -135,7 +133,7 @@
      * To be removed.
      *
      * @return the DN
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getName()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getName()
      */
     public DN getName() {
         return name;
@@ -145,7 +143,7 @@
      * To be removed.
      *
      * @return the search scope
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getScope()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getScope()
      */
     public SearchScope getScope() {
         return scope;
@@ -155,7 +153,7 @@
      * To be removed.
      *
      * @return the size limit
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getSizeLimit()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getSizeLimit()
      */
     public int getSizeLimit() {
         return sizeLimit;
@@ -165,7 +163,7 @@
      * To be removed.
      *
      * @return is single entry search
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#isSingleEntrySearch()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#isSingleEntrySearch()
      */
     public boolean isSingleEntrySearch() {
         return sizeLimit == 1 || SearchScope.BASE_OBJECT.equals(scope);
@@ -175,7 +173,7 @@
      * To be removed.
      *
      * @return the time limit
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#getTimeLimit()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#getTimeLimit()
      */
     public int getTimeLimit() {
         return timeLimit;
@@ -185,7 +183,7 @@
      * To be removed.
      *
      * @return the types only
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#isTypesOnly()
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#isTypesOnly()
      */
     public boolean isTypesOnly() {
         return typesOnly;
@@ -196,7 +194,7 @@
      *
      * @param policy the dereference aliases policy
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setDereferenceAliasesPolicy(DereferenceAliasesPolicy)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setDereferenceAliasesPolicy(DereferenceAliasesPolicy)
      */
     public SearchRequest setDereferenceAliasesPolicy(final DereferenceAliasesPolicy policy) {
         Reject.ifNull(policy);
@@ -210,7 +208,7 @@
      *
      * @param filter the search filter
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setFilter(org.forgerock.opendj.ldap.Filter)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setFilter(org.forgerock.opendj.ldap.Filter)
      */
     public SearchRequest setFilter(final SearchFilter filter) {
         Reject.ifNull(filter);
@@ -225,7 +223,7 @@
      * @param filter the search filter
      * @return the current request
      * @throws DirectoryException if problem occurs
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setFilter(String)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setFilter(String)
      */
     public SearchRequest setFilter(final String filter) throws DirectoryException {
         this.filter = SearchFilter.createFilterFromString(filter);
@@ -237,7 +235,7 @@
      *
      * @param dn the dn
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setName(org.forgerock.opendj.ldap.DN)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setName(org.forgerock.opendj.ldap.DN)
      */
     public SearchRequest setName(final DN dn) {
         Reject.ifNull(dn);
@@ -252,7 +250,7 @@
      * @param dn the dn
      * @return the current request
      * @throws DirectoryException if problem occurs
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setName(String)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setName(String)
      */
     public SearchRequest setName(final String dn) throws DirectoryException {
         Reject.ifNull(dn);
@@ -266,7 +264,7 @@
      *
      * @param scope the search scope
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setScope(SearchScope)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setScope(SearchScope)
      */
     public SearchRequest setScope(final SearchScope scope) {
         Reject.ifNull(scope);
@@ -280,7 +278,7 @@
      *
      * @param limit the size limit
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setSizeLimit(int)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setSizeLimit(int)
      */
     public SearchRequest setSizeLimit(final int limit) {
         Reject.ifFalse(limit >= 0, "negative size limit");
@@ -294,7 +292,7 @@
      *
      * @param limit the time limit
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setTimeLimit(int)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setTimeLimit(int)
      */
     public SearchRequest setTimeLimit(final int limit) {
         Reject.ifFalse(limit >= 0, "negative time limit");
@@ -308,51 +306,47 @@
      *
      * @param typesOnly the types only
      * @return the current request
-     * @see org.forgerock.opendj.ldap.requests.SearchRequestImpl#setTypesOnly(boolean)
+     * @see org.forgerock.opendj.ldap.requests.SearchRequest#setTypesOnly(boolean)
      */
     public SearchRequest setTypesOnly(final boolean typesOnly) {
         this.typesOnly = typesOnly;
         return this;
     }
 
-    /** {@inheritDoc} */
     @Override
     public SearchRequest addControl(Control control) {
         super.addControl(control);
         return this;
     }
 
-    /** {@inheritDoc} */
     @Override
     public SearchRequest addControl(Collection<Control> controls) {
         super.addControl(controls);
         return this;
     }
 
-    /** {@inheritDoc} */
     @Override
     public String toString() {
-        final StringBuilder builder = new StringBuilder();
-        builder.append("SearchRequest(name=");
-        builder.append(getName());
-        builder.append(", scope=");
-        builder.append(getScope());
-        builder.append(", dereferenceAliasesPolicy=");
-        builder.append(getDereferenceAliasesPolicy());
-        builder.append(", sizeLimit=");
-        builder.append(getSizeLimit());
-        builder.append(", timeLimit=");
-        builder.append(getTimeLimit());
-        builder.append(", typesOnly=");
-        builder.append(isTypesOnly());
-        builder.append(", filter=");
-        builder.append(getFilter());
-        builder.append(", attributes=");
-        builder.append(getAttributes());
-        builder.append(", controls=");
-        builder.append(getControls());
-        builder.append(")");
-        return builder.toString();
+        final StringBuilder sb = new StringBuilder();
+        sb.append(getClass().getSimpleName());
+        sb.append("(name=").append(getName());
+        sb.append(", scope=").append(getScope());
+        sb.append(", filter=").append(getFilter());
+        sb.append(", dereferenceAliasesPolicy=").append(getDereferenceAliasesPolicy());
+        if (getSizeLimit()!=0) {
+          sb.append(", sizeLimit=").append(getSizeLimit());
+        }
+        if (getTimeLimit()!=0) {
+          sb.append(", timeLimit=").append(getTimeLimit());
+        }
+        sb.append(", typesOnly=").append(isTypesOnly());
+        if (!getAttributes().isEmpty()) {
+          sb.append(", attributes=").append(getAttributes());
+        }
+        if (!getControls().isEmpty()) {
+          sb.append(", controls=").append(getControls());
+        }
+        sb.append(")");
+        return sb.toString();
     }
-
 }

--
Gitblit v1.10.0