Skip to content

Commit 4770e24

Browse files
author
Rupert Westenthaler
committed
MORE-Platform#303: This changes the lookup for ObservationFactory beans to be dynamic. The ApplicationContext is now used to lookup beans by id (obervergation.getType()) at the time of request. The bug was caused by a change in the bean initialization caused by removing some dependencies from the StudyService. Wit this change the initialisation order has no effect.
1 parent aeddc3b commit 4770e24

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

studymanager-core/src/main/java/io/redlink/more/studymanager/core/properties/model/Value.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,14 @@ public Value<T> setValidationFunction(Function<T, ValidationIssue> validationFun
126126
this.validationFunction = validationFunction;
127127
return this;
128128
}
129+
130+
@Override
131+
public String toString() {
132+
return "Value{" +
133+
"id='" + id + '\'' +
134+
", defaultValue=" + defaultValue +
135+
", required=" + required +
136+
", immutable=" + immutable +
137+
'}';
138+
}
129139
}

studymanager-services/src/main/java/io/redlink/more/studymanager/configuration/ComponentFactoriesConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import io.redlink.more.studymanager.core.factory.TriggerFactory;
1414
import io.redlink.more.studymanager.properties.ComponentFactoriesProperties;
1515
import org.reflections.Reflections;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1618
import org.springframework.beans.factory.BeanFactory;
1719
import org.springframework.beans.factory.BeanFactoryAware;
1820
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@@ -29,6 +31,7 @@
2931
@Configuration
3032
@EnableConfigurationProperties({ComponentFactoriesProperties.class})
3133
public class ComponentFactoriesConfiguration implements BeanFactoryAware {
34+
private final Logger logger = LoggerFactory.getLogger(ComponentFactoriesConfiguration.class);
3235
private BeanFactory beanFactory;
3336

3437
private final Reflections reflections;
@@ -61,8 +64,10 @@ public void onPostConstruct() {
6164
Set<Class<? extends ObservationFactory>> observationFactories = reflections.getSubTypesOf(ObservationFactory.class);
6265
observationFactories.stream().map(this::instantiate)
6366
.map(f -> f.init(componentFactoriesProperties.get(f.getId())))
64-
.forEach(m ->
65-
configurableBeanFactory.registerSingleton(m.getId(), m)
67+
.forEach(m -> {
68+
logger.trace("Registering observation factory: {}[class:{}, properties:{}]", m.getId(),m.getClass().getName(), m.getProperties());
69+
configurableBeanFactory.registerSingleton(m.getId(), m);
70+
}
6671
);
6772

6873
/*

studymanager-services/src/main/java/io/redlink/more/studymanager/service/ObservationService.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@
2525
import io.redlink.more.studymanager.repository.ObservationRepository;
2626
import io.redlink.more.studymanager.sdk.MoreSDK;
2727
import io.redlink.more.studymanager.utils.RandomSchedulerUtils;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
import org.springframework.beans.factory.annotation.Lookup;
31+
import org.springframework.context.ApplicationContext;
2832
import org.springframework.context.event.EventListener;
2933
import org.springframework.stereotype.Service;
3034

35+
import javax.annotation.Resource;
3136
import java.util.ArrayList;
3237
import java.util.Collection;
3338
import java.util.List;
@@ -37,20 +42,23 @@
3742
@Service
3843
public class ObservationService {
3944

45+
private final Logger log = LoggerFactory.getLogger(this.getClass());
46+
4047
private final StudyStateService studyStateService;
4148
private final ObservationRepository repository;
4249

43-
private final Map<String, ObservationFactory> observationFactories;
4450
private final MoreSDK sdk;
51+
ApplicationContext applicationContext;
4552

4653
public ObservationService(StudyStateService studyStateService,
4754
ObservationRepository repository,
48-
Map<String, ObservationFactory> observationFactories,
49-
MoreSDK sdk) {
55+
MoreSDK sdk,
56+
ApplicationContext applicationContext) {
5057
this.studyStateService = studyStateService;
5158
this.repository = repository;
52-
this.observationFactories = observationFactories;
59+
//this.observationFactories = observationFactories;
5360
this.sdk = sdk;
61+
this.applicationContext = applicationContext;
5462
}
5563

5664
public Observation addObservation(Observation observation) {
@@ -171,13 +179,15 @@ public List<ParticipantWithObservationProperties> getParticipantObservationPrope
171179
}
172180

173181
private ObservationFactory factory(Observation observation) {
174-
return observationFactories.get(observation.getType());
182+
ObservationFactory factory = applicationContext.getBean(observation.getType(), ObservationFactory.class);
183+
if(factory == null) {
184+
throw NotFoundException.ObservationFactory(observation.getType());
185+
} else {
186+
return factory;
187+
}
175188
}
176189

177190
private Observation validate(Observation observation) {
178-
if (!observationFactories.containsKey(observation.getType())) {
179-
throw NotFoundException.ObservationFactory(observation.getType());
180-
}
181191
try {
182192
final var factory = factory(observation);
183193
factory.validate(observation.getProperties());

0 commit comments

Comments
 (0)