View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  
20  package com.hack23.cia.service.impl;
21  
22  import java.io.Serializable;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.Future;
27  
28  import org.springframework.beans.BeansException;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.beans.factory.annotation.Qualifier;
31  import org.springframework.context.ApplicationContext;
32  import org.springframework.context.ApplicationContextAware;
33  import org.springframework.scheduling.annotation.Async;
34  import org.springframework.scheduling.annotation.AsyncResult;
35  import org.springframework.security.access.annotation.Secured;
36  import org.springframework.security.authentication.AuthenticationManager;
37  import org.springframework.stereotype.Service;
38  
39  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenCommittee;
40  import com.hack23.cia.service.api.AgentContainer;
41  import com.hack23.cia.service.api.ApplicationManager;
42  import com.hack23.cia.service.api.DataContainer;
43  import com.hack23.cia.service.api.DataSummary;
44  import com.hack23.cia.service.api.action.common.ServiceRequest;
45  import com.hack23.cia.service.api.action.common.ServiceResponse;
46  import com.hack23.cia.service.impl.action.common.BusinessService;
47  
48  /**
49   * The Class ApplicationManagerImpl.
50   */
51  @Service("ApplicationManager")
52  final class ApplicationManagerImpl implements ApplicationManager, ApplicationContextAware{
53  
54  	/** The authentication manager. */
55  	@Autowired
56  	@Qualifier("org.springframework.security.authenticationManager")
57  	private AuthenticationManager authenticationManager;
58  
59  	/** The data agent container. */
60  	@Autowired
61  	@Qualifier("DataAgentContainer")
62  	private AgentContainer dataAgentContainer;
63  
64  	/** The data summary data container. */
65  	@Autowired
66  	@Qualifier("DataSummaryDataContainer")
67  	private DataContainer<DataSummary, String> dataSummaryDataContainer;
68  
69  	/** The view riksdagen committee data container. */
70  	@Autowired
71  	@Qualifier("ViewRiksdagenCommitteeDataContainer")
72  	private DataContainer<ViewRiksdagenCommittee,String> viewRiksdagenCommitteeDataContainer;
73  
74  	/** The view data data container factory. */
75  	@Autowired
76  	private ViewDataDataContainerFactory viewDataDataContainerFactory;
77  
78  	/** The service request business service map. */
79  	private final Map<Class<? extends ServiceRequest>, BusinessService<? extends ServiceRequest,? extends ServiceResponse>> serviceRequestBusinessServiceMap = new ConcurrentHashMap<>();
80  
81  
82  	/**
83  	 * Instantiates a new application manager impl.
84  	 */
85  	public ApplicationManagerImpl() {
86  		super();
87  	}
88  
89  
90  	@Secured({"ROLE_ADMIN"})
91  	@Override
92  	public AgentContainer getAgentContainer() {
93  		return dataAgentContainer;
94  	}
95  
96  	@Secured({ "ROLE_ANONYMOUS","ROLE_USER", "ROLE_ADMIN" })
97  	@Override
98  	public <T extends Serializable, V extends Serializable> DataContainer<T, V> getDataContainer(
99  			final Class<T> dataObject) {
100 
101 		DataContainer<T, V> result;
102 
103 		if (dataObject.equals(DataSummary.class)) {
104 			result= (DataContainer<T, V>) dataSummaryDataContainer;
105 		} else if (dataObject.equals(ViewRiksdagenCommittee.class)) {
106 			result= (DataContainer<T, V>) viewRiksdagenCommitteeDataContainer;
107 		} else {
108 			result= viewDataDataContainerFactory.createDataContainer(dataObject);
109 		}
110 
111 		return result;
112 	}
113 
114 	@Secured({"ROLE_ANONYMOUS","ROLE_USER", "ROLE_ADMIN" })
115 	@Override
116 	public ServiceResponse service(final ServiceRequest serviceRequest) {
117 		final BusinessService businessService= serviceRequestBusinessServiceMap.get(serviceRequest.getClass());
118 
119 		ServiceResponse serviceResponse = null;
120 
121 		if (businessService != null) {
122 			serviceResponse = businessService.processService(serviceRequest);
123 		}
124 
125 		return serviceResponse;
126 	}
127 
128     @Async("SecureTaskExecutor")
129     @Secured({"ROLE_ANONYMOUS","ROLE_USER", "ROLE_ADMIN" })
130     @Override
131 	public Future<ServiceResponse> asyncService(final ServiceRequest serviceRequest) {
132 		final BusinessService businessService= serviceRequestBusinessServiceMap.get(serviceRequest.getClass());
133 
134 		ServiceResponse serviceResponse = null;
135 
136 		if (businessService != null) {
137 			serviceResponse = businessService.processService(serviceRequest);
138 		}
139 
140 		return new AsyncResult<>(serviceResponse);
141 	}
142 
143 
144 	@Override
145 	public void setApplicationContext(final ApplicationContext applicationContext)
146 			throws BeansException {
147 		final Map<String, BusinessService> beansOfType = applicationContext.getBeansOfType(BusinessService.class);
148 
149 		for (final Entry<String, BusinessService> entry : beansOfType.entrySet()) {
150 			serviceRequestBusinessServiceMap.put(entry.getValue().getSupportedService(), entry.getValue());
151 		}
152 	}
153 
154 }