-
-
Notifications
You must be signed in to change notification settings - Fork 694
/
BeanRegistry.kt
280 lines (247 loc) · 9.62 KB
/
BeanRegistry.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.koin.core.registry
import org.koin.core.KoinApplication.Companion.logger
import org.koin.core.definition.BeanDefinition
import org.koin.core.error.DefinitionOverrideException
import org.koin.core.error.NoBeanDefFoundException
import org.koin.core.logger.Level
import org.koin.core.module.Module
import org.koin.core.qualifier.Qualifier
import org.koin.ext.getFullName
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
/**
* Bean Registry
* declare/find definitions
*
* @author Arnaud Giuliani
*/
class BeanRegistry {
private val definitions: HashSet<BeanDefinition<*>> = hashSetOf()
private val definitionsNames: MutableMap<String, BeanDefinition<*>> = ConcurrentHashMap()
private val definitionsPrimaryTypes: MutableMap<KClass<*>, BeanDefinition<*>> = ConcurrentHashMap()
private val definitionsSecondaryTypes: MutableMap<KClass<*>, ArrayList<BeanDefinition<*>>> = ConcurrentHashMap()
private val definitionsToCreate: HashSet<BeanDefinition<*>> = hashSetOf()
/**
* Load definitions from a Module
* @param modules
*/
fun loadModules(modules: Iterable<Module>) {
modules.forEach { module: Module ->
saveDefinitions(module)
}
}
/**
* unload definitions from a Module
* @param modules
*/
internal fun unloadModules(modules: Iterable<Module>) {
modules.forEach { module: Module ->
removeDefinitions(module)
}
}
private fun removeDefinitions(module: Module) {
module.definitions.forEach { definition ->
removeDefinition(definition)
}
}
private fun saveDefinitions(module: Module) {
module.definitions.forEach { definition ->
saveDefinition(definition)
}
}
/**
* retrieve all definitions
* @return definitions
*/
fun getAllDefinitions(): Set<BeanDefinition<*>> = definitions
/**
* Save a definition
* @param definition
*/
private fun removeDefinition(definition: BeanDefinition<*>) {
definition.instance?.close()
definitions.remove(definition)
if (definition.qualifier != null) {
removeDefinitionForName(definition)
} else {
removeDefinitionForTypes(definition)
}
removeDefinitionForSecondaryTypes(definition)
}
/**
* Save a definition
* @param definition
*/
fun saveDefinition(definition: BeanDefinition<*>) {
definitions.addDefinition(definition)
definition.createInstanceHolder()
if (definition.qualifier != null) {
saveDefinitionForName(definition)
} else {
saveDefinitionForTypes(definition)
}
if (definition.secondaryTypes.isNotEmpty()) {
saveDefinitionForSecondaryTypes(definition)
}
if (definition.options.isCreatedAtStart) {
saveDefinitionForStart(definition)
}
}
private fun saveDefinitionForSecondaryTypes(definition: BeanDefinition<*>) {
definition.secondaryTypes.forEach {
saveDefinitionForSecondaryType(definition, it)
}
}
private fun saveDefinitionForSecondaryType(definition: BeanDefinition<*>, type: KClass<*>) {
val secondaryTypeDefinitions: ArrayList<BeanDefinition<*>> = definitionsSecondaryTypes[type]
?: createSecondaryType(type)
secondaryTypeDefinitions.add(definition)
if (logger.isAt(Level.INFO)) {
logger.info("bind secondary type:'${type.getFullName()}' ~ $definition")
}
}
private fun createSecondaryType(type: KClass<*>): ArrayList<BeanDefinition<*>> {
definitionsSecondaryTypes[type] = arrayListOf()
return definitionsSecondaryTypes[type]!!
}
private fun saveDefinitionForStart(definition: BeanDefinition<*>) {
definitionsToCreate.add(definition)
}
private fun HashSet<BeanDefinition<*>>.addDefinition(definition: BeanDefinition<*>) {
val added = add(definition)
if (!added && !definition.options.override) {
throw DefinitionOverrideException("Already existing definition or try to override an existing one: $definition")
}
}
private fun saveDefinitionForTypes(definition: BeanDefinition<*>) {
saveDefinitionForType(definition.primaryType, definition)
}
private fun removeDefinitionForSecondaryTypes(definition: BeanDefinition<*>) {
definition.secondaryTypes.forEach {
removeDefinitionForSecondaryType(definition, it)
}
}
private fun removeDefinitionForSecondaryType(definition: BeanDefinition<*>, type: KClass<*>) {
val removed = definitionsSecondaryTypes[type]?.remove(definition) ?: false
if (logger.isAt(Level.DEBUG) && removed) {
logger.info("unbind secondary type:'${type.getFullName()}' ~ $definition")
}
}
private fun removeDefinitionForTypes(definition: BeanDefinition<*>) {
val key = definition.primaryType
if (definitionsPrimaryTypes[key] == definition) {
definitionsPrimaryTypes.remove(key)
if (logger.isAt(Level.DEBUG)) {
logger.info("unbind type:'${key.getFullName()}' ~ $definition")
}
}
}
private fun saveDefinitionForType(type: KClass<*>, definition: BeanDefinition<*>) {
if (definitionsPrimaryTypes[type] != null && !definition.options.override) {
throw DefinitionOverrideException("Already existing definition or try to override an existing one with type '$type' and $definition but has already registered ${definitionsPrimaryTypes[type]}")
} else {
definitionsPrimaryTypes[type] = definition
if (logger.isAt(Level.INFO)) {
logger.info("bind type:'${type.getFullName()}' ~ $definition")
}
}
}
private fun removeDefinitionForName(definition: BeanDefinition<*>) {
definition.qualifier?.let {
val key = it.toString()
if (definitionsNames[key] == definition) {
definitionsNames.remove(key)
if (logger.isAt(Level.DEBUG)) {
logger.info("unbind qualifier:'$key' ~ $definition")
}
}
}
}
private fun saveDefinitionForName(definition: BeanDefinition<*>) {
definition.qualifier?.let {
if (definitionsNames[it.toString()] != null && !definition.options.override) {
throw DefinitionOverrideException("Already existing definition or try to override an existing one with qualifier '$it' with $definition but has already registered ${definitionsNames[it.toString()]}")
} else {
definitionsNames[it.toString()] = definition
if (logger.isAt(Level.INFO)) {
logger.info("bind qualifier:'${definition.qualifier}' ~ $definition")
}
}
}
}
/**
* Find a definition
* @param qualifier
* @param clazz
*/
fun findDefinition(
qualifier: Qualifier? = null,
clazz: KClass<*>
): BeanDefinition<*>? {
return if (qualifier != null) {
findDefinitionByName(qualifier.toString())
} else {
findDefinitionByType(clazz) ?: findDefinitionBySecondaryType(clazz)
}
}
//TODO Find with secondary type
private fun findDefinitionByType(kClass: KClass<*>): BeanDefinition<*>? {
return definitionsPrimaryTypes[kClass]
}
private fun findDefinitionBySecondaryType(kClass: KClass<*>): BeanDefinition<*>? {
val foundTypes = definitionsSecondaryTypes[kClass]
return when {
foundTypes != null && foundTypes.size == 1 -> foundTypes[0]
foundTypes != null && foundTypes.size > 1 -> throw NoBeanDefFoundException("Found multiple definitions for type '${kClass.getFullName()}': $foundTypes. Please use the 'bind<P,S>()' function to bind your instance from primary and secondary types.")
else -> null
}
}
private fun findDefinitionByName(name: String): BeanDefinition<*>? {
return definitionsNames[name]
}
internal fun findAllCreatedAtStartDefinition(): Set<BeanDefinition<*>> {
return definitionsToCreate
}
/**
* Total number of definitions
*/
fun size() = definitions.size
/**
* Retrieve a definition
* @param clazz
*/
fun getDefinition(clazz: KClass<*>): BeanDefinition<*>? {
return definitions.firstOrNull {
it.primaryType == clazz || it.secondaryTypes.contains(
clazz
)
}
}
fun close() {
definitions.forEach { it.close() }
definitions.clear()
definitionsNames.clear()
definitionsPrimaryTypes.clear()
definitionsToCreate.clear()
}
/**
* Find all definition compatible with given type
*/
fun getDefinitionsForClass(clazz: KClass<*>) = getAllDefinitions()
.filter { it.primaryType == clazz || it.secondaryTypes.contains(clazz) && !it.hasScopeSet() }
}