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;
import ch.dissem.bitmessage.entity.payload.ObjectType;
/**
* Some utilities to handle strings.
* 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.ObjectType;
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.exception.DecryptionFailedException;
import ch.dissem.bitmessage.utils.TestBase;
@ -30,7 +29,6 @@ import ch.dissem.bitmessage.utils.TestUtils;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import static org.junit.Assert.*;

View File

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

View File

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

View File

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