Move tryToClose() methods into utility classes.

This commit is contained in:
akwizgran
2018-11-23 15:02:27 +00:00
parent c09abdb088
commit 868c61e5d6
15 changed files with 252 additions and 302 deletions

View File

@@ -18,8 +18,8 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.system.AndroidExecutor; import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.util.AndroidUtils; import org.briarproject.bramble.util.AndroidUtils;
import org.briarproject.bramble.util.IoUtils;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
@@ -51,7 +51,6 @@ import static android.bluetooth.BluetoothDevice.EXTRA_DEVICE;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress; import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
@MethodsNotNullByDefault @MethodsNotNullByDefault
@@ -161,11 +160,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
@Override @Override
void tryToClose(@Nullable BluetoothServerSocket ss) { void tryToClose(@Nullable BluetoothServerSocket ss) {
try { IoUtils.tryToClose(ss, LOG, WARNING);
if (ss != null) ss.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
} }
@Override @Override
@@ -195,7 +190,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
s.connect(); s.connect();
return wrapSocket(s); return wrapSocket(s);
} catch (IOException e) { } catch (IOException e) {
tryToClose(s); IoUtils.tryToClose(s, LOG, WARNING);
throw e; throw e;
} }
} }
@@ -268,14 +263,6 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
return addresses; return addresses;
} }
private void tryToClose(@Nullable Closeable c) {
try {
if (c != null) c.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
}
private class BluetoothStateReceiver extends BroadcastReceiver { private class BluetoothStateReceiver extends BroadcastReceiver {
@Override @Override

View File

@@ -8,12 +8,15 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException;
@NotNullByDefault @NotNullByDefault
public class IoUtils { public class IoUtils {
@@ -54,16 +57,35 @@ public class IoUtils {
out.flush(); out.flush();
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
tryToClose(in); tryToClose(in, LOG, WARNING);
tryToClose(out); tryToClose(out, LOG, WARNING);
} }
} }
private static void tryToClose(@Nullable Closeable c) { public static void tryToClose(@Nullable Closeable c, Logger logger,
Level level) {
try { try {
if (c != null) c.close(); if (c != null) c.close();
} catch (IOException e) { } catch (IOException e) {
// We did our best logException(logger, level, e);
}
}
public static void tryToClose(@Nullable Socket s, Logger logger,
Level level) {
try {
if (s != null) s.close();
} catch (IOException e) {
logException(logger, level, e);
}
}
public static void tryToClose(@Nullable ServerSocket ss, Logger logger,
Level level) {
try {
if (ss != null) ss.close();
} catch (IOException e) {
logException(logger, level, e);
} }
} }

View File

@@ -15,16 +15,23 @@ import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
/** /**
* Contains all the H2-specific code for the database. * Contains all the H2-specific code for the database.
*/ */
@NotNullByDefault @NotNullByDefault
class H2Database extends JdbcDatabase { class H2Database extends JdbcDatabase {
private static final Logger LOG = getLogger(H2Database.class.getName());
private static final String HASH_TYPE = "BINARY(32)"; private static final String HASH_TYPE = "BINARY(32)";
private static final String SECRET_TYPE = "BINARY(32)"; private static final String SECRET_TYPE = "BINARY(32)";
private static final String BINARY_TYPE = "BINARY"; private static final String BINARY_TYPE = "BINARY";
@@ -121,8 +128,8 @@ class H2Database extends JdbcDatabase {
s.close(); s.close();
c.close(); c.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
tryToClose(c); tryToClose(c, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }

View File

@@ -14,16 +14,24 @@ import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
/** /**
* Contains all the HSQLDB-specific code for the database. * Contains all the HSQLDB-specific code for the database.
*/ */
@NotNullByDefault @NotNullByDefault
class HyperSqlDatabase extends JdbcDatabase { class HyperSqlDatabase extends JdbcDatabase {
private static final Logger LOG =
getLogger(HyperSqlDatabase.class.getName());
private static final String HASH_TYPE = "BINARY(32)"; private static final String HASH_TYPE = "BINARY(32)";
private static final String SECRET_TYPE = "BINARY(32)"; private static final String SECRET_TYPE = "BINARY(32)";
private static final String BINARY_TYPE = "BINARY"; private static final String BINARY_TYPE = "BINARY";
@@ -72,8 +80,8 @@ class HyperSqlDatabase extends JdbcDatabase {
s.close(); s.close();
c.close(); c.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
tryToClose(c); tryToClose(c, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -122,8 +130,8 @@ class HyperSqlDatabase extends JdbcDatabase {
s.close(); s.close();
c.close(); c.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
tryToClose(c); tryToClose(c, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }

View File

@@ -72,6 +72,7 @@ import static org.briarproject.bramble.db.DatabaseConstants.LAST_COMPACTED_KEY;
import static org.briarproject.bramble.db.DatabaseConstants.MAX_COMPACTION_INTERVAL_MS; import static org.briarproject.bramble.db.DatabaseConstants.MAX_COMPACTION_INTERVAL_MS;
import static org.briarproject.bramble.db.DatabaseConstants.SCHEMA_VERSION_KEY; import static org.briarproject.bramble.db.DatabaseConstants.SCHEMA_VERSION_KEY;
import static org.briarproject.bramble.db.ExponentialBackoff.calculateExpiry; import static org.briarproject.bramble.db.ExponentialBackoff.calculateExpiry;
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
import static org.briarproject.bramble.util.LogUtils.logDuration; import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now; import static org.briarproject.bramble.util.LogUtils.now;
@@ -458,30 +459,6 @@ abstract class JdbcDatabase implements Database<Connection> {
mergeSettings(txn, s, DB_SETTINGS_NAMESPACE); mergeSettings(txn, s, DB_SETTINGS_NAMESPACE);
} }
private void tryToClose(@Nullable ResultSet rs) {
try {
if (rs != null) rs.close();
} catch (SQLException e) {
logException(LOG, WARNING, e);
}
}
protected void tryToClose(@Nullable Statement s) {
try {
if (s != null) s.close();
} catch (SQLException e) {
logException(LOG, WARNING, e);
}
}
protected void tryToClose(@Nullable Connection c) {
try {
if (c != null) c.close();
} catch (SQLException e) {
logException(LOG, WARNING, e);
}
}
private void createTables(Connection txn) throws DbException { private void createTables(Connection txn) throws DbException {
Statement s = null; Statement s = null;
try { try {
@@ -502,7 +479,7 @@ abstract class JdbcDatabase implements Database<Connection> {
s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_KEYS)); s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_KEYS));
s.close(); s.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -519,7 +496,7 @@ abstract class JdbcDatabase implements Database<Connection> {
s.executeUpdate(INDEX_STATUSES_BY_CONTACT_ID_TIMESTAMP); s.executeUpdate(INDEX_STATUSES_BY_CONTACT_ID_TIMESTAMP);
s.close(); s.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -566,11 +543,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} catch (SQLException e) { } catch (SQLException e) {
// Try to close the connection // Try to close the connection
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
try { tryToClose(txn, LOG, WARNING);
txn.close();
} catch (SQLException e1) {
logException(LOG, WARNING, e1);
}
// Whatever happens, allow the database to close // Whatever happens, allow the database to close
connectionsLock.lock(); connectionsLock.lock();
try { try {
@@ -659,8 +632,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return c; return c;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -681,7 +654,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -704,7 +677,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Create a status row for each message in the group // Create a status row for each message in the group
addStatus(txn, c, g, groupShared); addStatus(txn, c, g, groupShared);
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -735,8 +708,8 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -761,7 +734,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -810,7 +783,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -840,8 +813,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -873,7 +846,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -915,8 +888,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -935,7 +908,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1015,8 +988,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return keySetId; return keySetId;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1039,8 +1012,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1061,8 +1034,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1083,8 +1056,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1105,8 +1078,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1127,8 +1100,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1149,8 +1122,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1174,8 +1147,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return found; return found;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1198,8 +1171,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return count; return count;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1223,7 +1196,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1240,7 +1213,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1273,8 +1246,8 @@ abstract class JdbcDatabase implements Database<Connection> {
return new Contact(c, author, localAuthorId, alias, verified, return new Contact(c, author, localAuthorId, alias, verified,
active); active);
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1310,8 +1283,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return contacts; return contacts;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1333,8 +1306,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1371,8 +1344,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return contacts; return contacts;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1395,8 +1368,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return new Group(g, clientId, majorVersion, descriptor); return new Group(g, clientId, majorVersion, descriptor);
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1423,8 +1396,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return groups; return groups;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1449,8 +1422,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return v; return v;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1473,8 +1446,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return visible; return visible;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1505,8 +1478,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return localAuthor; return localAuthor;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1537,8 +1510,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return authors; return authors;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1566,8 +1539,8 @@ abstract class JdbcDatabase implements Database<Connection> {
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length); System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
return new Message(m, g, timestamp, body); return new Message(m, g, timestamp, body);
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1590,8 +1563,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1628,8 +1601,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if (intersection == null) throw new AssertionError(); if (intersection == null) throw new AssertionError();
return intersection; return intersection;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1661,8 +1634,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return all; return all;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1696,8 +1669,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return metadata; return metadata;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1720,8 +1693,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return metadata; return metadata;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1746,8 +1719,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return metadata; return metadata;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1776,8 +1749,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return statuses; return statuses;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1807,8 +1780,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return status; return status;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1837,8 +1810,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return dependencies; return dependencies;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1868,8 +1841,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return dependents; return dependents;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1891,8 +1864,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return state; return state;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1916,8 +1889,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1950,8 +1923,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -1975,8 +1948,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2014,8 +1987,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2048,8 +2021,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2076,8 +2049,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2106,8 +2079,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return nextSendTime; return nextSendTime;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2145,8 +2118,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return ids; return ids;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2168,8 +2141,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return s; return s;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2234,8 +2207,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return keys; return keys;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2254,7 +2227,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2281,7 +2254,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2308,7 +2281,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2338,7 +2311,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (rows != 1) throw new DbStateException(); if (rows != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2383,8 +2356,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if (rows != 1) throw new DbStateException(); if (rows != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2450,7 +2423,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
return added; return added;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2497,7 +2470,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (rows != 1) throw new DbStateException(); if (rows != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2516,7 +2489,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2535,7 +2508,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2554,7 +2527,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2571,7 +2544,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2587,7 +2560,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2615,7 +2588,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2632,7 +2605,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2648,7 +2621,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2667,7 +2640,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
return affected == 1; return affected == 1;
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2692,7 +2665,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (rows != 1) throw new DbStateException(); if (rows != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2709,7 +2682,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2730,7 +2703,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2749,7 +2722,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2767,7 +2740,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2785,7 +2758,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2804,7 +2777,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2834,7 +2807,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2860,7 +2833,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2913,7 +2886,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0) throw new DbStateException(); if (affected < 0) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2936,7 +2909,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2955,7 +2928,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected < 0 || affected > 1) throw new DbStateException(); if (affected < 0 || affected > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -2991,8 +2964,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs, LOG, WARNING);
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
@@ -3059,7 +3032,7 @@ abstract class JdbcDatabase implements Database<Connection> {
if (rows < 0 || rows > 1) throw new DbStateException(); if (rows < 0 || rows > 1) throw new DbStateException();
ps.close(); ps.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(ps); tryToClose(ps, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }

View File

@@ -0,0 +1,42 @@
package org.briarproject.bramble.db;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import static org.briarproject.bramble.util.LogUtils.logException;
@NotNullByDefault
class JdbcUtils {
static void tryToClose(@Nullable ResultSet rs, Logger logger, Level level) {
try {
if (rs != null) rs.close();
} catch (SQLException e) {
logException(logger, level, e);
}
}
static void tryToClose(@Nullable Statement s, Logger logger, Level level) {
try {
if (s != null) s.close();
} catch (SQLException e) {
logException(logger, level, e);
}
}
static void tryToClose(@Nullable Connection c, Logger logger, Level level) {
try {
if (c != null) c.close();
} catch (SQLException e) {
logException(logger, level, e);
}
}
}

View File

@@ -7,10 +7,8 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
class Migration38_39 implements Migration<Connection> { class Migration38_39 implements Migration<Connection> {
@@ -40,16 +38,8 @@ class Migration38_39 implements Migration<Connection> {
+ " ALTER COLUMN contactId" + " ALTER COLUMN contactId"
+ " SET NOT NULL"); + " SET NOT NULL");
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
private void tryToClose(@Nullable Statement s) {
try {
if (s != null) s.close();
} catch (SQLException e) {
logException(LOG, WARNING, e);
}
}
} }

View File

@@ -7,10 +7,8 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
class Migration39_40 implements Migration<Connection> { class Migration39_40 implements Migration<Connection> {
@@ -39,16 +37,8 @@ class Migration39_40 implements Migration<Connection> {
+ " ALTER COLUMN eta" + " ALTER COLUMN eta"
+ " SET NOT NULL"); + " SET NOT NULL");
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
private void tryToClose(@Nullable Statement s) {
try {
if (s != null) s.close();
} catch (SQLException e) {
logException(LOG, WARNING, e);
}
}
} }

View File

@@ -7,11 +7,9 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
class Migration40_41 implements Migration<Connection> { class Migration40_41 implements Migration<Connection> {
@@ -41,16 +39,8 @@ class Migration40_41 implements Migration<Connection> {
s.execute("ALTER TABLE contacts" s.execute("ALTER TABLE contacts"
+ dbTypes.replaceTypes(" ADD alias _STRING")); + dbTypes.replaceTypes(" ADD alias _STRING"));
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
throw new DbException(e); throw new DbException(e);
} }
} }
private void tryToClose(@Nullable Statement s) {
try {
if (s != null) s.close();
} catch (SQLException e) {
logException(LOG, WARNING, e);
}
}
} }

View File

@@ -4,12 +4,11 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportConnectionReader; import org.briarproject.bramble.api.plugin.TransportConnectionReader;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.IoUtils.tryToClose;
@NotNullByDefault @NotNullByDefault
class FileTransportReader implements TransportConnectionReader { class FileTransportReader implements TransportConnectionReader {
@@ -34,11 +33,7 @@ class FileTransportReader implements TransportConnectionReader {
@Override @Override
public void dispose(boolean exception, boolean recognised) { public void dispose(boolean exception, boolean recognised) {
try { tryToClose(in, LOG, WARNING);
in.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
plugin.readerFinished(file, exception, recognised); plugin.readerFinished(file, exception, recognised);
} }
} }

View File

@@ -4,12 +4,11 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportConnectionWriter; import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.IoUtils.tryToClose;
@NotNullByDefault @NotNullByDefault
class FileTransportWriter implements TransportConnectionWriter { class FileTransportWriter implements TransportConnectionWriter {
@@ -44,11 +43,7 @@ class FileTransportWriter implements TransportConnectionWriter {
@Override @Override
public void dispose(boolean exception) { public void dispose(boolean exception) {
try { tryToClose(out, LOG, WARNING);
out.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
plugin.writerFinished(file, exception); plugin.writerFinished(file, exception);
} }
} }

View File

@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection; import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.settings.Settings; import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.util.IoUtils;
import org.briarproject.bramble.util.StringUtils; import org.briarproject.bramble.util.StringUtils;
import java.io.IOException; import java.io.IOException;
@@ -35,7 +36,6 @@ import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS; import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS;
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS; import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS;
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED; import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress; import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
@NotNullByDefault @NotNullByDefault
@@ -293,11 +293,7 @@ class LanTcpPlugin extends TcpPlugin {
@Override @Override
public void close() { public void close() {
try { IoUtils.tryToClose(ss, LOG, WARNING);
ss.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
} }
} }

View File

@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback; import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection; import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.util.IoUtils;
import org.briarproject.bramble.util.StringUtils; import org.briarproject.bramble.util.StringUtils;
import java.io.IOException; import java.io.IOException;
@@ -153,13 +154,8 @@ abstract class TcpPlugin implements DuplexPlugin {
} }
protected void tryToClose(@Nullable ServerSocket ss) { protected void tryToClose(@Nullable ServerSocket ss) {
try { IoUtils.tryToClose(ss, LOG, WARNING);
if (ss != null) ss.close(); callback.transportDisabled();
} catch (IOException e) {
logException(LOG, WARNING, e);
} finally {
callback.transportDisabled();
}
} }
String getIpPortString(InetSocketAddress a) { String getIpPortString(InetSocketAddress a) {

View File

@@ -31,7 +31,6 @@ import org.briarproject.bramble.api.system.LocationUtils;
import org.briarproject.bramble.api.system.ResourceProvider; import org.briarproject.bramble.api.system.ResourceProvider;
import org.briarproject.bramble.util.IoUtils; import org.briarproject.bramble.util.IoUtils;
import java.io.Closeable;
import java.io.EOFException; import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -303,8 +302,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
IoUtils.copyAndClose(in, out); IoUtils.copyAndClose(in, out);
doneFile.createNewFile(); doneFile.createNewFile();
} catch (IOException e) { } catch (IOException e) {
tryToClose(in); IoUtils.tryToClose(in, LOG, WARNING);
tryToClose(out); IoUtils.tryToClose(out, LOG, WARNING);
throw new PluginException(e); throw new PluginException(e);
} }
} }
@@ -341,22 +340,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
return getClass().getClassLoader().getResourceAsStream("torrc"); return getClass().getClassLoader().getResourceAsStream("torrc");
} }
private void tryToClose(@Nullable Closeable c) {
try {
if (c != null) c.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
}
private void tryToClose(@Nullable Socket s) {
try {
if (s != null) s.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
}
private void listFiles(File f) { private void listFiles(File f) {
if (f.isDirectory()) { if (f.isDirectory()) {
File[] children = f.listFiles(); File[] children = f.listFiles();
@@ -378,7 +361,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
return b; return b;
} finally { } finally {
tryToClose(in); IoUtils.tryToClose(in, LOG, WARNING);
} }
} }
@@ -418,13 +401,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
private void tryToClose(@Nullable ServerSocket ss) { private void tryToClose(@Nullable ServerSocket ss) {
try { IoUtils.tryToClose(ss, LOG, WARNING);
if (ss != null) ss.close(); callback.transportDisabled();
} catch (IOException e) {
logException(LOG, WARNING, e);
} finally {
callback.transportDisabled();
}
} }
private void publishHiddenService(String port) { private void publishHiddenService(String port) {
@@ -593,7 +571,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
LOG.info("Could not connect to " + scrubOnion(bestOnion) LOG.info("Could not connect to " + scrubOnion(bestOnion)
+ ": " + e.toString()); + ": " + e.toString());
} }
tryToClose(s); IoUtils.tryToClose(s, LOG, WARNING);
return null; return null;
} }
} }

View File

@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.reporting.DevReporter;
import org.briarproject.bramble.util.IoUtils; import org.briarproject.bramble.util.IoUtils;
import org.briarproject.bramble.util.StringUtils; import org.briarproject.bramble.util.StringUtils;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@@ -26,13 +25,12 @@ import java.net.Socket;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.net.SocketFactory; import javax.net.SocketFactory;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.IoUtils.tryToClose;
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
@@ -66,7 +64,7 @@ class DevReporterImpl implements DevReporter, EventListener {
s.setSoTimeout(SOCKET_TIMEOUT); s.setSoTimeout(SOCKET_TIMEOUT);
return s; return s;
} catch (IOException e) { } catch (IOException e) {
tryToClose(s); tryToClose(s, LOG, WARNING);
throw e; throw e;
} }
} }
@@ -88,8 +86,7 @@ class DevReporterImpl implements DevReporter, EventListener {
writer.append(armoured); writer.append(armoured);
writer.flush(); writer.flush();
} finally { } finally {
if (writer != null) tryToClose(writer, LOG, WARNING);
writer.close();
} }
} }
@@ -121,27 +118,11 @@ class DevReporterImpl implements DevReporter, EventListener {
f.delete(); f.delete();
} catch (IOException e) { } catch (IOException e) {
LOG.log(WARNING, "Failed to send reports", e); LOG.log(WARNING, "Failed to send reports", e);
tryToClose(out); tryToClose(out, LOG, WARNING);
tryToClose(in); tryToClose(in, LOG, WARNING);
return; return;
} }
} }
LOG.info("Reports sent"); LOG.info("Reports sent");
} }
private void tryToClose(@Nullable Closeable c) {
try {
if (c != null) c.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
}
private void tryToClose(@Nullable Socket s) {
try {
if (s != null) s.close();
} catch (IOException e) {
logException(LOG, WARNING, e);
}
}
} }