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.viewfactory.impl.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.viewfactory.api.common.ModelAndView;
17  import com.hack23.cia.web.viewfactory.api.common.ViewFactoryService;
18  import com.hack23.cia.web.viewfactory.impl.common.ViewFactory;
19  
20  /***
21   * The Class ViewFactoryServiceImpl.
22   */
23  public class ViewFactoryServiceImpl implements ViewFactoryService {
24  
25      /*** The Constant LOGGER. */
26      private static final Log LOGGER = LogFactory
27              .getLog(ViewFactoryServiceImpl.class);
28  
29      /*** The model and view lookup view factory map. */
30      private final Map<Class<ModelAndView>, ViewFactory> modelAndViewLookupViewFactoryMap = new THashMap<Class<ModelAndView>, ViewFactory>();
31  
32      /***
33       * Instantiates a new view factory service impl.
34       *
35       * @param viewFactories the view factories
36       */
37      public ViewFactoryServiceImpl(final List<ViewFactory> viewFactories) {
38          super();
39          for (final ViewFactory viewFactory : viewFactories) {
40              modelAndViewLookupViewFactoryMap.put(viewFactory
41                      .getSupportedModelAndView(), viewFactory);
42          }
43      }
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see
49       * com.hack23.cia.web.controller.viewFactory.ViewFactoryService#processView
50       * (com.hack23.cia.web.controller.viewFactory.ModelAndView)
51       */
52      @Override
53      public final void processView(final ModelAndView modelAndView) {
54          if (modelAndView != null) {
55              LOGGER.info("Processing View :"
56                      + modelAndView.getViewSpecificationDescription());
57              final ViewFactory viewFactory = modelAndViewLookupViewFactoryMap
58                      .get(modelAndView.getClass());
59              if (viewFactory != null) {
60                  LOGGER.info("Found viewFactory :" + viewFactory.getClass().getSimpleName());                        
61                  viewFactory.processView(modelAndView);
62              } else {
63                  LOGGER.error("ViewFactory missing for :"
64                          + modelAndView.getClass().getSimpleName());
65              }
66          } else {
67              LOGGER.error("ModelAndView missing :");
68          }
69      }
70  }