Add CloseableInjector

I'm going to try to minimise use of this (as per
https://github.com/google/guice/wiki/Avoid-Injecting-Closable-Resources).
For example, I'm going to inject a pooling DataSource rather than
Connection objects, as per the advice at the end of the page. However,
HikariCP's DataSource implementation is itself Closeable.

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent a0f7bf3922
commit c5bd044574
  1. 1
      archive/build.gradle.kts
  2. 4
      archive/src/main/kotlin/org/openrs2/archive/cache/DownloadCommand.kt
  3. 4
      archive/src/main/kotlin/org/openrs2/archive/cache/ExportCommand.kt
  4. 4
      archive/src/main/kotlin/org/openrs2/archive/cache/ImportCommand.kt
  5. 4
      archive/src/main/kotlin/org/openrs2/archive/cache/ImportMasterIndexCommand.kt
  6. 5
      archive/src/main/kotlin/org/openrs2/archive/key/BruteForceCommand.kt
  7. 4
      archive/src/main/kotlin/org/openrs2/archive/key/DownloadCommand.kt
  8. 5
      archive/src/main/kotlin/org/openrs2/archive/key/ImportCommand.kt
  9. 4
      archive/src/main/kotlin/org/openrs2/archive/name/DownloadCommand.kt
  10. 4
      archive/src/main/kotlin/org/openrs2/archive/name/GenerateCommand.kt
  11. 4
      archive/src/main/kotlin/org/openrs2/archive/name/ImportCommand.kt
  12. 4
      archive/src/main/kotlin/org/openrs2/archive/web/WebCommand.kt
  13. 1
      deob-ast/build.gradle.kts
  14. 4
      deob-ast/src/main/kotlin/org/openrs2/deob/ast/DeobfuscateAstCommand.kt
  15. 1
      deob-bytecode/build.gradle.kts
  16. 4
      deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/DeobfuscateBytecodeCommand.kt
  17. 1
      deob-processor/build.gradle.kts
  18. 11
      deob-processor/src/main/kotlin/org/openrs2/deob/processor/NameMapProcessor.kt
  19. 1
      deob/build.gradle.kts
  20. 4
      deob/src/main/kotlin/org/openrs2/deob/DeobfuscateCommand.kt
  21. 2
      game/build.gradle.kts
  22. 4
      game/src/main/kotlin/org/openrs2/game/GameCommand.kt
  23. 10
      inject/build.gradle.kts
  24. 23
      inject/src/main/kotlin/org/openrs2/inject/CloseableInjector.kt
  25. 1
      patcher/build.gradle.kts
  26. 4
      patcher/src/main/kotlin/org/openrs2/patcher/PatchCommand.kt
  27. 1
      settings.gradle.kts

@ -16,6 +16,7 @@ dependencies {
implementation(project(":cli"))
implementation(project(":db"))
implementation(project(":http"))
implementation(project(":inject"))
implementation(project(":json"))
implementation(project(":net"))
implementation(project(":protocol"))

@ -6,13 +6,15 @@ import com.github.ajalt.clikt.parameters.arguments.default
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class DownloadCommand : CliktCommand(name = "download") {
private val game by argument().default("oldschool")
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val downloader = injector.getInstance(CacheDownloader::class.java)
downloader.download(game)
}
}
}

@ -8,6 +8,7 @@ import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.cache.DiskStore
import org.openrs2.inject.CloseableInjector
public class ExportCommand : CliktCommand(name = "export") {
private val id by argument().int()
@ -19,7 +20,7 @@ public class ExportCommand : CliktCommand(name = "export") {
)
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val exporter = injector.getInstance(CacheExporter::class.java)
DiskStore.create(output).use { store ->
@ -27,3 +28,4 @@ public class ExportCommand : CliktCommand(name = "export") {
}
}
}
}

@ -10,6 +10,7 @@ import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.cache.Store
import org.openrs2.cli.instant
import org.openrs2.inject.CloseableInjector
public class ImportCommand : CliktCommand(name = "import") {
private val build by option().int()
@ -25,7 +26,7 @@ public class ImportCommand : CliktCommand(name = "import") {
)
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(CacheImporter::class.java)
Store.open(input).use { store ->
@ -33,3 +34,4 @@ public class ImportCommand : CliktCommand(name = "import") {
}
}
}
}

@ -13,6 +13,7 @@ import org.openrs2.archive.ArchiveModule
import org.openrs2.buffer.use
import org.openrs2.cache.MasterIndexFormat
import org.openrs2.cli.instant
import org.openrs2.inject.CloseableInjector
import java.nio.file.Files
public class ImportMasterIndexCommand : CliktCommand(name = "import-master-index") {
@ -30,7 +31,7 @@ public class ImportMasterIndexCommand : CliktCommand(name = "import-master-index
)
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(CacheImporter::class.java)
Unpooled.wrappedBuffer(Files.readAllBytes(input)).use { buf ->
@ -38,3 +39,4 @@ public class ImportMasterIndexCommand : CliktCommand(name = "import-master-index
}
}
}
}

