You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/08/03 08:52:22 UTC

[09/20] james-project git commit: JAMES-2517 Use try-with-resource in more places

JAMES-2517 Use try-with-resource in more places


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3c275fe6
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3c275fe6
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3c275fe6

Branch: refs/heads/master
Commit: 3c275fe650fcd6b0ecb9840231a634d75966d58a
Parents: a58e7dd
Author: Benoit Tellier <bt...@linagora.com>
Authored: Fri Aug 3 11:00:29 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Fri Aug 3 15:51:21 2018 +0700

----------------------------------------------------------------------
 .../hbase/HBaseMailboxSessionMapperFactory.java |    7 +-
 .../mailbox/hbase/io/ChunkInputStream.java      |    9 +-
 .../mailbox/hbase/io/ChunkOutputStream.java     |    8 +-
 .../mailbox/hbase/mail/HBaseMailboxMapper.java  |  212 +-
 .../mailbox/hbase/mail/HBaseMessageMapper.java  |   75 +-
 .../mailbox/hbase/mail/HBaseUidProvider.java    |   24 +-
 .../org/apache/james/mailbox/jcr/JCRUtils.java  |    8 +-
 .../lucene/search/LuceneMessageSearchIndex.java | 2732 +++++++++---------
 .../maildir/user/MaildirSubscriptionMapper.java |   29 +-
 .../base/AutomaticallySentMailDetectorImpl.java |   13 +-
 .../james/transport/mailets/LogMessage.java     |    5 +-
 .../james/mpt/ant/MailProtocolTestTask.java     |   20 +-
 .../org/apache/james/server/core/MailImpl.java  |   13 +-
 .../apache/james/system/hbase/TablePool.java    |    5 +-
 .../mailets/JDBCRecipientRewriteTable.java      |   32 +-
 .../apache/james/queue/jms/JMSMailQueue.java    |   97 +-
 16 files changed, 1537 insertions(+), 1752 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/HBaseMailboxSessionMapperFactory.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/HBaseMailboxSessionMapperFactory.java b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/HBaseMailboxSessionMapperFactory.java
index db441ca..1c840af 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/HBaseMailboxSessionMapperFactory.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/HBaseMailboxSessionMapperFactory.java
@@ -36,7 +36,6 @@ import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.MasterNotRunningException;
 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
 import org.apache.hadoop.hbase.client.HBaseAdmin;
-import org.apache.hadoop.io.IOUtils;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.SubscriptionException;
@@ -82,9 +81,7 @@ public class HBaseMailboxSessionMapperFactory extends MailboxSessionMapperFactor
         this.messageIdFactory = messageIdFactory;
 
         //TODO: add better exception handling for this
