-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathV2RouterModule.java
More file actions
47 lines (42 loc) · 1.91 KB
/
V2RouterModule.java
File metadata and controls
47 lines (42 loc) · 1.91 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
46
47
package com.uid2.admin.vertx.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.ClassPath;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
public class V2RouterModule extends AbstractModule {
private static final Logger LOGGER = LoggerFactory.getLogger(V2RouterModule.class);
/*
Finds all classes in com.uid2.admin.vertx.api which implement IRouterProvider and register them.
They are registered both as IRouterProvider and as their individual class.
*/
@Override
protected void configure() {
try {
Multibinder<IRouteProvider> interfaceBinder = Multibinder.newSetBinder(binder(), IRouteProvider.class);
val cp = ClassPath.from(getClass().getClassLoader());
val routerProviders = cp
.getTopLevelClasses()
.stream()
.filter(ci -> ci.getName().startsWith("com.uid2.admin.vertx.api"))
.map(ci -> ci.load())
.filter(cl -> !cl.isInterface() && Arrays.stream(cl.getInterfaces()).anyMatch(interf -> interf == IRouteProvider.class || interf == IBlockingRouteProvider.class))
.map(cl -> (Class<IRouteProvider>)cl)
.collect(Collectors.toSet());
for (val routerProviderClass : routerProviders) {
LOGGER.info("Registering v2 route provider " + routerProviderClass.getName());
bind(routerProviderClass);
interfaceBinder.addBinding().to(routerProviderClass);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}