From 1fbfb2b8c865affd0b411e573c88e9ebbc39653c Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 26 Jun 2013 09:04:35 +0000
Subject: [PATCH] StaticUtils.java: Added closeSilently(Iterable<? extends Closeable>)

---
 opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java                  |   20 +++++++++-
 opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MultiColumnPrinter.java |   79 ++++++++++++++++++++-------------------
 opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/PerformanceRunner.java  |   18 +++++----
 3 files changed, 69 insertions(+), 48 deletions(-)

diff --git a/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index f89e2b0..bbfd09e 100644
--- a/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj-sdk/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -81,10 +81,10 @@
      */
     public static final byte[] EMPTY_BYTES = new byte[0];
 
-    // The name of the time zone for universal coordinated time (UTC).
+    /** The name of the time zone for universal coordinated time (UTC). */
     private static final String TIME_ZONE_UTC = "UTC";
 
-    // UTC TimeZone is assumed to never change over JVM lifetime
+    /** UTC TimeZone is assumed to never change over JVM lifetime. */
     private static final TimeZone TIME_ZONE_UTC_OBJ = TimeZone.getTimeZone(TIME_ZONE_UTC);
 
     /**
@@ -1174,6 +1174,22 @@
      *            The resources to be closed, which may be {@code null}.
      */
     public static void closeSilently(Closeable... resources) {
+        if (resources == null) {
+            return;
+        }
+        closeSilently(Arrays.asList(resources));
+    }
+
+    /**
+     * 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(Iterable<? extends Closeable> resources) {
+        if (resources == null) {
+            return;
+        }
         for (Closeable r : resources) {
             try {
                 if (r != null) {
diff --git a/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MultiColumnPrinter.java b/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MultiColumnPrinter.java
index a0ac6c9..a1518f3 100644
--- a/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MultiColumnPrinter.java
+++ b/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MultiColumnPrinter.java
@@ -22,12 +22,13 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package com.forgerock.opendj.ldap.tools;
 
-import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Vector;
 
 /**
@@ -74,16 +75,16 @@
  * </PRE>
  * <P>
  * This class also supports multi-row titles and having title strings spanning
- * multiple collumns. Example usage:
+ * multiple columns. Example usage:
  *
  * <PRE>
  * TestPrinter tp = new TestPrinter(4, 2, &quot;-&quot;);
  * String oneRow[] = new String[4];
  * int[] span = new int[4];
- * span[0] = 2; // spans 2 collumns
- * span[1] = 0; // spans 0 collumns
- * span[2] = 2; // spans 2 collumns
- * span[3] = 0; // spans 0 collumns
+ * span[0] = 2; // spans 2 columns
+ * span[1] = 0; // spans 0 columns
+ * span[2] = 2; // spans 2 columns
+ * span[3] = 0; // spans 0 columns
  * tp.setTitleAlign(CENTER);
  * oneRow[0] = &quot;Name&quot;;
  * oneRow[1] = &quot;&quot;;
@@ -122,11 +123,11 @@
  */
 final class MultiColumnPrinter {
 
-    final static int LEFT = 0;
+    static final int LEFT = 0;
 
-    final static int CENTER = 1;
+    static final int CENTER = 1;
 
-    final static int RIGHT = 2;
+    static final int RIGHT = 2;
 
     private int numCol = 2;
 
@@ -138,9 +139,9 @@
 
     private String border = null;
 
-    private Vector<String[]> titleTable = null;
+    private List<String[]> titleTable = null;
 
-    private Vector<int[]> titleSpanTable = null;
+    private List<int[]> titleSpanTable = null;
 
     private final int[] curLength;
 
@@ -154,6 +155,8 @@
      *            number of columns
      * @param gap
      *            gap between each column
+     * @param app
+     *            the console application to use for outputting data
      */
     MultiColumnPrinter(final int numCol, final int gap, final ConsoleApplication app) {
         this(numCol, gap, null, LEFT, app);
@@ -168,6 +171,8 @@
      *            gap between each column
      * @param border
      *            character used to frame the titles
+     * @param app
+     *            the console application to use for outputting data
      */
     MultiColumnPrinter(final int numCol, final int gap, final String border,
             final ConsoleApplication app) {
@@ -185,6 +190,8 @@
      *            character used to frame the titles
      * @param align
      *            type of alignment within columns
+     * @param app
+     *            the console application to use for outputting data
      */
     MultiColumnPrinter(final int numCol, final int gap, final String border, final int align,
             final ConsoleApplication app) {
@@ -223,11 +230,11 @@
 
     /**
      * Adds to the row of strings to be used as the title for the table. Also
-     * allows for certain title strings to span multiple collumns The span
-     * parameter is an array of integers which indicate how many collumns the
-     * corresponding title string will occupy. For a row that is 4 collumns
+     * allows for certain title strings to span multiple columns The span
+     * parameter is an array of integers which indicate how many columns the
+     * corresponding title string will occupy. For a row that is 4 columns
      * wide, it is possible to have some title strings in a row to 'span'
-     * multiple collumns:
+     * multiple columns:
      * <P>
      *
      * <PRE>
@@ -240,14 +247,14 @@
      * </PRE>
      *
      * In the example above, the title row has a string 'Name' that spans 2
-     * collumns. The string 'Contact' also spans 2 collumns. The above is done
+     * columns. The string 'Contact' also spans 2 columns. The above is done
      * by passing in to addTitle() an array that contains:
      *
      * <PRE>
-     * span[0] = 2; // spans 2 collumns
-     * span[1] = 0; // spans 0 collumns, ignore
-     * span[2] = 2; // spans 2 collumns
-     * span[3] = 0; // spans 0 collumns, ignore
+     * span[0] = 2; // spans 2 columns
+     * span[1] = 0; // spans 0 columns, ignore
+     * span[2] = 2; // spans 2 columns
+     * span[3] = 0; // spans 0 columns, ignore
      * </PRE>
      * <P>
      * A span value of 1 is the default. The method addTitle(String[] row)
@@ -264,20 +271,20 @@
      * @param row
      *            Array of strings to print in one row of title.
      * @param span
-     *            Array of integers that reflect the number of collumns the
+     *            Array of integers that reflect the number of columns the
      *            corresponding title string will occupy.
      */
     void addTitle(final String[] row, final int[] span) {
         // Need to create a new instance of it, otherwise the new values
         // will always overwrite the old values.
 
-        final String[] rowInstance = new String[(row.length)];
+        final String[] rowInstance = new String[row.length];
         for (int i = 0; i < row.length; i++) {
             rowInstance[i] = row[i];
         }
-        titleTable.addElement(rowInstance);
+        titleTable.add(rowInstance);
 
-        titleSpanTable.addElement(span);
+        titleSpanTable.add(span);
     }
 
     /**
@@ -331,11 +338,9 @@
         // Get the longest string for each column and store in curLength[]
 
         // Scan through title rows
-        Enumeration<String[]> elm = titleTable.elements();
-        Enumeration<int[]> spanEnum = titleSpanTable.elements();
-        while (elm.hasMoreElements()) {
-            final String[] row = elm.nextElement();
-            final int[] curSpan = spanEnum.nextElement();
+        Iterator<int[]> spanEnum = titleSpanTable.iterator();
+        for (String[] row : titleTable) {
+            final int[] curSpan = spanEnum.next();
 
             for (int i = 0; i < numCol; i++) {
                 // None of the fields should be null, but if it
@@ -347,8 +352,8 @@
                 int len = row[i].length();
 
                 /*
-                 * If a title string spans multiple collumns, then the space it
-                 * occupies in each collumn is at most len/span (since we have
+                 * If a title string spans multiple columns, then the space it
+                 * occupies in each column is at most len/span (since we have
                  * gap to take into account as well).
                  */
                 final int span = curSpan[i];
@@ -367,7 +372,7 @@
                         }
 
                         /*
-                         * Add remainder to last collumn in span to avoid
+                         * Add remainder to last column in span to avoid
                          * round-off errors.
                          */
                         curLength[(i + span) - 1] += rem;
@@ -377,12 +382,10 @@
         }
 
         printBorder();
-        elm = titleTable.elements();
-        spanEnum = titleSpanTable.elements();
 
-        while (elm.hasMoreElements()) {
-            final String[] row = elm.nextElement();
-            final int[] curSpan = spanEnum.nextElement();
+        spanEnum = titleSpanTable.iterator();
+        for (String[] row : titleTable) {
+            final int[] curSpan = spanEnum.next();
 
             for (int i = 0; i < numCol; i++) {
                 int availableSpace = 0;
diff --git a/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/PerformanceRunner.java b/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/PerformanceRunner.java
index 95a1224..0fed2b3 100644
--- a/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/PerformanceRunner.java
+++ b/opendj-sdk/opendj3/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/PerformanceRunner.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2011 ForgeRock AS.
+ *      Portions copyright 2011-2013 ForgeRock AS.
  */
 
 package com.forgerock.opendj.ldap.tools;
@@ -190,8 +190,6 @@
                 printer = null;
             }
 
-            final String[] strings = new String[numColumns];
-
             final long startTime = System.currentTimeMillis();
             long statTime = startTime;
             long gcDuration = 0;
@@ -235,6 +233,7 @@
                 recentDuration /= 1000.0;
                 averageDuration /= 1000.0;
 
+                final String[] strings = new String[numColumns];
                 strings[0] = String.format("%.1f", resultCount / recentDuration);
                 strings[1] = String.format("%.1f", totalResultCount / averageDuration);
 
@@ -349,6 +348,7 @@
             this.startTime = startTime;
         }
 
+        @Override
         public void handleErrorResult(final ErrorResultException error) {
             failedRecentCount.getAndIncrement();
             updateStats();
@@ -358,6 +358,7 @@
             }
         }
 
+        @Override
         public void handleResult(final S result) {
             successRecentCount.getAndIncrement();
             updateStats();
@@ -763,15 +764,17 @@
         argParser.addArgument(arguments);
     }
 
+    @Override
     public void handleConnectionClosed() {
         // Ignore
     }
 
+    @Override
     public synchronized void handleConnectionError(final boolean isDisconnectNotification,
             final ErrorResultException error) {
         if (!stopRequested) {
-            app.println(LocalizableMessage.raw("Error occurred on one or more " + "connections: "
-                    + error.getResult().toString()));
+            app.println(LocalizableMessage.raw("Error occurred on one or more connections: "
+                    + error.getResult()));
             if (error.getCause() != null && app.isVerbose()) {
                 error.getCause().printStackTrace(app.getErrorStream());
             }
@@ -779,6 +782,7 @@
         }
     }
 
+    @Override
     public void handleUnsolicitedNotification(final ExtendedResult notification) {
         // Ignore
     }
@@ -854,9 +858,7 @@
             stopRequested = true;
             app.println(LocalizableMessage.raw(e.getResult().getDiagnosticMessage()));
         } finally {
-            for (final Connection c : connections) {
-                c.close();
-            }
+            StaticUtils.closeSilently(connections);
         }
 
         return 0;

--
Gitblit v1.10.0