1
|
// $Id: CdmApplicationController.java 11680 2011-04-04 17:07:39Z a.mueller $
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
|
12
|
package eu.etaxonomy.cdm.api.application;
|
13
|
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
18
|
import org.springframework.context.support.AbstractApplicationContext;
|
19
|
import org.springframework.context.support.GenericApplicationContext;
|
20
|
import org.springframework.core.io.ClassPathResource;
|
21
|
import org.springframework.core.io.Resource;
|
22
|
import org.springframework.security.access.PermissionEvaluator;
|
23
|
import org.springframework.security.authentication.ProviderManager;
|
24
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
25
|
import org.springframework.security.core.Authentication;
|
26
|
import org.springframework.security.core.context.SecurityContext;
|
27
|
import org.springframework.security.core.context.SecurityContextHolder;
|
28
|
import org.springframework.transaction.PlatformTransactionManager;
|
29
|
//import org.springframework.transaction.PlatformTransactionManager;
|
30
|
import org.springframework.transaction.TransactionStatus;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
33
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
34
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
35
|
import eu.etaxonomy.cdm.api.service.ICollectionService;
|
36
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
37
|
import eu.etaxonomy.cdm.api.service.IDatabaseService;
|
38
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
39
|
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
|
40
|
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
|
41
|
import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
|
42
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
43
|
import eu.etaxonomy.cdm.api.service.IIdentificationKeyService;
|
44
|
import eu.etaxonomy.cdm.api.service.ILocationService;
|
45
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
46
|
import eu.etaxonomy.cdm.api.service.INameService;
|
47
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
48
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
|
49
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
|
50
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
51
|
import eu.etaxonomy.cdm.api.service.IService;
|
52
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
53
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
54
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
55
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
56
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
57
|
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
|
58
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
59
|
import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
|
60
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
61
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
62
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
|
63
|
|
64
|
|
65
|
/**
|
66
|
* @author a.mueller
|
67
|
* @author j.koch
|
68
|
*
|
69
|
*/
|
70
|
public class CdmApplicationRemoteController implements ICdmApplicationRemoteConfiguration {
|
71
|
private static final Logger logger = Logger.getLogger(CdmApplicationRemoteController.class);
|
72
|
|
73
|
public static final String DEFAULT_APPLICATION_CONTEXT_RESOURCE = "/eu/etaxonomy/cdm/remotingApplicationContext.xml";
|
74
|
|
75
|
public AbstractApplicationContext applicationContext;
|
76
|
private ICdmApplicationRemoteConfiguration configuration;
|
77
|
private Resource applicationContextResource;
|
78
|
private IProgressMonitor progressMonitor;
|
79
|
|
80
|
/**
|
81
|
* Constructor, opens a spring ApplicationContext with defaults
|
82
|
*/
|
83
|
public static CdmApplicationRemoteController NewInstance() {
|
84
|
logger.info("Configure CdmApplicationRemoteController with defaults");
|
85
|
return new CdmApplicationRemoteController(null, null);
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Constructor, opens a spring ApplicationContext with given application context
|
90
|
* @param applicationContextResource
|
91
|
*/
|
92
|
public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource, IProgressMonitor progressMonitor) {
|
93
|
logger.info("Configure CdmApplicationRemoteController with given application context");
|
94
|
return new CdmApplicationRemoteController(applicationContextResource, progressMonitor);
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Constructor, starts the application remote controller
|
99
|
* @param applicationContextResource
|
100
|
*/
|
101
|
private CdmApplicationRemoteController(Resource applicationContextResource, IProgressMonitor progressMonitor){
|
102
|
logger.info("Start CdmApplicationRemoteController");
|
103
|
this.applicationContextResource = applicationContextResource != null ? applicationContextResource : new ClassPathResource(DEFAULT_APPLICATION_CONTEXT_RESOURCE);
|
104
|
this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
|
105
|
setNewApplicationContext();
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Sets the application context to a new spring ApplicationContext and initializes the Controller
|
110
|
*/
|
111
|
private boolean setNewApplicationContext(){
|
112
|
logger.info("Set new application context");
|
113
|
progressMonitor.beginTask("Start application context.", 6);
|
114
|
progressMonitor.worked(1);
|
115
|
|
116
|
GenericApplicationContext applicationContext = new GenericApplicationContext();
|
117
|
|
118
|
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
|
119
|
progressMonitor.subTask("Registering resources.");
|
120
|
xmlReader.loadBeanDefinitions(applicationContextResource);
|
121
|
progressMonitor.worked(1);
|
122
|
|
123
|
progressMonitor.subTask("This might take a while ...");
|
124
|
applicationContext.refresh();
|
125
|
applicationContext.start();
|
126
|
progressMonitor.worked(1);
|
127
|
|
128
|
progressMonitor.subTask("Cleaning up.");
|
129
|
setApplicationContext(applicationContext);
|
130
|
progressMonitor.done();
|
131
|
return true;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Tests if some DefinedTermsAreMissing.
|
136
|
* @return true, if at least one is missing, else false
|
137
|
*/
|
138
|
public boolean testDefinedTermsAreMissing(){
|
139
|
UUID englishUuid = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
|
140
|
DefinedTermBase<?> english = this.getTermService().find(englishUuid);
|
141
|
if ( english == null || ! english.getUuid().equals(englishUuid)){
|
142
|
return true;
|
143
|
}else{
|
144
|
return false;
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Sets a new application Context.
|
150
|
* @param ac
|
151
|
*/
|
152
|
public void setApplicationContext(AbstractApplicationContext ac){
|
153
|
closeApplicationContext(); //closes old application context if necessary
|
154
|
applicationContext = ac;
|
155
|
applicationContext.registerShutdownHook();
|
156
|
init();
|
157
|
}
|
158
|
|
159
|
/* (non-Javadoc)
|
160
|
* @see java.lang.Object#finalize()
|
161
|
*/
|
162
|
public void finalize(){
|
163
|
close();
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* closes the application
|
168
|
*/
|
169
|
public void close(){
|
170
|
closeApplicationContext();
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* closes the application context
|
175
|
*/
|
176
|
private void closeApplicationContext(){
|
177
|
if (applicationContext != null){
|
178
|
logger.info("Close ApplicationContext");
|
179
|
applicationContext.close();
|
180
|
}
|
181
|
}
|
182
|
|
183
|
private void init(){
|
184
|
logger.info("Init " + this.getClass().getName() + " ... ");
|
185
|
//if (logger.isInfoEnabled()){
|
186
|
for (String beanName : applicationContext.getBeanDefinitionNames()){
|
187
|
logger.debug(beanName);}
|
188
|
//}
|
189
|
//TODO delete next row (was just for testing)
|
190
|
//if (logger.isInfoEnabled()){
|
191
|
logger.info("Registered Beans: ");
|
192
|
String[] beanNames = applicationContext.getBeanDefinitionNames();
|
193
|
for (String beanName : beanNames){
|
194
|
logger.info(beanName);
|
195
|
}
|
196
|
//}
|
197
|
//configuration = new CdmApplicationRemoteConfiguration();
|
198
|
configuration = (ICdmApplicationRemoteConfiguration) applicationContext.getBean("cdmApplicationRemoteConfiguration");
|
199
|
}
|
200
|
|
201
|
|
202
|
|
203
|
/* ****** Services *********/
|
204
|
|
205
|
public final INameService getNameService(){
|
206
|
return configuration.getNameService();
|
207
|
}
|
208
|
|
209
|
public final ITaxonService getTaxonService(){
|
210
|
return configuration.getTaxonService();
|
211
|
}
|
212
|
|
213
|
public final IClassificationService getClassificationService(){
|
214
|
return configuration.getClassificationService();
|
215
|
}
|
216
|
|
217
|
public final ITaxonNodeService getTaxonNodeService(){
|
218
|
return configuration.getTaxonNodeService();
|
219
|
}
|
220
|
|
221
|
public final IReferenceService getReferenceService(){
|
222
|
return configuration.getReferenceService();
|
223
|
}
|
224
|
|
225
|
public final IAgentService getAgentService(){
|
226
|
return configuration.getAgentService();
|
227
|
}
|
228
|
|
229
|
public final ITermService getTermService(){
|
230
|
return configuration.getTermService();
|
231
|
}
|
232
|
|
233
|
public final IDescriptionService getDescriptionService(){
|
234
|
return configuration.getDescriptionService();
|
235
|
}
|
236
|
|
237
|
public final IOccurrenceService getOccurrenceService(){
|
238
|
return configuration.getOccurrenceService();
|
239
|
}
|
240
|
|
241
|
public final IMediaService getMediaService(){
|
242
|
return configuration.getMediaService();
|
243
|
}
|
244
|
|
245
|
public final ICommonService getCommonService(){
|
246
|
return configuration.getCommonService();
|
247
|
}
|
248
|
|
249
|
public final ILocationService getLocationService(){
|
250
|
return configuration.getLocationService();
|
251
|
}
|
252
|
|
253
|
public final IUserService getUserService(){
|
254
|
return configuration.getUserService();
|
255
|
}
|
256
|
|
257
|
public IGroupService getGroupService() {
|
258
|
return configuration.getGroupService();
|
259
|
}
|
260
|
|
261
|
public final ICollectionService getCollectionService(){
|
262
|
return configuration.getCollectionService();
|
263
|
}
|
264
|
|
265
|
public final IFeatureTreeService getFeatureTreeService(){
|
266
|
return configuration.getFeatureTreeService();
|
267
|
}
|
268
|
|
269
|
public final IFeatureNodeService getFeatureNodeService(){
|
270
|
return configuration.getFeatureNodeService();
|
271
|
}
|
272
|
|
273
|
public final IVocabularyService getVocabularyService(){
|
274
|
return configuration.getVocabularyService();
|
275
|
}
|
276
|
|
277
|
public final IIdentificationKeyService getIdentificationKeyService(){
|
278
|
return configuration.getIdentificationKeyService();
|
279
|
}
|
280
|
|
281
|
public final IPolytomousKeyService getPolytomousKeyService(){
|
282
|
return configuration.getPolytomousKeyService();
|
283
|
}
|
284
|
|
285
|
public final IPolytomousKeyNodeService getPolytomousKeyNodeService(){
|
286
|
return configuration.getPolytomousKeyNodeService();
|
287
|
}
|
288
|
|
289
|
public final IService<CdmBase> getMainService(){
|
290
|
return configuration.getMainService();
|
291
|
}
|
292
|
|
293
|
public final IWorkingSetService getWorkingSetService(){
|
294
|
return configuration.getWorkingSetService();
|
295
|
}
|
296
|
|
297
|
|
298
|
@Override
|
299
|
public ProviderManager getAuthenticationManager() {
|
300
|
return configuration.getAuthenticationManager();
|
301
|
}
|
302
|
|
303
|
|
304
|
|
305
|
@Override
|
306
|
public ConversationHolder NewConversation() {
|
307
|
return configuration.NewConversation();
|
308
|
}
|
309
|
|
310
|
@Override
|
311
|
public Object getBean(String name) {
|
312
|
return configuration.getBean(name);
|
313
|
}
|
314
|
|
315
|
@Override
|
316
|
public IGrantedAuthorityService getGrantedAuthorityService() {
|
317
|
return configuration.getGrantedAuthorityService();
|
318
|
}
|
319
|
|
320
|
@Override
|
321
|
public ICdmPermissionEvaluator getPermissionEvaluator() {
|
322
|
return configuration.getPermissionEvaluator();
|
323
|
}
|
324
|
|
325
|
@Override
|
326
|
public void authenticate(String username, String password) {
|
327
|
configuration.authenticate(username, password);
|
328
|
|
329
|
}
|
330
|
|
331
|
|
332
|
}
|