Code cleanup

This commit is contained in:
Christian Basler 2016-02-26 15:06:47 +01:00
parent bc68a5d3ec
commit 2a17e6024f
5 changed files with 73 additions and 66 deletions

View File

@ -16,8 +16,6 @@
package ch.dissem.bitmessage.utils; package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.payload.ObjectType;
/** /**
* Some utilities to handle strings. * Some utilities to handle strings.
* TODO: Probably this should be split in a GUI related and an SQL related utility class. * TODO: Probably this should be split in a GUI related and an SQL related utility class.

View File

@ -22,7 +22,6 @@ import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.payload.Msg; import ch.dissem.bitmessage.entity.payload.Msg;
import ch.dissem.bitmessage.entity.payload.ObjectType; import ch.dissem.bitmessage.entity.payload.ObjectType;
import ch.dissem.bitmessage.entity.payload.Pubkey; import ch.dissem.bitmessage.entity.payload.Pubkey;
import ch.dissem.bitmessage.entity.payload.V4Pubkey;
import ch.dissem.bitmessage.entity.valueobject.PrivateKey; import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.exception.DecryptionFailedException;
import ch.dissem.bitmessage.utils.TestBase; import ch.dissem.bitmessage.utils.TestBase;
@ -30,7 +29,6 @@ import ch.dissem.bitmessage.utils.TestUtils;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
import static org.junit.Assert.*; import static org.junit.Assert.*;

View File

@ -99,12 +99,12 @@ public class ConnectionOrganizer implements Runnable {
networkHandler.startConnection(c); networkHandler.startConnection(c);
} }
Thread.sleep(10000); Thread.sleep(10000);
} else if (initialConnection != null) { } else if (initialConnection == null) {
Thread.sleep(30000);
} else {
initialConnection.disconnect(); initialConnection.disconnect();
initialConnection = null; initialConnection = null;
Thread.sleep(10000); Thread.sleep(10000);
} else {
Thread.sleep(30000);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
networkHandler.stop(); networkHandler.stop();

View File

@ -47,9 +47,11 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
@Override @Override
public List<Label> getLabels() { public List<Label> getLabels() {
List<Label> result = new LinkedList<>(); List<Label> result = new LinkedList<>();
try (Connection connection = config.getConnection()) { try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label ORDER BY ord"); ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label ORDER BY ord")
) {
while (rs.next()) { while (rs.next()) {
result.add(getLabel(rs)); result.add(getLabel(rs));
} }
@ -74,10 +76,12 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
@Override @Override
public List<Label> getLabels(Label.Type... types) { public List<Label> getLabels(Label.Type... types) {
List<Label> result = new LinkedList<>(); List<Label> result = new LinkedList<>();
try (Connection connection = config.getConnection()) { try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label WHERE type IN (" + join(types) + ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label WHERE type IN (" + join(types) +
") ORDER BY ord"); ") ORDER BY ord")
) {
while (rs.next()) { while (rs.next()) {
result.add(getLabel(rs)); result.add(getLabel(rs));
} }
@ -98,9 +102,11 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
where += "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" + where += "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" +
"SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))"; "SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))";
try (Connection connection = config.getConnection()) { try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM Message WHERE " + where); ResultSet rs = stmt.executeQuery("SELECT count(*) FROM Message WHERE " + where)
) {
if (rs.next()) { if (rs.next()) {
return rs.getInt(1); return rs.getInt(1);
} }
@ -146,9 +152,12 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
private List<Plaintext> find(String where) { private List<Plaintext> find(String where) {
List<Plaintext> result = new LinkedList<>(); List<Plaintext> result = new LinkedList<>();
try (Connection connection = config.getConnection()) { try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, iv, type, sender, recipient, data, sent, received, status FROM Message WHERE " + where); ResultSet rs = stmt.executeQuery("SELECT id, iv, type, sender, recipient, data, sent, received, status " +
"FROM Message WHERE " + where)
) {
while (rs.next()) { while (rs.next()) {
byte[] iv = rs.getBytes("iv"); byte[] iv = rs.getBytes("iv");
InputStream data = rs.getBinaryStream("data"); InputStream data = rs.getBinaryStream("data");
@ -173,9 +182,11 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
private Collection<Label> findLabels(Connection connection, long messageId) { private Collection<Label> findLabels(Connection connection, long messageId) {
List<Label> result = new ArrayList<>(); List<Label> result = new ArrayList<>();
try { try (
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label WHERE id IN (SELECT label_id FROM Message_Label WHERE message_id=" + messageId + ")"); ResultSet rs = stmt.executeQuery("SELECT id, label, type, color FROM Label " +
"WHERE id IN (SELECT label_id FROM Message_Label WHERE message_id=" + messageId + ")")
) {
while (rs.next()) { while (rs.next()) {
result.add(getLabel(rs)); result.add(getLabel(rs));
} }
@ -209,16 +220,17 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
} }
// remove existing labels // remove existing labels
Statement stmt = connection.createStatement(); try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id=" + message.getId()); stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id=" + message.getId());
}
// save labels // save labels
PreparedStatement ps = connection.prepareStatement("INSERT INTO Message_Label VALUES (" + message.getId() + ", ?)"); try (PreparedStatement ps = connection.prepareStatement("INSERT INTO Message_Label VALUES (" +
message.getId() + ", ?)")) {
for (Label label : message.getLabels()) { for (Label label : message.getLabels()) {
ps.setLong(1, (Long) label.getId()); ps.setLong(1, (Long) label.getId());
ps.executeUpdate(); ps.executeUpdate();
} }
}
connection.commit(); connection.commit();
} catch (IOException | SQLException e) { } catch (IOException | SQLException e) {
try { try {
@ -234,9 +246,11 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
} }
private void insert(Connection connection, Plaintext message) throws SQLException, IOException { private void insert(Connection connection, Plaintext message) throws SQLException, IOException {
PreparedStatement ps = connection.prepareStatement( try (PreparedStatement ps = connection.prepareStatement(
"INSERT INTO Message (iv, type, sender, recipient, data, sent, received, status, initial_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", "INSERT INTO Message (iv, type, sender, recipient, data, sent, received, status, initial_hash) " +
Statement.RETURN_GENERATED_KEYS); "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
Statement.RETURN_GENERATED_KEYS)
) {
ps.setBytes(1, message.getInventoryVector() == null ? null : message.getInventoryVector().getHash()); ps.setBytes(1, message.getInventoryVector() == null ? null : message.getInventoryVector().getHash());
ps.setString(2, message.getType().name()); ps.setString(2, message.getType().name());
ps.setString(3, message.getFrom().getAddress()); ps.setString(3, message.getFrom().getAddress());
@ -248,16 +262,17 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
ps.setBytes(9, message.getInitialHash()); ps.setBytes(9, message.getInitialHash());
ps.executeUpdate(); ps.executeUpdate();
// get generated id // get generated id
ResultSet rs = ps.getGeneratedKeys(); try (ResultSet rs = ps.getGeneratedKeys()) {
rs.next(); rs.next();
message.setId(rs.getLong(1)); message.setId(rs.getLong(1));
} }
}
}
private void update(Connection connection, Plaintext message) throws SQLException, IOException { private void update(Connection connection, Plaintext message) throws SQLException, IOException {
PreparedStatement ps = connection.prepareStatement( try (PreparedStatement ps = connection.prepareStatement(
"UPDATE Message SET iv=?, sent=?, received=?, status=?, initial_hash=? WHERE id=?"); "UPDATE Message SET iv=?, sent=?, received=?, status=?, initial_hash=? WHERE id=?")) {
ps.setBytes(1, message.getInventoryVector() == null ? null : message.getInventoryVector().getHash()); ps.setBytes(1, message.getInventoryVector() == null ? null : message.getInventoryVector().getHash());
ps.setLong(2, message.getSent()); ps.setLong(2, message.getSent());
ps.setLong(3, message.getReceived()); ps.setLong(3, message.getReceived());
@ -266,13 +281,13 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
ps.setLong(6, (Long) message.getId()); ps.setLong(6, (Long) message.getId());
ps.executeUpdate(); ps.executeUpdate();
} }
}
@Override @Override
public void remove(Plaintext message) { public void remove(Plaintext message) {
try (Connection connection = config.getConnection()) { try (Connection connection = config.getConnection()) {
try {
connection.setAutoCommit(false); connection.setAutoCommit(false);
Statement stmt = connection.createStatement(); try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id = " + message.getId()); stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id = " + message.getId());
stmt.executeUpdate("DELETE FROM Message WHERE id = " + message.getId()); stmt.executeUpdate("DELETE FROM Message WHERE id = " + message.getId());
connection.commit(); connection.commit();

View File

@ -39,11 +39,7 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
* @author Christian Basler * @author Christian Basler
*/ */
public class WifImporter { public class WifImporter {
private final static Logger LOG = LoggerFactory.getLogger(WifImporter.class);
private final BitmessageContext ctx; private final BitmessageContext ctx;
private final Ini ini = new Ini();
private final List<BitmessageAddress> identities = new LinkedList<>(); private final List<BitmessageAddress> identities = new LinkedList<>();
public WifImporter(BitmessageContext ctx, File file) throws IOException { public WifImporter(BitmessageContext ctx, File file) throws IOException {