From 7934d276c1dfdc3224c391c23f574d244c5b0a10 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 29 Nov 2012 23:42:35 +0000
Subject: [PATCH] Minor code cleanup.

---
 opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java                  |    1 
 opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java                            |   20 ++++++++++
 opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java                   |    8 ++--
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFChangeRecordReader.java                 |    1 
 opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/EntryTestCase.java                          |    2 
 opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ArgumentGroup.java                |    2 
 opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/AbstractAsynchronousConnectionTestCase.java |    2 
 opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java                   |   10 ++--
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Matcher.java                                |    3 +
 opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Search.java                    |    1 
 opendj3/opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/DefaultTCPNIOTransportTestCase.java         |   13 ++++--
 opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/GetInfo.java                   |    1 
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFEntryReader.java                        |    1 
 13 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java
index bc7d773..e04718c 100644
--- a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java
+++ b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java
@@ -94,6 +94,7 @@
  * specific credentials to connect, and ensure that your application has access
  * to use the LDAP controls needed.
  */
+@SuppressWarnings("resource")
 public final class Controls {
 
     /**
diff --git a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/GetInfo.java b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/GetInfo.java
index ced5ea8..8d0cbf8 100644
--- a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/GetInfo.java
+++ b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/GetInfo.java
@@ -38,6 +38,7 @@
 /**
  * Demonstrates accessing server information about capabilities and schema.
  */
+@SuppressWarnings("resource")
 public final class GetInfo {
     // Connection information
     private static String host;
diff --git a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Search.java b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Search.java
index 17bfbc8..0a18ee4 100644
--- a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Search.java
+++ b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Search.java
@@ -50,6 +50,7 @@
  *      &lt;baseDN> &lt;scope> &lt;filter> [&lt;attibute> &lt;attribute> ...]
  * </pre>
  */
+@SuppressWarnings("resource")
 public final class Search {
     /**
      * Main method.
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index d892bf3..847ad6d 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -30,6 +30,8 @@
 import static org.forgerock.opendj.ldap.CoreMessages.ERR_HEX_DECODE_INVALID_CHARACTER;
 import static org.forgerock.opendj.ldap.CoreMessages.ERR_HEX_DECODE_INVALID_LENGTH;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
@@ -1144,6 +1146,24 @@
     }
 
     /**
+     * Closes the provided resources ignoring any errors which occurred.
+     *
+     * @param resources
+     *            The resources to be closed, which may be {@code null}.
+     */
+    public static void closeSilently(Closeable... resources) {
+        for (Closeable r : resources) {
+            try {
+                if (r != null) {
+                    r.close();
+                }
+            } catch (IOException ignored) {
+                // Ignore.
+            }
+        }
+    }
+
+    /**
      * Attempts to compress the data in the provided source array into the given
      * destination array. If the compressed data will fit into the destination
      * array, then this method will return the number of bytes of compressed
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Matcher.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Matcher.java
index ebc8d16..871e370 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Matcher.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Matcher.java
@@ -22,6 +22,7 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
+ *      Portions copyright 2012 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -559,6 +560,8 @@
                 switch (matches(v, rule, assertion)) {
                 case TRUE:
                     return ConditionResult.TRUE;
+                case FALSE:
+                    continue;
                 case UNDEFINED:
                     r = ConditionResult.UNDEFINED;
                 }
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFChangeRecordReader.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFChangeRecordReader.java
index 4ba70a7..d9bd11d 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFChangeRecordReader.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFChangeRecordReader.java
@@ -107,6 +107,7 @@
      */
     public static ChangeRecord valueOfLDIFChangeRecord(final String... ldifLines) {
         // LDIF change record reader is tolerant to missing change types.
+        @SuppressWarnings("resource")
         final LDIFChangeRecordReader reader = new LDIFChangeRecordReader(ldifLines);
         try {
             if (!reader.hasNext()) {
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFEntryReader.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFEntryReader.java
index 3cd1a7d..1be1abb 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFEntryReader.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldif/LDIFEntryReader.java
@@ -74,6 +74,7 @@
      *             If {@code ldifLines} was {@code null}.
      */
     public static Entry valueOfLDIFEntry(final String... ldifLines) {
+        @SuppressWarnings("resource")
         final LDIFEntryReader reader = new LDIFEntryReader(ldifLines);
         try {
             if (!reader.hasNext()) {
diff --git a/opendj3/opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/DefaultTCPNIOTransportTestCase.java b/opendj3/opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/DefaultTCPNIOTransportTestCase.java
index 1e3c5bd..7058267 100644
--- a/opendj3/opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/DefaultTCPNIOTransportTestCase.java
+++ b/opendj3/opendj-ldap-sdk/src/test/java/com/forgerock/opendj/ldap/DefaultTCPNIOTransportTestCase.java
@@ -22,6 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
+ *      Portions copyright 2012 ForgeRock AS.
  */
 
 package com.forgerock.opendj.ldap;
@@ -57,10 +58,14 @@
 
         // Establish a socket connection to see if the transport factory works.
         final Socket socket = new Socket();
-        socket.connect(socketAddress);
+        try {
+            socket.connect(socketAddress);
 
-        // Successfully connected if there is no exception.
-        assertTrue(socket.isConnected());
-        // Don't stop the transport because it is shared with the ldap server.
+            // Successfully connected if there is no exception.
+            assertTrue(socket.isConnected());
+            // Don't stop the transport because it is shared with the ldap server.
+        } finally {
+            socket.close();
+        }
     }
 }
diff --git a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/AbstractAsynchronousConnectionTestCase.java b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/AbstractAsynchronousConnectionTestCase.java
index 538b7ca..6b3fc0f 100644
--- a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/AbstractAsynchronousConnectionTestCase.java
+++ b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/AbstractAsynchronousConnectionTestCase.java
@@ -60,7 +60,7 @@
  * Unit test for AbstractAsynchronousConnection. The tests verify that all
  * synchronous operation methods delegate to the equivalent asynchronous method.
  */
-@SuppressWarnings("javadoc")
+@SuppressWarnings({ "javadoc", "resource" })
 public class AbstractAsynchronousConnectionTestCase extends SdkTestCase {
 
     private final class MockConnection extends AbstractAsynchronousConnection {
diff --git a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/EntryTestCase.java b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/EntryTestCase.java
index 5721f28..009f2c7 100644
--- a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/EntryTestCase.java
+++ b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/EntryTestCase.java
@@ -43,7 +43,7 @@
 /**
  * Test {@code Entry}.
  */
-@SuppressWarnings("javadoc")
+@SuppressWarnings({ "javadoc", "resource" })
 public final class EntryTestCase extends SdkTestCase {
 
     private static interface EntryFactory {
diff --git a/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ArgumentGroup.java b/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ArgumentGroup.java
index d9c29e1..f981732 100644
--- a/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ArgumentGroup.java
+++ b/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ArgumentGroup.java
@@ -37,7 +37,7 @@
 /**
  * Class for organizing options into logical groups when arguement usage is
  * printed. To use an argument group, create an instance and use
- * {@link ArgumentParser#addArgument(Argument, ArgumentGroup)}
+ * {@code ArgumentParser#addArgument(Argument, ArgumentGroup)}
  * when adding arguments for to the parser.
  */
 final class ArgumentGroup implements Comparable<ArgumentGroup> {
diff --git a/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java b/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java
index cf9dbb4..94f6e23 100644
--- a/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java
+++ b/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java
@@ -30,6 +30,7 @@
 import static com.forgerock.opendj.ldap.tools.ToolConstants.*;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
 import static com.forgerock.opendj.ldap.tools.Utils.filterExitCode;
+import static com.forgerock.opendj.util.StaticUtils.closeSilently;
 
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -231,6 +232,7 @@
         return verbose.isPresent();
     }
 
+    @SuppressWarnings("resource")
     private int run(final String[] args) {
         // Create the command-line argument parser for use with this
         // program.
@@ -471,8 +473,8 @@
 
         writer = new LDIFEntryWriter(getOutputStream());
         final VisitorImpl visitor = new VisitorImpl();
+        ChangeRecordReader reader = null;
         try {
-            ChangeRecordReader reader;
             if (filename.isPresent()) {
                 try {
                     reader = new LDIFChangeRecordReader(new FileInputStream(filename.getValue()));
@@ -503,9 +505,7 @@
                 return ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue();
             }
         } finally {
-            if (connection != null) {
-                connection.close();
-            }
+            closeSilently(reader, connection);
         }
 
         return ResultCode.SUCCESS.intValue();
diff --git a/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java b/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java
index dfa65b0..4c6873b 100644
--- a/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java
+++ b/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java
@@ -30,6 +30,7 @@
 import static com.forgerock.opendj.ldap.tools.ToolConstants.*;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
 import static com.forgerock.opendj.ldap.tools.Utils.filterExitCode;
+import static com.forgerock.opendj.util.StaticUtils.closeSilently;
 
 import java.io.BufferedReader;
 import java.io.FileReader;
@@ -220,6 +221,7 @@
         return verbose.isPresent();
     }
 
+    @SuppressWarnings("resource")
     private int run(final String[] args, final boolean returnMatchingEntries) {
         // Create the command-line argument parser for use with this
         // program.
@@ -833,10 +835,8 @@
             // We don't actually need to open a connection or perform the
             // search, so we're done. We should return 0 to either mean that the
             // processing was successful or that there were no matching entries,
-            // based
-            // on countEntries.isPresent() (but in either case the return value
-            // should
-            // be zero).
+            // based on countEntries.isPresent() (but in either case the return value
+            // should be zero).
             return 0;
         }
 
@@ -946,7 +946,7 @@
         } catch (final ErrorResultException ere) {
             return Utils.printErrorMessage(this, ere);
         } finally {
-            connection.close();
+            closeSilently(ldifWriter, connection);
         }
 
         return 0;

--
Gitblit v1.10.0