From 00e446104348c99b8ba2cb84aede8697d2b7550b Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 16 Feb 2018 16:57:08 +0100 Subject: [PATCH] Fix issues with Java 7 compatibility --- .../bitmessage/ports/NodeRegistryHelper.kt | 2 +- .../networking/nio/NioNetworkHandler.kt | 6 +-- .../bitmessage/repository/JdbcInventory.kt | 53 +++++++++++-------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt b/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt index 80b897b..ce9dc88 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt @@ -41,7 +41,7 @@ object NodeRegistryHelper { val line = scanner.nextLine().trim { it <= ' ' } if (line.startsWith("[stream")) { stream = java.lang.Long.parseLong(line.substring(8, line.lastIndexOf(']'))) - streamSet = HashSet() + streamSet = HashSet() result.put(stream, streamSet) } else if (streamSet != null && !line.isEmpty() && !line.startsWith("#")) { val portIndex = line.lastIndexOf(':') diff --git a/networking/src/main/kotlin/ch/dissem/bitmessage/networking/nio/NioNetworkHandler.kt b/networking/src/main/kotlin/ch/dissem/bitmessage/networking/nio/NioNetworkHandler.kt index c18a76d..fff2d2b 100644 --- a/networking/src/main/kotlin/ch/dissem/bitmessage/networking/nio/NioNetworkHandler.kt +++ b/networking/src/main/kotlin/ch/dissem/bitmessage/networking/nio/NioNetworkHandler.kt @@ -59,8 +59,8 @@ class NioNetworkHandler : NetworkHandler, InternalContext.ContextHolder { private var selector: Selector? = null private var serverChannel: ServerSocketChannel? = null private val connectionQueue = ConcurrentLinkedQueue() - private val connections = ConcurrentHashMap() - private val requestedObjects = ConcurrentHashMap(10000) + private val connections: MutableMap = ConcurrentHashMap() + private val requestedObjects: MutableMap = ConcurrentHashMap(10000) private var starter: Thread? = null @@ -74,7 +74,7 @@ class NioNetworkHandler : NetworkHandler, InternalContext.ContextHolder { channel.configureBlocking(false) val connection = Connection(ctx, SYNC, NetworkAddress.Builder().ip(server).port(port).stream(1).build(), - HashMap(), timeoutInSeconds) + HashMap(), timeoutInSeconds) while (channel.isConnected && !connection.isSyncFinished) { write(channel, connection.io) read(channel, connection.io) diff --git a/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcInventory.kt b/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcInventory.kt index 07d8efc..58d23fc 100644 --- a/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcInventory.kt +++ b/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcInventory.kt @@ -31,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { - private val cache = ConcurrentHashMap>() + private val cache: MutableMap> = ConcurrentHashMap() override fun getInventory(vararg streams: Long): List { val result = LinkedList() @@ -48,14 +48,15 @@ class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { if (result == null) { synchronized(cache) { if (cache[stream] == null) { - val map = ConcurrentHashMap() - cache.put(stream, map) + val map: MutableMap = ConcurrentHashMap() result = map + cache[stream] = map try { config.getConnection().use { connection -> connection.createStatement().use { stmt -> - stmt.executeQuery("SELECT hash, expires FROM Inventory " + - "WHERE expires > " + (now - 5 * MINUTE) + " AND stream = " + stream).use { rs -> + stmt.executeQuery( + "SELECT hash, expires FROM Inventory WHERE expires > ${now - 5 * MINUTE} AND stream = $stream" + ).use { rs -> while (rs.next()) { map.put(InventoryVector(rs.getBytes("hash")), rs.getLong("expires")) } @@ -71,7 +72,8 @@ class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { return result!! } - override fun getMissing(offer: List, vararg streams: Long): List = offer - streams.flatMap { getCache(it).keys } + override fun getMissing(offer: List, vararg streams: Long): List = + offer - streams.flatMap { getCache(it).keys } override fun getObject(vector: InventoryVector): ObjectMessage? { config.getConnection().use { connection -> @@ -81,7 +83,7 @@ class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { val data = rs.getBlob("data") return Factory.getObjectMessage(rs.getInt("version"), data.binaryStream, data.length().toInt()) } else { - LOG.info("Object requested that we don't have. IV: " + vector) + LOG.info("Object requested that we don't have. IV: $vector") return null } } @@ -106,7 +108,13 @@ class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { val result = LinkedList() while (rs.next()) { val data = rs.getBlob("data") - result.add(Factory.getObjectMessage(rs.getInt("version"), data.binaryStream, data.length().toInt())!!) + result.add( + Factory.getObjectMessage( + rs.getInt("version"), + data.binaryStream, + data.length().toInt() + )!! + ) } return result } @@ -120,21 +128,22 @@ class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { try { config.getConnection().use { connection -> - connection.prepareStatement("INSERT INTO Inventory " + "(hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)").use { ps -> - val iv = objectMessage.inventoryVector - LOG.trace("Storing object " + iv) - ps.setBytes(1, iv.hash) - ps.setLong(2, objectMessage.stream) - ps.setLong(3, objectMessage.expiresTime) - JdbcHelper.Companion.writeBlob(ps, 4, objectMessage) - ps.setLong(5, objectMessage.type) - ps.setLong(6, objectMessage.version) - ps.executeUpdate() - getCache(objectMessage.stream).put(iv, objectMessage.expiresTime) - } + connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)") + .use { ps -> + val iv = objectMessage.inventoryVector + LOG.trace("Storing object " + iv) + ps.setBytes(1, iv.hash) + ps.setLong(2, objectMessage.stream) + ps.setLong(3, objectMessage.expiresTime) + JdbcHelper.Companion.writeBlob(ps, 4, objectMessage) + ps.setLong(5, objectMessage.type) + ps.setLong(6, objectMessage.version) + ps.executeUpdate() + getCache(objectMessage.stream).put(iv, objectMessage.expiresTime) + } } } catch (e: SQLException) { - LOG.debug("Error storing object of type " + objectMessage.payload.javaClass.simpleName, e) + LOG.debug("Error storing object of type ${objectMessage.payload.javaClass.simpleName}", e) } catch (e: Exception) { LOG.error(e.message, e) } @@ -148,7 +157,7 @@ class JdbcInventory(config: JdbcConfig) : JdbcHelper(config), Inventory { try { config.getConnection().use { connection -> connection.createStatement().use { stmt -> - stmt.executeUpdate("DELETE FROM Inventory WHERE expires < " + (now - 5 * MINUTE)) + stmt.executeUpdate("DELETE FROM Inventory WHERE expires < ${now - 5 * MINUTE}") } } } catch (e: SQLException) {