@ -4,12 +4,13 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class BruteForceCommand : CliktCommand(name = "brute-force") {
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val bruteForcer = injector.getInstance(KeyBruteForcer::class.java)
bruteForcer.bruteForce()
}
}
}

@ -4,11 +4,13 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class DownloadCommand : CliktCommand(name = "download") {
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(KeyImporter::class.java)
importer.download()
}
}
}

@ -6,6 +6,7 @@ import com.github.ajalt.clikt.parameters.types.path
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class ImportCommand : CliktCommand(name = "import") {
private val input by argument().path(
@ -14,9 +15,9 @@ public class ImportCommand : CliktCommand(name = "import") {
)
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(KeyImporter::class.java)
importer.import(input)
}
}
}

@ -4,11 +4,13 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class DownloadCommand : CliktCommand(name = "download") {
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(NameImporter::class.java)
importer.download()
}
}
}

@ -4,10 +4,11 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class GenerateCommand : CliktCommand(name = "generate") {
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(NameImporter::class.java)
val names = mutableSetOf<String>()
@ -22,6 +23,7 @@ public class GenerateCommand : CliktCommand(name = "generate") {
importer.import(names)
}
}
private companion object {
private val PREFIXES = setOf("m", "um", "l", "ul", "n")

@ -7,12 +7,13 @@ import com.github.ajalt.clikt.parameters.types.inputStream
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class ImportCommand : CliktCommand(name = "import") {
private val input by option().inputStream().defaultStdin()
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val importer = injector.getInstance(NameImporter::class.java)
input.bufferedReader().useLines { lines ->
@ -20,3 +21,4 @@ public class ImportCommand : CliktCommand(name = "import") {
}
}
}
}

@ -3,11 +3,13 @@ package org.openrs2.archive.web
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import org.openrs2.archive.ArchiveModule
import org.openrs2.inject.CloseableInjector
public class WebCommand : CliktCommand(name = "web") {
override fun run() {
val injector = Guice.createInjector(ArchiveModule)
CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector ->
val server = injector.getInstance(WebServer::class.java)
server.start()
}
}
}

@ -13,6 +13,7 @@ dependencies {
api("com.github.ajalt.clikt:clikt:${Versions.clikt}")
api("com.google.inject:guice:${Versions.guice}")
implementation(project(":inject"))
implementation(project(":util"))
implementation("com.github.javaparser:javaparser-symbol-solver-core:${Versions.javaParser}")
implementation("com.google.guava:guava:${Versions.guava}")

@ -3,13 +3,15 @@ package org.openrs2.deob.ast
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import org.openrs2.deob.util.Module
import org.openrs2.inject.CloseableInjector
public fun main(args: Array<String>): Unit = DeobfuscateAstCommand().main(args)
public class DeobfuscateAstCommand : CliktCommand(name = "ast") {
override fun run() {
val injector = Guice.createInjector(AstDeobfuscatorModule)
CloseableInjector(Guice.createInjector(AstDeobfuscatorModule)).use { injector ->
val deobfuscator = injector.getInstance(AstDeobfuscator::class.java)
deobfuscator.run(Module.ALL)
}
}
}

@ -14,6 +14,7 @@ dependencies {
implementation(project(":deob-annotations"))
implementation(project(":deob-util"))
implementation(project(":inject"))
implementation(project(":patcher"))
implementation(project(":yaml"))
implementation("com.google.guava:guava:${Versions.guava}")

@ -2,13 +2,14 @@ package org.openrs2.deob.bytecode
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import org.openrs2.inject.CloseableInjector
import java.nio.file.Path
public fun main(args: Array<String>): Unit = DeobfuscateBytecodeCommand().main(args)
public class DeobfuscateBytecodeCommand : CliktCommand(name = "bytecode") {
override fun run() {
val injector = Guice.createInjector(BytecodeDeobfuscatorModule)
CloseableInjector(Guice.createInjector(BytecodeDeobfuscatorModule)).use { injector ->
val deobfuscator = injector.getInstance(BytecodeDeobfuscator::class.java)
deobfuscator.run(
input = Path.of("nonfree/lib"),
@ -16,3 +17,4 @@ public class DeobfuscateBytecodeCommand : CliktCommand(name = "bytecode") {
)
}
}
}

@ -6,6 +6,7 @@ plugins {
dependencies {
implementation(project(":deob-annotations"))
implementation(project(":deob-util"))
implementation(project(":inject"))
implementation(project(":yaml"))
}

@ -12,6 +12,7 @@ import org.openrs2.deob.annotation.OriginalMember
import org.openrs2.deob.util.map.Field
import org.openrs2.deob.util.map.Method
import org.openrs2.deob.util.map.NameMap
import org.openrs2.inject.CloseableInjector
import org.openrs2.util.io.useAtomicBufferedWriter
import org.openrs2.yaml.Yaml
import java.lang.reflect.Proxy
@ -40,16 +41,12 @@ import javax.lang.model.element.VariableElement
"map"
)
public class NameMapProcessor : AbstractProcessor() {
private val injector = CloseableInjector(Guice.createInjector(DeobfuscatorProcessorModule))
private val map = NameMap()
private val mapper: ObjectMapper
private val mapper = injector.getInstance(Key.get(ObjectMapper::class.java, Yaml::class.java))
private lateinit var trees: Trees
private lateinit var localScanner: LocalVariableScanner
init {
val injector = Guice.createInjector(DeobfuscatorProcessorModule)
mapper = injector.getInstance(Key.get(ObjectMapper::class.java, Yaml::class.java))
}
override fun init(env: ProcessingEnvironment) {
super.init(env)
trees = Trees.instance(unwrap(env))
@ -124,6 +121,8 @@ public class NameMapProcessor : AbstractProcessor() {
mapPath.useAtomicBufferedWriter { writer ->
mapper.writeValue(writer, combinedMap)
}
injector.close()
}
return true

@ -15,6 +15,7 @@ dependencies {
implementation(project(":decompiler"))
implementation(project(":deob-ast"))
implementation(project(":deob-bytecode"))
implementation(project(":inject"))
}
publishing {

@ -2,13 +2,15 @@ package org.openrs2.deob
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import org.openrs2.inject.CloseableInjector
public fun main(args: Array<String>): Unit = DeobfuscateCommand().main(args)
public class DeobfuscateCommand : CliktCommand(name = "deob") {
override fun run() {
val injector = Guice.createInjector(DeobfuscatorModule)
CloseableInjector(Guice.createInjector(DeobfuscatorModule)).use { injector ->
val deobfuscator = injector.getInstance(Deobfuscator::class.java)
deobfuscator.run()
}
}
}

@ -11,7 +11,7 @@ application {
dependencies {
api("com.github.ajalt.clikt:clikt:${Versions.clikt}")
implementation("com.google.inject:guice:${Versions.guice}")
implementation(project(":inject"))
}
publishing {

@ -2,13 +2,15 @@ package org.openrs2.game
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import org.openrs2.inject.CloseableInjector
public fun main(args: Array<String>): Unit = GameCommand().main(args)
public class GameCommand : CliktCommand(name = "game") {
override fun run() {
val injector = Guice.createInjector(GameModule)
CloseableInjector(Guice.createInjector(GameModule)).use { injector ->
val server = injector.getInstance(GameServer::class.java)
server.run()
}
}
}

@ -5,9 +5,6 @@ plugins {
dependencies {
api("com.google.inject:guice:${Versions.guice}")
implementation("com.google.guava:guava:${Versions.guava}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}")
}
publishing {
@ -16,13 +13,10 @@ publishing {
pom {
packaging = "jar"
name.set("OpenRS2 HTTP")
name.set("OpenRS2 Inject")
description.set(
"""
Guava module for creating a HTTP client with .netrc and
redirection support. The I/O dispatcher is used to run
asynchronous requests, as code using coroutines will likely use
the I/O dispatcher to read the response body.
Guice extension for closing AutoCloseable singletons.
""".trimIndent()
)
}

@ -0,0 +1,23 @@
package org.openrs2.inject
import com.google.inject.Injector
import com.google.inject.Scopes
public class CloseableInjector(
private val injector: Injector
) : Injector by injector, AutoCloseable {
override fun close() {
for (binding in allBindings.values) {
if (!Scopes.isSingleton(binding)) {
continue
}
val o = binding.provider.get()
if (o !is AutoCloseable) {
continue
}
o.close()
}
}
}

@ -15,6 +15,7 @@ dependencies {
implementation(project(":conf"))
implementation(project(":crypto"))
implementation(project(":inject"))
implementation("org.openrs2:openrs2-natives-all:${Versions.openrs2Natives}")
}

@ -2,13 +2,14 @@ package org.openrs2.patcher
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import org.openrs2.inject.CloseableInjector
import java.nio.file.Path
public fun main(args: Array<String>): Unit = PatchCommand().main(args)
public class PatchCommand : CliktCommand(name = "patch") {
override fun run() {
val injector = Guice.createInjector(PatcherModule)
CloseableInjector(Guice.createInjector(PatcherModule)).use { injector ->
val patcher = injector.getInstance(Patcher::class.java)
patcher.run(
input = Path.of("nonfree/lib"),
@ -17,3 +18,4 @@ public class PatchCommand : CliktCommand(name = "patch") {
)
}
}
}

@ -32,6 +32,7 @@ include(
"deob-util",
"game",
"http",
"inject",
"json",
"net",
"nonfree",

Loading…
Cancel
Save