View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
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       * (non-Javadoc)
70       * 
71       * @see
72       * com.hack23.cia.web.controller.Controller#handleAction(com.hack23.cia.
73       * web.action.ControllerAction)
74       */
75      //@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
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()); //$NON-NLS-1$        
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); //$NON-NLS-1$
96          } else {
97          	LOGGER.warn("No action received");
98          }
99          
100     }
101 
102 }