1
2
3
4
5
6 package com.hack23.cia.web.controller.application;
7
8 import gnu.trove.THashMap;
9
10 import java.util.List;
11 import java.util.Map;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import com.hack23.cia.web.action.common.ApplicationErrorAction;
17 import com.hack23.cia.web.action.common.ControllerAction;
18 import com.hack23.cia.web.controller.common.ActionHandler;
19
20 /***
21 * The Class FrontController.
22 */
23 public class FrontController implements Controller {
24
25 /*** The Constant LOGGER. */
26 private static final Log LOGGER = LogFactory.getLog(FrontController.class);
27
28 /*** The action handlers map. */
29
30 private final Map<Class<ControllerAction>, ActionHandler> actionHandlersMap = new THashMap<Class<ControllerAction>, ActionHandler>();
31
32 /*** The default action handler. */
33 private final ActionHandler defaultActionHandler;
34
35 /***
36 * Instantiates a new front controller.
37 *
38 * @param actionHandlers the action handlers
39 * @param defaultActionHandler the default action handler
40 */
41 public FrontController(final List<ActionHandler> actionHandlers,final ActionHandler defaultActionHandler) {
42 super();
43 this.defaultActionHandler = defaultActionHandler;
44 for (final ActionHandler actionHandler : actionHandlers) {
45 actionHandlersMap.put(actionHandler.getSupportedAction(),
46 actionHandler);
47 }
48 }
49
50 /***
51 * Gets the action handlers map.
52 *
53 * @return the action handlers map
54 */
55 public final Map<Class<ControllerAction>, ActionHandler> getActionHandlersMap() {
56 return actionHandlersMap;
57 }
58
59 /***
60 * Gets the default action handler.
61 *
62 * @return the default action handler
63 */
64 public final ActionHandler getDefaultActionHandler() {
65 return defaultActionHandler;
66 }
67
68
69
70
71
72
73
74
75
76 @Override
77 public final void handleAction(final ControllerAction action)
78 throws Exception {
79
80 if (action != null) {
81 LOGGER.info("Action Received: " + action.toString());
82 final ActionHandler actionHandler = actionHandlersMap.get(action.getClass());
83
84 try {
85 if (actionHandler != null) {
86 actionHandler.processAction(action);
87 } else {
88 defaultActionHandler.processAction(action);
89 }
90 } catch (final Exception e) {
91 e.printStackTrace();
92 final ApplicationErrorAction applicationErrorAction = new ApplicationErrorAction(e.getStackTrace(),"Exception from ActionHandler");
93 actionHandlersMap.get(applicationErrorAction.getClass()).processAction(applicationErrorAction);
94 }
95 LOGGER.info("Action Completed: " + action);
96 } else {
97 LOGGER.warn("No action received");
98 }
99
100 }
101
102 }