From a00aa726fe1b96929c9817821adb4fca6607a549 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 15 Aug 2018 17:25:22 +0200 Subject: [PATCH] Introduce CloseableUtil --- resources/smacklint.sh | 3 ++ .../smack/bosh/XMPPBOSHConnection.java | 28 +++------- .../smack/SmackInitialization.java | 19 +++---- .../smack/initializer/UrlInitializer.java | 12 +---- .../smack/provider/ProviderFileLoader.java | 8 +-- .../proxy/Socks5ProxySocketConnection.java | 22 +++----- .../smack/util/CloseableUtil.java | 37 +++++++++++++ .../bytestreams/socks5/Socks5Client.java | 8 +-- .../bytestreams/socks5/Socks5Proxy.java | 10 +--- .../filetransfer/IncomingFileTransfer.java | 18 ++----- .../filetransfer/OutgoingFileTransfer.java | 29 ++-------- .../packet/JivePropertiesExtension.java | 19 ++----- .../smackx/omemo/FileBasedOmemoStore.java | 54 +++---------------- .../store/abstr/AbstractOpenPgpKeyStore.java | 2 +- .../filebased/FileBasedOpenPgpKeyStore.java | 25 ++------- .../FileBasedOpenPgpMetadataStore.java | 28 +++------- .../filebased/FileBasedOpenPgpTrustStore.java | 25 ++------- .../util/dns/minidns/MiniDnsDaneVerifier.java | 11 ++-- .../smack/tcp/XMPPTCPConnection.java | 7 +-- 19 files changed, 109 insertions(+), 256 deletions(-) create mode 100644 resources/smacklint.sh create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/util/CloseableUtil.java diff --git a/resources/smacklint.sh b/resources/smacklint.sh new file mode 100644 index 000000000..815950c09 --- /dev/null +++ b/resources/smacklint.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +find smack-integration-test -type f -path "*/src/main/java/*/smackx/*/package-info.java" diff --git a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java index 756c2d885..441b2bdf6 100644 --- a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java +++ b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java @@ -41,6 +41,7 @@ import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.StanzaError; import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure; import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.PacketParserUtils; import org.igniterealtime.jbosh.AbstractBody; @@ -261,28 +262,13 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection { isFirstInitialization = false; // Close down the readers and writers. - if (readerPipe != null) { - try { - readerPipe.close(); - } - catch (Throwable ignore) { /* ignore */ } - reader = null; - } - if (reader != null) { - try { - reader.close(); - } - catch (Throwable ignore) { /* ignore */ } - reader = null; - } - if (writer != null) { - try { - writer.close(); - } - catch (Throwable ignore) { /* ignore */ } - writer = null; - } + CloseableUtil.maybeClose(readerPipe, LOGGER); + CloseableUtil.maybeClose(reader, LOGGER); + CloseableUtil.maybeClose(writer, LOGGER); + readerPipe = null; + reader = null; + writer = null; readerConsumer = null; } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java index b3a11b8a6..f6807512b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java @@ -18,7 +18,6 @@ package org.jivesoftware.smack; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collection; @@ -37,6 +36,7 @@ import org.jivesoftware.smack.sasl.core.SASLAnonymous; import org.jivesoftware.smack.sasl.core.SASLXOauth2Mechanism; import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism; import org.jivesoftware.smack.sasl.core.ScramSha1PlusMechanism; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.StringUtils; @@ -61,17 +61,15 @@ public final class SmackInitialization { */ static { String smackVersion; + BufferedReader reader = null; try { - BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8)); + reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8)); smackVersion = reader.readLine(); - try { - reader.close(); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "IOException closing stream", e); - } } catch (Exception e) { LOGGER.log(Level.SEVERE, "Could not determine Smack version", e); smackVersion = "unknown"; + } finally { + CloseableUtil.maybeClose(reader, LOGGER); } SMACK_VERSION = smackVersion; @@ -146,12 +144,7 @@ public final class SmackInitialization { eventType = parser.next(); } while (eventType != XmlPullParser.END_DOCUMENT); - try { - cfgFileStream.close(); - } - catch (IOException e) { - LOGGER.log(Level.SEVERE, "Error while closing config file input stream", e); - } + CloseableUtil.maybeClose(cfgFileStream, LOGGER); } private static void parseClassesToLoad(XmlPullParser parser, boolean optional, diff --git a/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java b/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java index c7c4440e8..ec7b047e7 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java @@ -16,7 +16,6 @@ */ package org.jivesoftware.smack.initializer; -import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.LinkedList; @@ -27,6 +26,7 @@ import java.util.logging.Logger; import org.jivesoftware.smack.SmackInitialization; import org.jivesoftware.smack.provider.ProviderFileLoader; import org.jivesoftware.smack.provider.ProviderManager; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.FileUtils; /** @@ -86,14 +86,6 @@ public abstract class UrlInitializer implements SmackInitializer { } private static void maybeClose(InputStream is) { - if (is == null) { - return; - } - try { - is.close(); - } - catch (IOException e) { - LOGGER.log(Level.WARNING, "Could not close input stream", e); - } + CloseableUtil.maybeClose(is, LOGGER); } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java index ddba7a6e9..a3c9fec17 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java @@ -26,6 +26,7 @@ import java.util.logging.Logger; import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.CloseableUtil; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; @@ -141,12 +142,7 @@ public class ProviderFileLoader implements ProviderLoader { exceptions.add(e); } finally { - try { - providerStream.close(); - } - catch (Exception e) { - LOGGER.log(Level.WARNING, "Exception closing provider stream", e); - } + CloseableUtil.maybeClose(providerStream, LOGGER); } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/proxy/Socks5ProxySocketConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/proxy/Socks5ProxySocketConnection.java index b3f62f19f..2380f3f86 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/proxy/Socks5ProxySocketConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/proxy/Socks5ProxySocketConnection.java @@ -21,7 +21,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; +import java.util.logging.Logger; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.StringUtils; /** @@ -30,6 +32,8 @@ import org.jivesoftware.smack.util.StringUtils; * @author Atul Aggarwal */ public class Socks5ProxySocketConnection implements ProxySocketConnection { + private static final Logger LOGGER = Logger.getLogger(Socks5ProxySocketConnection.class.getName()); + private final ProxyInfo proxy; Socks5ProxySocketConnection(ProxyInfo proxy) { @@ -164,11 +168,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection { } if (!check) { - try { - socket.close(); - } - catch (Exception eee) { - } + CloseableUtil.maybeClose(socket, LOGGER); throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "fail in SOCKS5 proxy"); } @@ -253,11 +253,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection { fill(in, buf, 4); if (buf[1] != 0) { - try { - socket.close(); - } - catch (Exception eee) { - } + CloseableUtil.maybeClose(socket, LOGGER); throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "server returns " + buf[1]); } @@ -280,11 +276,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection { throw e; } catch (Exception e) { - try { - socket.close(); - } - catch (Exception eee) { - } + CloseableUtil.maybeClose(socket, LOGGER); // TODO convert to IOException(e) when minimum Android API level is 9 or higher throw new IOException(e.getLocalizedMessage()); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/CloseableUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/CloseableUtil.java new file mode 100644 index 000000000..e76766d3c --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/CloseableUtil.java @@ -0,0 +1,37 @@ +/** + * + * Copyright 2018 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.util; + +import java.io.Closeable; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class CloseableUtil { + + public static void maybeClose(Closeable closable, Logger logger) { + if (closable == null) { + return; + } + + try { + closable.close(); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not close " + closable, e); + } + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java index 1e58b9c9f..556957e69 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java @@ -29,11 +29,11 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost; @@ -97,11 +97,7 @@ public class Socks5Client { } catch (SmackException e) { if (!socket.isClosed()) { - try { - socket.close(); - } catch (IOException e2) { - LOGGER.log(Level.WARNING, "Could not close SOCKS5 socket", e2); - } + CloseableUtil.maybeClose(socket, LOGGER); } throw e; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java index b39527ee3..4e4a42d26 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java @@ -38,6 +38,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.StringUtils; /** @@ -409,14 +410,7 @@ public final class Socks5Proxy { */ } catch (Exception e) { - try { - if (socket != null) { - socket.close(); - } - } - catch (IOException e1) { - /* do nothing */ - } + CloseableUtil.maybeClose(socket, LOGGER); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java index a5a5e0dd5..b8f058370 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java @@ -27,12 +27,12 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.util.CloseableUtil; /** @@ -158,20 +158,8 @@ public class IncomingFileTransfer extends FileTransfer { if (getStatus().equals(Status.in_progress)) { setStatus(Status.complete); } - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Closing input stream", e); - } - } - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Closing output stream", e); - } - } + CloseableUtil.maybeClose(inputStream, LOGGER); + CloseableUtil.maybeClose(outputStream, LOGGER); } }, "File Transfer " + streamID); transferThread.start(); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java index 6de904d01..41f55d4b8 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java @@ -22,7 +22,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; @@ -30,6 +29,7 @@ import org.jivesoftware.smack.SmackException.IllegalStateChangeException; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.packet.StanzaError; +import org.jivesoftware.smack.util.CloseableUtil; import org.jxmpp.jid.Jid; @@ -258,19 +258,8 @@ public class OutgoingFileTransfer extends FileTransfer { setStatus(FileTransfer.Status.error); setException(e); } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Closing input stream", e); - } - } - - try { - outputStream.close(); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Closing output stream", e); - } + CloseableUtil.maybeClose(inputStream, LOGGER); + CloseableUtil.maybeClose(outputStream, LOGGER); } updateStatus(Status.in_progress, FileTransfer.Status.complete); } @@ -325,16 +314,8 @@ public class OutgoingFileTransfer extends FileTransfer { setStatus(FileTransfer.Status.error); setException(e); } finally { - try { - if (in != null) { - in.close(); - } - - outputStream.flush(); - outputStream.close(); - } catch (IOException e) { - /* Do Nothing */ - } + CloseableUtil.maybeClose(in, LOGGER); + CloseableUtil.maybeClose(outputStream, LOGGER); } updateStatus(Status.in_progress, FileTransfer.Status.complete); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java index b61da73a4..2d4f02242 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java @@ -29,6 +29,7 @@ import java.util.logging.Logger; import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.stringencoder.Base64; @@ -191,22 +192,8 @@ public class JivePropertiesExtension implements ExtensionElement { valueStr = "Serializing error: " + e.getMessage(); } finally { - if (out != null) { - try { - out.close(); - } - catch (Exception e) { - // Ignore. - } - } - if (byteStream != null) { - try { - byteStream.close(); - } - catch (Exception e) { - // Ignore. - } - } + CloseableUtil.maybeClose(out, LOGGER); + CloseableUtil.maybeClose(byteStream, LOGGER); } } xml.attribute("type", type); diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/FileBasedOmemoStore.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/FileBasedOmemoStore.java index b7c5b00c9..4ecefcda7 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/FileBasedOmemoStore.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/FileBasedOmemoStore.java @@ -35,6 +35,8 @@ import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; +import org.jivesoftware.smack.util.CloseableUtil; + import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException; import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList; import org.jivesoftware.smackx.omemo.internal.OmemoDevice; @@ -417,15 +419,8 @@ public abstract class FileBasedOmemoStore publicKeyRingCollections = new HashMap<>(); protected Map secretKeyRingCollections = new HashMap<>(); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java index 108b796a4..4dc8da07d 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java @@ -27,6 +27,7 @@ import java.io.OutputStream; import java.util.Date; import java.util.Map; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore; @@ -82,16 +83,8 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { try { outputStream = prepareFileOutputStream(file); publicKeys.encode(outputStream); - outputStream.close(); - } catch (IOException e) { - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException ignored) { - // Don't care - } - } - throw e; + } finally { + CloseableUtil.maybeClose(outputStream, LOGGER); } } @@ -113,16 +106,8 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { try { outputStream = prepareFileOutputStream(file); secretKeys.encode(outputStream); - outputStream.close(); - } catch (IOException e) { - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException ignored) { - // Don't care - } - } - throw e; + } finally { + CloseableUtil.maybeClose(outputStream, LOGGER); } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java index b4ea4506d..88b4df546 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore; import org.jivesoftware.smackx.ox.util.FileUtils; @@ -110,18 +111,9 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore } } - reader.close(); return fingerprintDateMap; - - } catch (IOException e) { - if (reader != null) { - try { - reader.close(); - } catch (IOException ignored) { - // Don't care - } - } - throw e; + } finally { + CloseableUtil.maybeClose(reader, LOGGER); } } @@ -166,17 +158,8 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore writer.write(line); writer.newLine(); } - writer.flush(); - writer.close(); - } catch (IOException e) { - if (writer != null) { - try { - writer.close(); - } catch (IOException ignored) { - // Don't care - } - } - throw e; + } finally { + CloseableUtil.maybeClose(writer, LOGGER); } } @@ -184,6 +167,7 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), ANNOUNCED); } + // TODO: This method appears to be unused. Remove it? private File getRetrievedFingerprintsPath(BareJid contact) { return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), RETRIEVED); } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java index 07df98f62..c72b85ffb 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java @@ -28,6 +28,7 @@ import java.io.OutputStreamWriter; import java.util.logging.Level; import java.util.logging.Logger; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore; import org.jivesoftware.smackx.ox.util.FileUtils; @@ -82,21 +83,14 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { file.getAbsolutePath()); } } - reader.close(); return trust != null ? trust : Trust.undecided; } catch (IOException e) { - if (reader != null) { - try { - reader.close(); - } catch (IOException ignored) { - // Don't care - } - } - if (e instanceof FileNotFoundException) { return Trust.undecided; } throw e; + } finally { + CloseableUtil.maybeClose(reader, LOGGER); } } @@ -134,17 +128,8 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { writer = new BufferedWriter(osw); writer.write(trust.toString()); - writer.flush(); - writer.close(); - } catch (IOException e) { - if (writer != null) { - try { - writer.close(); - } catch (IOException ignored) { - // Don't care - } - } - throw e; + } finally { + CloseableUtil.maybeClose(writer, LOGGER); } } diff --git a/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsDaneVerifier.java b/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsDaneVerifier.java index 40058304c..d73a332c8 100644 --- a/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsDaneVerifier.java +++ b/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsDaneVerifier.java @@ -1,6 +1,6 @@ /** * - * Copyright 2015-2016 Florian Schmaus + * Copyright 2015-2018 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,9 @@ */ package org.jivesoftware.smack.util.dns.minidns; -import java.io.IOException; import java.security.KeyManagementException; import java.security.SecureRandom; import java.security.cert.CertificateException; -import java.util.logging.Level; import java.util.logging.Logger; import javax.net.ssl.KeyManager; @@ -29,6 +27,7 @@ import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.dns.SmackDaneVerifier; import org.minidns.dane.DaneVerifier; @@ -64,11 +63,7 @@ public class MiniDnsDaneVerifier implements SmackDaneVerifier { // DANE verification was successful, but according to the TLSA RR we also must perform PKIX validation. if (expectingTrustManager.hasException()) { // PKIX validation has failed. Throw an exception but close the socket first. - try { - sslSocket.close(); - } catch (IOException e) { - LOGGER.log(Level.FINER, "Closing TLS socket failed", e); - } + CloseableUtil.maybeClose(sslSocket, LOGGER); throw expectingTrustManager.getException(); } } diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 5d76ce380..e8fdd9317 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -125,6 +125,7 @@ import org.jivesoftware.smack.sm.predicates.Predicate; import org.jivesoftware.smack.sm.provider.ParseStreamManagement; import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown; import org.jivesoftware.smack.util.Async; +import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.DNSUtil; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.StringUtils; @@ -506,11 +507,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { } LOGGER.finer("PacketReader has been shut down"); - try { - socket.close(); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "shutdown", e); - } + CloseableUtil.maybeClose(socket, LOGGER); setWasAuthenticated(); // If we are able to resume the stream, then don't set