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.service.impl.application;
7   
8   import gnu.trove.THashMap;
9   
10  import java.util.List;
11  import java.util.Map;
12  
13  import com.hack23.cia.service.api.application.ApplicationManager;
14  import com.hack23.cia.service.api.common.ServiceRequest;
15  import com.hack23.cia.service.api.common.ServiceResponse;
16  import com.hack23.cia.service.impl.common.BusinessService;
17  
18  /***
19   * The Class ApplicationManagerImpl.
20   */
21  public class ApplicationManagerImpl implements ApplicationManager {
22  
23      /*** The default business service. */
24      private final BusinessService defaultBusinessService;
25  
26      /*** The service request business service map. */
27      private final Map<Class<ServiceRequest>, BusinessService> serviceRequestBusinessServiceMap = new THashMap<Class<ServiceRequest>, BusinessService>();
28      
29      /***
30       * Instantiates a new application manager impl.
31       *
32       * @param businessServices the business services
33       * @param defaultBusinessService the default business service
34       */
35      public ApplicationManagerImpl(final List<BusinessService> businessServices,final BusinessService defaultBusinessService) {
36          super();
37          this.defaultBusinessService = defaultBusinessService;
38          for (final BusinessService serviceHandler : businessServices) {
39              serviceRequestBusinessServiceMap.put(serviceHandler
40                      .getSupportedService(), serviceHandler);
41          }
42      }
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see
48       * com.hack23.cia.service.ApplicationManager#service(com.hack23.cia.service
49       * .api.ServiceRequest)
50       */
51      @Override
52      public final ServiceResponse service(final ServiceRequest serviceRequest) {
53          final BusinessService businessService = serviceRequestBusinessServiceMap
54                  .get(serviceRequest.getClass());
55          ServiceResponse serviceResponse = null;
56  
57          if (businessService != null) {
58              serviceResponse = businessService.processService(serviceRequest);
59          } else {
60              serviceResponse = defaultBusinessService.processService(serviceRequest);
61          }
62  
63          return serviceResponse;
64      }
65  
66  }