-        HBaseAdmin hbaseAdmin = null;
-        try {
-            hbaseAdmin = new HBaseAdmin(conf);
+        try (HBaseAdmin hbaseAdmin = new HBaseAdmin(conf)) {
             HTableDescriptor desc = null;
             HColumnDescriptor hColumnDescriptor = null;
 
@@ -127,8 +124,6 @@ public class HBaseMailboxSessionMapperFactory extends MailboxSessionMapperFactor
 
         } catch (Exception e) {
             throw new RuntimeException(e);
-        } finally {
-            IOUtils.cleanup(null, hbaseAdmin);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkInputStream.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkInputStream.java b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkInputStream.java
index 277a0e5..76a5009 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkInputStream.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkInputStream.java
@@ -74,10 +74,8 @@ public class ChunkInputStream extends InputStream {
      * @throws IOException
      */
     private boolean fetchChunk() throws IOException {
-        HTable messages = null;
-        try {
+        try (HTable messages = new HTable(conf, tableName)) {
             byte[] cp = Bytes.toBytes(chunkPos);
-            messages = new HTable(conf, tableName);
             Get get = new Get(key);
             get.addColumn(cf, cp);
             get.setMaxVersions(1);
@@ -92,11 +90,6 @@ public class ChunkInputStream extends InputStream {
             }
         } catch (IOException e) {
             throw new IOException("Unable to read data", e);
-        } finally {
-            if (messages != null) {
-                messages.close();
-
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkOutputStream.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkOutputStream.java b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkOutputStream.java
index f66fa5b..4b5eae1 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkOutputStream.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/io/ChunkOutputStream.java
@@ -93,9 +93,7 @@ public class ChunkOutputStream extends OutputStream {
      */
     private void writeData(boolean close) throws IOException {
         if (pos != 0 && (close || pos == chunk.length - 1)) {
-            HTable messages = null;
-            try {
-                messages = new HTable(conf, tableName);
+            try (HTable messages = new HTable(conf, tableName)) {
                 Put put = new Put(key);
                 put.add(cf, Bytes.toBytes(chunkPos), Bytes.head(chunk, (int) pos + 1));
                 messages.put(put);
@@ -104,10 +102,6 @@ public class ChunkOutputStream extends OutputStream {
 
             } catch (IOException e) {
                 throw new IOException("Unable to write data", e);
-            } finally {
-                if (messages != null) {
-                    messages.close();
-                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
index b5926e4..847d682 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMailboxMapper.java
@@ -81,11 +81,9 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
     
     @Override
     public Mailbox findMailboxByPath(MailboxPath mailboxPath) throws MailboxException, MailboxNotFoundException {
-        HTable mailboxes = null;
-        ResultScanner scanner = null;
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
-            
+
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
+
             Scan scan = new Scan();
             scan.addFamily(MAILBOX_CF);
             scan.setCaching(mailboxes.getConfiguration().getInt("hbase.client.scanner.caching", 1) * 2);
@@ -107,35 +105,24 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
             filters.addFilter(nameFilter);
             SingleColumnValueFilter namespaceFilter = new SingleColumnValueFilter(MAILBOX_CF, MAILBOX_NAMESPACE, CompareOp.EQUAL, Bytes.toBytes(mailboxPath.getNamespace()));
             filters.addFilter(namespaceFilter);
-            
             scan.setFilter(filters);
-            scanner = mailboxes.getScanner(scan);
-            Result result = scanner.next();
-            
-            if (result == null) {
-                throw new MailboxNotFoundException(mailboxPath);
+
+            try (ResultScanner scanner = mailboxes.getScanner(scan)) {
+                Result result = scanner.next();
+                if (result == null) {
+                    throw new MailboxNotFoundException(mailboxPath);
+                }
+                return mailboxFromResult(result);
             }
-            return mailboxFromResult(result);
         } catch (IOException e) {
             throw new MailboxException("Search of mailbox " + mailboxPath + " failed", e);
-        } finally {
-            scanner.close();
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
 
     @Override
     public Mailbox findMailboxById(MailboxId id) throws MailboxException, MailboxNotFoundException {
         HBaseId mailboxId = (HBaseId)id;
-        HTable mailboxes = null;
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             Get get = new Get(mailboxId.toBytes());
             Result result = mailboxes.get(get);
             if (result == null) {
@@ -144,24 +131,12 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
             return mailboxFromResult(result);
         } catch (IOException ex) {
             throw new MailboxException("IOException in HBase cluster during get()", ex);
-        } finally {
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
     
     @Override
     public List<Mailbox> findMailboxWithPathLike(MailboxPath mailboxPath) throws MailboxException {
-        HTable mailboxes = null;
-        ResultScanner scanner = null;
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
-            
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             Scan scan = new Scan();
             scan.addFamily(MAILBOX_CF);
             scan.setCaching(mailboxes.getConfiguration().getInt("hbase.client.scanner.caching", 1) * 2);
@@ -195,59 +170,38 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
             filters.addFilter(namespaceFilter);
             
             scan.setFilter(filters);
-            scanner = mailboxes.getScanner(scan);
-            
-            List<Mailbox> mailboxList = new ArrayList<>();
-            
-            for (Result result : scanner) {
-                mailboxList.add(mailboxFromResult(result));
+            try (ResultScanner scanner = mailboxes.getScanner(scan)) {
+                List<Mailbox> mailboxList = new ArrayList<>();
+                for (Result result : scanner) {
+                    mailboxList.add(mailboxFromResult(result));
+                }
+                return mailboxList;
             }
-            return mailboxList;
         } catch (IOException e) {
             throw new MailboxException("Search of mailbox " + mailboxPath + " failed", e);
-        } finally {
-            scanner.close();
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
     
     @Override
     public List<Mailbox> list() throws MailboxException {
-        HTable mailboxes = null;
-        ResultScanner scanner = null;
         //TODO: possible performance isssues, we are creating an object from all the rows in HBase mailbox table
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             Scan scan = new Scan();
             scan.addFamily(MAILBOX_CF);
             scan.setCaching(mailboxes.getConfiguration().getInt("hbase.client.scanner.caching", 1) * 2);
             scan.setMaxVersions(1);
-            scanner = mailboxes.getScanner(scan);
-            List<Mailbox> mailboxList = new ArrayList<>();
-            
-            Result result;
-            while ((result = scanner.next()) != null) {
-                Mailbox mlbx = mailboxFromResult(result);
-                mailboxList.add(mlbx);
+
+            try (ResultScanner scanner = mailboxes.getScanner(scan)) {
+                List<Mailbox> mailboxList = new ArrayList<>();
+                Result result;
+                while ((result = scanner.next()) != null) {
+                    Mailbox mlbx = mailboxFromResult(result);
+                    mailboxList.add(mlbx);
+                }
+                return mailboxList;
             }
-            return mailboxList;
         } catch (IOException ex) {
             throw new MailboxException("HBase IOException in list()", ex);
-        } finally {
-            scanner.close();
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
     
@@ -258,9 +212,7 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
     @Override
     public MailboxId save(Mailbox mlbx) throws MailboxException {
         //TODO: maybe switch to checkAndPut for transactions
-        HTable mailboxes = null;
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             /*
              * cast to HBaseMailbox to access lastuid and ModSeq
              */
@@ -269,47 +221,25 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
             return mlbx.getMailboxId();
         } catch (IOException ex) {
             throw new MailboxException("IOExeption", ex);
-        } finally {
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
     
     @Override
     public void delete(Mailbox mlbx) throws MailboxException {
         //TODO: maybe switch to checkAndDelete
-        HTable mailboxes = null;
         HBaseId mailboxId = (HBaseId) mlbx.getMailboxId();
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             //TODO: delete all maessages from this mailbox
             Delete delete = new Delete(mailboxId.toBytes());
             mailboxes.delete(delete);
         } catch (IOException ex) {
             throw new MailboxException("IOException in HBase cluster during delete()", ex);
-        } finally {
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
     
     @Override
     public boolean hasChildren(Mailbox mailbox, char c) throws MailboxException, MailboxNotFoundException {
-        HTable mailboxes = null;
-        ResultScanner scanner = null;
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
-            
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             Scan scan = new Scan();
             scan.addFamily(MAILBOX_CF);
             scan.setCaching(mailboxes.getConfiguration().getInt("hbase.client.scanner.caching", 1) * 2);
@@ -330,8 +260,7 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
             filters.addFilter(namespaceFilter);
             
             scan.setFilter(filters);
-            scanner = mailboxes.getScanner(scan);
-            try {
+            try (ResultScanner scanner = mailboxes.getScanner(scan)) {
                 if (scanner.next() != null) {
                     return true;
                 }
@@ -341,57 +270,46 @@ public class HBaseMailboxMapper extends HBaseNonTransactionalMapper implements M
             return false;
         } catch (IOException e) {
             throw new MailboxException("Search of mailbox " + mailbox + " failed", e);
-        } finally {
-            scanner.close();
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
     
     public void deleteAllMemberships() {
-        HTable messages = null;
-        HTable mailboxes = null;
-        ResultScanner scanner = null;
-        try {
-            messages = new HTable(conf, MESSAGES_TABLE);
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
-            Scan scan = new Scan();
-            scan.setMaxVersions(1);
-            scan.addColumn(MESSAGES_META_CF, MESSAGE_INTERNALDATE);
-            scanner = messages.getScanner(scan);
-            Result result;
-            List<Delete> deletes = new ArrayList<>();
-            while ((result = scanner.next()) != null) {
-                deletes.add(new Delete(result.getRow()));
-            }
-            long totalDeletes = deletes.size();
-            messages.delete(deletes);
-            if (deletes.size() > 0) {
-                //TODO: what shoul we do if not all messages are deleted?
-                System.out.println("Just " + deletes.size() + " out of " + totalDeletes + " messages have been deleted");
-                //throw new RuntimeException("Just " + deletes.size() + " out of " + totalDeletes + " messages have been deleted");
-            }
-            List<Put> puts = new ArrayList<>();
-            scan = new Scan();
-            scan.setMaxVersions(1);
-            scan.addColumn(MAILBOX_CF, MAILBOX_MESSAGE_COUNT);
-            IOUtils.cleanup(null, scanner);
-            scanner = mailboxes.getScanner(scan);
-            Put put = null;
-            while ((result = scanner.next()) != null) {
-                put = new Put(result.getRow());
-                put.add(MAILBOX_CF, MAILBOX_MESSAGE_COUNT, Bytes.toBytes(0L));
-                puts.add(new Put());
+        try (HTable messages = new HTable(conf, MESSAGES_TABLE)) {
+            try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
+                Scan scan = new Scan();
+                scan.setMaxVersions(1);
+                scan.addColumn(MESSAGES_META_CF, MESSAGE_INTERNALDATE);
+                try (ResultScanner scanner = messages.getScanner(scan)) {
+                    Result result;
+                    List<Delete> deletes = new ArrayList<>();
+                    while ((result = scanner.next()) != null) {
+                        deletes.add(new Delete(result.getRow()));
+                    }
+                    long totalDeletes = deletes.size();
+                    messages.delete(deletes);
+                    if (deletes.size() > 0) {
+                        //TODO: what shoul we do if not all messages are deleted?
+                        System.out.println("Just " + deletes.size() + " out of " + totalDeletes + " messages have been deleted");
+                        //throw new RuntimeException("Just " + deletes.size() + " out of " + totalDeletes + " messages have been deleted");
+                    }
+                }
+
+                List<Put> puts = new ArrayList<>();
+                scan = new Scan();
+                scan.setMaxVersions(1);
+                scan.addColumn(MAILBOX_CF, MAILBOX_MESSAGE_COUNT);
+                try (ResultScanner scanner = mailboxes.getScanner(scan)) {
+                    Put put = null;
+                    Result result;
+                    while ((result = scanner.next()) != null) {
+                        put = new Put(result.getRow());
+                        put.add(MAILBOX_CF, MAILBOX_MESSAGE_COUNT, Bytes.toBytes(0L));
+                        puts.add(new Put());
+                    }
+                }
             }
         } catch (IOException e) {
             throw new RuntimeException("Error deleting MESSAGES table ", e);
-        } finally {
-            IOUtils.cleanup(null, scanner, messages, mailboxes);
         }
     }
     

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java
index e4f7c7a..d6d602f 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java
@@ -363,11 +363,9 @@ public class HBaseMessageMapper extends NonTransactionalMapper implements Messag
         /* TODO: see if it is possible to store the number of unseen messages in the mailbox table
          * and just return that value with a Get and kepp it up to date.
          */
-        HTable messages = null;
         ResultScanner scanner = null;
         HBaseId mailboxId = (HBaseId) mailbox.getMailboxId();
-        try {
-            messages = new HTable(conf, MESSAGES_TABLE);
+        try (HTable messages = new HTable(conf, MESSAGES_TABLE)) {
             /* Limit the number of entries scanned to just the mails in this mailbox */
             Scan scan = new Scan(
                     messageRowKey(mailboxId, MessageUid.MAX_VALUE),
@@ -382,13 +380,6 @@ public class HBaseMessageMapper extends NonTransactionalMapper implements Messag
             throw new MailboxException("Search of first unseen message failed in mailbox " + mailbox, e);
         } finally {
             scanner.close();
-            if (messages != null) {
-                try {
-                    messages.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + messages, ex);
-                }
-            }
         }
     }
 
@@ -433,11 +424,8 @@ public class HBaseMessageMapper extends NonTransactionalMapper implements Messag
 
     @Override
     public MessageUid findFirstUnseenMessageUid(Mailbox mailbox) throws MailboxException {
-        HTable messages = null;
-        ResultScanner scanner = null;
         HBaseId mailboxId = (HBaseId) mailbox.getMailboxId();
-        try {
-            messages = new HTable(conf, MESSAGES_TABLE);
+        try (HTable messages = new HTable(conf, MESSAGES_TABLE)) {
             /* Limit the number of entries scanned to just the mails in this mailbox */
             Scan scan = new Scan(
                     messageRowKey(mailboxId, MessageUid.MAX_VALUE), 
@@ -448,28 +436,20 @@ public class HBaseMessageMapper extends NonTransactionalMapper implements Messag
             scan.setFilter(filter);
             scan.setCaching(messages.getConfiguration().getInt("hbase.client.scanner.caching", 1) * 2);
             scan.setMaxVersions(1);
-            scanner = messages.getScanner(scan);
-            Result result;
-            MessageUid lastUnseen = null;
-            byte[] row = null;
-            while ((result = scanner.next()) != null) {
-                row = result.getRow();
-            }
-            if (row != null) {
-                lastUnseen = MessageUid.of(Long.MAX_VALUE - Bytes.toLong(row, 16, 8));
+            try (ResultScanner scanner = messages.getScanner(scan)) {
+                Result result;
+                MessageUid lastUnseen = null;
+                byte[] row = null;
+                while ((result = scanner.next()) != null) {
+                    row = result.getRow();
+                }
+                if (row != null) {
+                    lastUnseen = MessageUid.of(Long.MAX_VALUE - Bytes.toLong(row, 16, 8));
+                }
+                return lastUnseen;
             }
-            return lastUnseen;
         } catch (IOException e) {
             throw new MailboxException("Search of first unseen message failed in mailbox " + mailbox, e);
-        } finally {
-            scanner.close();
-            if (messages != null) {
-                try {
-                    messages.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + messages, ex);
-                }
-            }
         }
     }
 
@@ -478,11 +458,8 @@ public class HBaseMessageMapper extends NonTransactionalMapper implements Messag
         /** TODO: improve performance by implementing a last seen and last recent value per mailbox.
          * maybe one more call to HBase is less expensive than iterating throgh all rows.
          */
-        HTable messages = null;
-        ResultScanner scanner = null;
         HBaseId mailboxId = (HBaseId) mailbox.getMailboxId();
-        try {
-            messages = new HTable(conf, MESSAGES_TABLE);
+        try (HTable messages = new HTable(conf, MESSAGES_TABLE)) {
             /* Limit the number of entries scanned to just the mails in this mailbox */
             Scan scan = new Scan(
                     messageRowKey(mailboxId, MessageUid.MAX_VALUE),
@@ -494,25 +471,17 @@ public class HBaseMessageMapper extends NonTransactionalMapper implements Messag
             scan.setCaching(messages.getConfiguration().getInt("hbase.client.scanner.caching", 1) * 2);
             scan.setMaxVersions(1);
 
-            scanner = messages.getScanner(scan);
-            Result result;
-            List<MessageUid> uids = new ArrayList<>();
-            while ((result = scanner.next()) != null) {
-                uids.add(MessageUid.of(Long.MAX_VALUE - Bytes.toLong(result.getRow(), 16, 8)));
+            try (ResultScanner scanner = messages.getScanner(scan)) {
+                Result result;
+                List<MessageUid> uids = new ArrayList<>();
+                while ((result = scanner.next()) != null) {
+                    uids.add(MessageUid.of(Long.MAX_VALUE - Bytes.toLong(result.getRow(), 16, 8)));
+                }
+                Collections.reverse(uids);
+                return uids;
             }
-            Collections.reverse(uids);
-            return uids;
         } catch (IOException e) {
             throw new MailboxException("Search of recent messages failed in mailbox " + mailbox, e);
-        } finally {
-            scanner.close();
-            if (messages != null) {
-                try {
-                    messages.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + messages, ex);
-                }
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseUidProvider.java
----------------------------------------------------------------------
diff --git a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseUidProvider.java b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseUidProvider.java
index e547524..3f5b0db 100644
--- a/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseUidProvider.java
+++ b/mailbox/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseUidProvider.java
@@ -53,10 +53,8 @@ public class HBaseUidProvider implements UidProvider {
 
     @Override
     public Optional<MessageUid> lastUid(MailboxSession session, Mailbox mailbox) throws MailboxException {
-        HTable mailboxes = null;
         HBaseId mailboxId = (HBaseId) mailbox.getMailboxId();
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             Get get = new Get(mailboxId.toBytes());
             get.addColumn(MAILBOX_CF, MAILBOX_LASTUID);
             get.setMaxVersions(1);
@@ -72,14 +70,6 @@ public class HBaseUidProvider implements UidProvider {
             return Optional.of(MessageUid.of(rawUid));
         } catch (IOException e) {
             throw new MailboxException("lastUid", e);
-        } finally {
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
 
@@ -91,22 +81,12 @@ public class HBaseUidProvider implements UidProvider {
     @Override
     public MessageUid nextUid(MailboxSession session, MailboxId mailboxId) throws MailboxException {
         HBaseId hbaseId = (HBaseId) mailboxId;
-        HTable mailboxes = null;
-        try {
-            mailboxes = new HTable(conf, MAILBOXES_TABLE);
+        try (HTable mailboxes = new HTable(conf, MAILBOXES_TABLE)) {
             MessageUid newValue = MessageUid.of(mailboxes.incrementColumnValue(hbaseId.toBytes(), MAILBOX_CF, MAILBOX_LASTUID, 1));
             mailboxes.close();
             return newValue;
         } catch (IOException e) {
             throw new MailboxException("lastUid", e);
-        } finally {
-            if (mailboxes != null) {
-                try {
-                    mailboxes.close();
-                } catch (IOException ex) {
-                    throw new MailboxException("Error closing table " + mailboxes, ex);
-                }
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/3c275fe6/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRUtils.java
----------------------------------------------------------------------
diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRUtils.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRUtils.java
index 8c50d68..192b548 100644
--- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRUtils.java
+++ b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRUtils.java
@@ -64,14 +64,12 @@ public class JCRUtils implements JCRImapConstants {
     
     /**
      * Register the imap CND file 
-     * 
-     * @param session
      */
     public static void registerCnd(Session session) {
         // Register the custom node types defined in the CND file
-        InputStream is = Thread.currentThread().getContextClassLoader()
-                              .getResourceAsStream("mailbox-jcr.cnd");
-        try {
+        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+
+        try (InputStream is = contextClassLoader.getResourceAsStream("mailbox-jcr.cnd")) {
             CndImporter.registerNodeTypes(new InputStreamReader(is), session);
         } catch (Exception e) {
             throw new RuntimeException("Unable to register cnd file", e);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org