-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHandlerAdapter.java
More file actions
34 lines (28 loc) · 1.21 KB
/
HandlerAdapter.java
File metadata and controls
34 lines (28 loc) · 1.21 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
package webserver;
import annotation.RequestMapping;
import controller.Controller;
import controller.ControllerKey;
import controller.DefaultController;
import org.reflections.Reflections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HandlerAdapter {
private static final Controller defaultController = new DefaultController();
private static final Map<ControllerKey, Controller> controllerMap = new HashMap<>();
public HandlerAdapter() {
Reflections reflections = new Reflections("controller");
Set<Class<?>> requestMappingControllers = reflections.getTypesAnnotatedWith(RequestMapping.class);
for (Class<?> controller : requestMappingControllers) {
RequestMapping annotation = controller.getAnnotation(RequestMapping.class);
try {
controllerMap.put(new ControllerKey(annotation.method(), annotation.path()), (Controller) controller.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
public Controller controller(ControllerKey key){
return controllerMap.getOrDefault(key,defaultController);
}
}