Skip to content

Commit 22709cf

Browse files
committed
Refactor module service injection with grouping support
- Added `ModuleServiceHolder` to support separate module grouping. - Updated `ModuleServiceInjector` to group services by module using `@ModuleService` annotations. - Injected grouped services dynamically into module holders or default holder.
1 parent 6a7379f commit 22709cf

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.netgrif.application.engine.integration.modules
2+
3+
class ModuleServiceHolder {
4+
}

application-engine/src/main/groovy/com/netgrif/application/engine/integration/modules/ModuleServiceInjector.groovy

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,47 @@ import org.springframework.stereotype.Component
1515
class ModuleServiceInjector implements ApplicationEngineFinishRunner {
1616

1717
private static final Logger log = LoggerFactory.getLogger(ModuleServiceInjector.class)
18+
private final String DEFAULT_KEY = "default"
1819

1920
@Override
2021
void run(ApplicationArguments args) throws Exception {
2122
def injectableBeans = ApplicationContextProvider.getAppContext().getBeansWithAnnotation(ModuleService.class)
2223
if (!injectableBeans.isEmpty()) {
2324
MetaClass holderMetaClass = ModuleHolder.metaClass
24-
injectableBeans.forEach((key, value) -> {
25-
log.debug("Injecting module service {} into action delegate make it available under module.{}", key, key)
26-
holderMetaClass[key] = value
27-
})
25+
def groupedServices = groupServicesByModule(injectableBeans)
26+
groupedServices.each { entry ->
27+
if (entry.key == DEFAULT_KEY) {
28+
entry.value.each { serviceEntry ->
29+
log.info("Injecting module service {} into action delegate making it available under module.{}", serviceEntry.key, serviceEntry.key)
30+
holderMetaClass[serviceEntry.key] = serviceEntry.value
31+
}
32+
} else {
33+
MetaClass moduleServiceHolderMetaClass = ModuleServiceHolder.metaClass
34+
entry.value.each { serviceEntry ->
35+
log.info("Injecting module service {} into module holder {} into action delegate making it available under module.{}.{}", serviceEntry.key, entry.key, entry.key, serviceEntry.key)
36+
moduleServiceHolderMetaClass[serviceEntry.key] = serviceEntry.value
37+
}
38+
holderMetaClass[entry.key] = new ModuleServiceHolder()
39+
}
40+
}
2841
}
2942
}
43+
44+
private Map<String, Map<String, Object>> groupServicesByModule(Map<String, Object> services) {
45+
Map<String, Map<String, Object>> grouped = [(DEFAULT_KEY): [:]]
46+
services.each { entry ->
47+
ModuleService[] annotations = entry.value.getClass().getAnnotationsByType(ModuleService.class)
48+
if (annotations.length == 0) throw new IllegalStateException("Module Service bean must have @ModuleService annotations")
49+
ModuleService annotation = annotations[0]
50+
if (annotation.value().isBlank()) {
51+
grouped[(DEFAULT_KEY)].put(entry.key, entry.value)
52+
} else {
53+
String moduleName = annotation.value()
54+
if (!grouped.containsKey(moduleName)) grouped.put(moduleName, [:])
55+
grouped[moduleName].put(entry.key, entry.value)
56+
}
57+
}
58+
return grouped
59+
}
60+
3061
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.netgrif.application.engine.integration.modules;
22

3+
import org.springframework.core.annotation.AliasFor;
4+
35
import java.lang.annotation.*;
46

57
@Documented
68
@Target({ElementType.TYPE})
79
@Retention(RetentionPolicy.RUNTIME)
810
public @interface ModuleService {
11+
12+
@AliasFor("module")
13+
String value() default "";
14+
15+
@AliasFor("value")
16+
String module() default "";
17+
918
}

0 commit comments

Comments
 (0)