1 package net.pterodactylus.sone.main
3 import com.codahale.metrics.*
4 import com.google.common.base.*
5 import com.google.common.eventbus.*
6 import com.google.inject.*
7 import com.google.inject.matcher.*
8 import com.google.inject.name.Names.*
9 import com.google.inject.spi.*
10 import net.pterodactylus.sone.database.*
11 import net.pterodactylus.sone.database.memory.*
12 import net.pterodactylus.sone.freenet.*
13 import net.pterodactylus.sone.freenet.wot.*
14 import net.pterodactylus.util.config.*
15 import net.pterodactylus.util.config.ConfigurationException
16 import net.pterodactylus.util.logging.*
17 import net.pterodactylus.util.version.Version
19 import java.util.logging.*
21 open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: EventBus) : AbstractModule() {
23 override fun configure() {
24 val sonePropertiesFile = File("sone.properties")
25 val firstStart = !sonePropertiesFile.exists()
27 val configuration = try {
28 Configuration(MapConfigurationBackend(sonePropertiesFile, false))
29 } catch (ce: ConfigurationException) {
30 sonePropertiesFile.delete()
32 Configuration(MapConfigurationBackend(sonePropertiesFile, true))
34 val context = Context("Sone")
35 val loaders = configuration.getStringValue("Developer.LoadFromFilesystem")
38 configuration.getStringValue("Developer.FilesystemPath")
40 ?.let { DebugLoaders(it) }
43 bind(Configuration::class.java).toInstance(configuration)
44 bind(EventBus::class.java).toInstance(eventBus)
45 bind(Boolean::class.java).annotatedWith(named("FirstStart")).toInstance(firstStart)
46 bind(Boolean::class.java).annotatedWith(named("NewConfig")).toInstance(newConfig)
47 bind(Context::class.java).toInstance(context)
48 bind(object : TypeLiteral<Optional<Context>>() {}).toInstance(Optional.of(context))
49 bind(SonePlugin::class.java).toInstance(sonePlugin)
50 bind(Version::class.java).toInstance(sonePlugin.version.drop(1).parseVersion())
51 bind(PluginVersion::class.java).toInstance(PluginVersion(sonePlugin.version))
52 bind(PluginYear::class.java).toInstance(PluginYear(sonePlugin.year))
53 bind(PluginHomepage::class.java).toInstance(PluginHomepage(sonePlugin.homepage))
54 bind(Database::class.java).to(MemoryDatabase::class.java).`in`(Singleton::class.java)
55 bind(Translation::class.java).toInstance(BaseL10nTranslation(sonePlugin.l10n().base))
56 loaders?.let { bind(Loaders::class.java).toInstance(it) }
57 bind(MetricRegistry::class.java).`in`(Singleton::class.java)
58 bind(WebOfTrustConnector::class.java).to(PluginWebOfTrustConnector::class.java).`in`(Singleton::class.java)
60 bindListener(Matchers.any(), object : TypeListener {
61 override fun <I> hear(typeLiteral: TypeLiteral<I>, typeEncounter: TypeEncounter<I>) {
62 typeEncounter.register(InjectionListener { injectee ->
63 logger.fine { "Injecting $injectee..." }
64 eventBus.register(injectee)
70 private val logger: Logger = Logging.getLogger(javaClass)
74 private fun String.parseVersion(): Version = Version.parse(this)