forked from openrs2/openrs2
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>
parent
a0f7bf3922
commit
c5bd044574
@ -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() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue