1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-21 19:42:05 +01:00

Introduce CloseableUtil

This commit is contained in:
Florian Schmaus 2018-08-15 17:25:22 +02:00
parent 1136e8a2e9
commit a00aa726fe
19 changed files with 109 additions and 256 deletions

3
resources/smacklint.sh Normal file
View file

@ -0,0 +1,3 @@
#!/usr/bin/env bash
find smack-integration-test -type f -path "*/src/main/java/*/smackx/*/package-info.java"

View file

@ -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;
}

View file

@ -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,

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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());
}

View file

@ -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);
}
}
}

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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();

View file

@ -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);
}

View file

@ -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);

View file

@ -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<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not write longs to file.", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close OutputStream.", e);
}
}
CloseableUtil.maybeClose(out, LOGGER);
}
}
@ -447,13 +442,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
LOGGER.log(Level.SEVERE, "Could not read long from file.", e);
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
}
}
CloseableUtil.maybeClose(in, LOGGER);
}
return l;
@ -481,15 +470,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not write bytes to file.", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close OutputStream.", e);
}
}
CloseableUtil.maybeClose(out, LOGGER);
}
}
@ -510,15 +492,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
b = null;
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not read bytes from file.", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
}
}
CloseableUtil.maybeClose(in, LOGGER);
}
return b;
@ -540,15 +515,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not write integers to file.", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close OutputStream.", e);
}
}
CloseableUtil.maybeClose(out, LOGGER);
}
}
@ -577,13 +545,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not read integers.", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
}
}
CloseableUtil.maybeClose(in, LOGGER);
}
return integers;

View file

@ -43,7 +43,7 @@ import org.pgpainless.util.BCUtil;
public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
private static final Logger LOGGER = Logger.getLogger(AbstractOpenPgpKeyStore.class.getName());
protected static final Logger LOGGER = Logger.getLogger(AbstractOpenPgpKeyStore.class.getName());
protected Map<BareJid, PGPPublicKeyRingCollection> publicKeyRingCollections = new HashMap<>();
protected Map<BareJid, PGPSecretKeyRingCollection> secretKeyRingCollections = new HashMap<>();

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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();
}
}

View file

@ -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