-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceStrategyRegistry.java
More file actions
45 lines (40 loc) · 1.7 KB
/
Copy pathSourceStrategyRegistry.java
File metadata and controls
45 lines (40 loc) · 1.7 KB
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
package com.openelements.content;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
/**
* Selects the {@link ContentSourceStrategy} for a {@link ContentSource} by its {@link SourceType}.
*
* <p>All strategy beans are injected and indexed by {@link ContentSourceStrategy#type()}, so a new
* strategy is auto-discovered simply by being a bean. Two strategies claiming the same type is a
* configuration error and fails fast at startup.
*/
@Component
public class SourceStrategyRegistry {
private final Map<SourceType, ContentSourceStrategy> byType;
public SourceStrategyRegistry(List<ContentSourceStrategy> strategies) {
Map<SourceType, ContentSourceStrategy> map = new EnumMap<>(SourceType.class);
for (ContentSourceStrategy strategy : strategies) {
ContentSourceStrategy previous = map.putIfAbsent(strategy.type(), strategy);
if (previous != null) {
throw new IllegalStateException(
"Multiple content source strategies registered for type " + strategy.type());
}
}
this.byType = map;
}
/**
* @param source the source whose strategy is needed
* @return the strategy handling the source's type
* @throws IllegalArgumentException if no strategy is registered for the source's type
*/
public ContentSourceStrategy forSource(ContentSource source) {
ContentSourceStrategy strategy = byType.get(source.type());
if (strategy == null) {
throw new IllegalArgumentException(
"No content source strategy registered for type " + source.type());
}
return strategy;
}
}