1
|
// $Id$
|
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.List;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.hibernate.SessionFactory;
|
19
|
import org.springframework.beans.MutablePropertyValues;
|
20
|
import org.springframework.beans.factory.config.BeanDefinition;
|
21
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
22
|
import org.springframework.context.support.AbstractApplicationContext;
|
23
|
import org.springframework.context.support.GenericApplicationContext;
|
24
|
import org.springframework.core.io.ClassPathResource;
|
25
|
import org.springframework.core.io.Resource;
|
26
|
import org.springframework.security.authentication.ProviderManager;
|
27
|
import org.springframework.transaction.PlatformTransactionManager;
|
28
|
import org.springframework.transaction.TransactionDefinition;
|
29
|
import org.springframework.transaction.TransactionStatus;
|
30
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.application.eclipse.EclipseRcpSaveGenericApplicationContext;
|
33
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
34
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
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.ILocationService;
|
42
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
43
|
import eu.etaxonomy.cdm.api.service.INameService;
|
44
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
45
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
46
|
import eu.etaxonomy.cdm.api.service.IService;
|
47
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
48
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
49
|
import eu.etaxonomy.cdm.api.service.ITaxonTreeService;
|
50
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
51
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
52
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
53
|
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
|
54
|
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
|
55
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
56
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
57
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
58
|
import eu.etaxonomy.cdm.model.common.CdmMetaData;
|
59
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
60
|
import eu.etaxonomy.cdm.model.common.User;
|
61
|
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
|
62
|
|
63
|
|
64
|
/**
|
65
|
* @author a.mueller
|
66
|
*
|
67
|
*/
|
68
|
public class CdmApplicationController {
|
69
|
private static final Logger logger = Logger.getLogger(CdmApplicationController.class);
|
70
|
|
71
|
public static final String DEFAULT_APPLICATION_CONTEXT_RESOURCE = "/eu/etaxonomy/cdm/defaultApplicationContext.xml";
|
72
|
|
73
|
public AbstractApplicationContext applicationContext;
|
74
|
private ICdmApplicationConfiguration configuration;
|
75
|
private Resource applicationContextResource;
|
76
|
|
77
|
final static DbSchemaValidation defaultDbSchemaValidation = DbSchemaValidation.VALIDATE;
|
78
|
|
79
|
|
80
|
|
81
|
/**
|
82
|
* Constructor, opens an spring 2.5 ApplicationContext by using the default data source
|
83
|
*/
|
84
|
public static CdmApplicationController NewInstance() throws DataSourceNotFoundException, TermNotFoundException {
|
85
|
logger.info("Start CdmApplicationController with default data source");
|
86
|
CdmPersistentDataSource dataSource = CdmPersistentDataSource.NewDefaultInstance();
|
87
|
DbSchemaValidation dbSchemaValidation = defaultDbSchemaValidation;
|
88
|
return new CdmApplicationController(null, dataSource, dbSchemaValidation, false);
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Constructor, opens an spring 2.5 ApplicationContext by using the default data source
|
93
|
* @param dbSchemaValidation validation type for database schema
|
94
|
*/
|
95
|
public static CdmApplicationController NewInstance(DbSchemaValidation dbSchemaValidation) throws DataSourceNotFoundException, TermNotFoundException {
|
96
|
logger.info("Start CdmApplicationController with default data source");
|
97
|
CdmPersistentDataSource dataSource = CdmPersistentDataSource.NewDefaultInstance();
|
98
|
if (dbSchemaValidation == null){
|
99
|
dbSchemaValidation = defaultDbSchemaValidation;
|
100
|
}
|
101
|
return new CdmApplicationController(null, dataSource, dbSchemaValidation, false);
|
102
|
}
|
103
|
|
104
|
|
105
|
/**
|
106
|
* Constructor, opens an spring 2.5 ApplicationContext by using the according data source and the
|
107
|
* default database schema validation type
|
108
|
* @param dataSource
|
109
|
*/
|
110
|
public static CdmApplicationController NewInstance(ICdmDataSource dataSource)
|
111
|
throws DataSourceNotFoundException, TermNotFoundException{
|
112
|
return new CdmApplicationController(null, dataSource, defaultDbSchemaValidation, false);
|
113
|
}
|
114
|
|
115
|
public static CdmApplicationController NewInstance(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation)
|
116
|
throws DataSourceNotFoundException, TermNotFoundException{
|
117
|
return new CdmApplicationController(null, dataSource, dbSchemaValidation, false);
|
118
|
}
|
119
|
|
120
|
public static CdmApplicationController NewInstance(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
|
121
|
throws DataSourceNotFoundException, TermNotFoundException{
|
122
|
return new CdmApplicationController(null, dataSource, dbSchemaValidation, omitTermLoading);
|
123
|
}
|
124
|
|
125
|
public static CdmApplicationController NewInstance(Resource applicationContextResource, ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
|
126
|
throws DataSourceNotFoundException, TermNotFoundException{
|
127
|
return new CdmApplicationController(applicationContextResource, dataSource, dbSchemaValidation, omitTermLoading);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Constructor, opens an spring 2.5 ApplicationContext by using the according data source
|
132
|
* @param dataSource
|
133
|
* @param dbSchemaValidation
|
134
|
* @param omitTermLoading
|
135
|
*/
|
136
|
private CdmApplicationController(Resource applicationContextResource, ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading){
|
137
|
logger.info("Start CdmApplicationController with datasource: " + dataSource.getName());
|
138
|
if (applicationContextResource != null){
|
139
|
this.applicationContextResource = applicationContextResource;
|
140
|
}else{
|
141
|
this.applicationContextResource = new ClassPathResource(DEFAULT_APPLICATION_CONTEXT_RESOURCE);
|
142
|
}
|
143
|
|
144
|
setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading);
|
145
|
|
146
|
// if (setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading) == false){
|
147
|
// throw new DataSourceNotFoundException("Wrong datasource: " + dataSource );
|
148
|
// }
|
149
|
}
|
150
|
|
151
|
|
152
|
/**
|
153
|
* Sets the application context to a new spring ApplicationContext by using the according data source and initializes the Controller.
|
154
|
* @param dataSource
|
155
|
*/
|
156
|
private boolean setNewDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading){
|
157
|
if (dbSchemaValidation == null){
|
158
|
dbSchemaValidation = defaultDbSchemaValidation;
|
159
|
}
|
160
|
logger.info("Connecting to '" + dataSource.getName() + "'");
|
161
|
|
162
|
|
163
|
GenericApplicationContext appContext;
|
164
|
// try {
|
165
|
appContext = new EclipseRcpSaveGenericApplicationContext();
|
166
|
|
167
|
BeanDefinition datasourceBean = dataSource.getDatasourceBean();
|
168
|
datasourceBean.setAttribute("isLazy", false);
|
169
|
appContext.registerBeanDefinition("dataSource", datasourceBean);
|
170
|
|
171
|
BeanDefinition hibernatePropBean= dataSource.getHibernatePropertiesBean(dbSchemaValidation);
|
172
|
appContext.registerBeanDefinition("hibernateProperties", hibernatePropBean);
|
173
|
|
174
|
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext);
|
175
|
xmlReader.loadBeanDefinitions(this.applicationContextResource);
|
176
|
|
177
|
//omitTerms
|
178
|
String initializerName = "persistentTermInitializer";
|
179
|
BeanDefinition beanDef = appContext.getBeanDefinition(initializerName);
|
180
|
MutablePropertyValues values = beanDef.getPropertyValues();
|
181
|
values.addPropertyValue("omit", omitTermLoading);
|
182
|
|
183
|
appContext.refresh();
|
184
|
appContext.start();
|
185
|
|
186
|
setApplicationContext(appContext);
|
187
|
|
188
|
//initialize user and metaData for new databases
|
189
|
int userCount = getUserService().count(User.class);
|
190
|
if (userCount == 0 ){
|
191
|
createAdminUser();
|
192
|
}
|
193
|
int metaDataCount = getCommonService().getCdmMetaData().size();
|
194
|
if (metaDataCount == 0){
|
195
|
createMetadata();
|
196
|
}
|
197
|
|
198
|
return true;
|
199
|
}
|
200
|
|
201
|
private void createAdminUser(){
|
202
|
User firstUser = User.NewInstance("admin", "0000");
|
203
|
getUserService().save(firstUser);
|
204
|
logger.info("Admin user created.");
|
205
|
}
|
206
|
|
207
|
private void createMetadata(){
|
208
|
List<CdmMetaData> metaData = CdmMetaData.defaultMetaData();
|
209
|
getCommonService().saveAllMetaData(metaData);
|
210
|
logger.info("Metadata created.");
|
211
|
}
|
212
|
|
213
|
|
214
|
/**
|
215
|
* Tests if some DefinedTermsAreMissing.
|
216
|
* @return true, if at least one is missing, else false
|
217
|
*/
|
218
|
public boolean testDefinedTermsAreMissing(){
|
219
|
UUID englishUuid = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
|
220
|
DefinedTermBase<?> english = this.getTermService().getByUri(englishUuid.toString());
|
221
|
if ( english == null || ! english.getUuid().equals(englishUuid)){
|
222
|
return true;
|
223
|
}else{
|
224
|
return false;
|
225
|
}
|
226
|
}
|
227
|
|
228
|
|
229
|
/**
|
230
|
* Changes the ApplicationContext to the new dataSource
|
231
|
* @param dataSource
|
232
|
*/
|
233
|
public boolean changeDataSource(ICdmDataSource dataSource){
|
234
|
//logger.info("Change datasource to : " + dataSource);
|
235
|
return setNewDataSource(dataSource, DbSchemaValidation.VALIDATE, false);
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* Changes the ApplicationContext to the new dataSource
|
240
|
* @param dataSource
|
241
|
* @param dbSchemaValidation
|
242
|
*/
|
243
|
public boolean changeDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation){
|
244
|
//logger.info("Change datasource to : " + dataSource);
|
245
|
return setNewDataSource(dataSource, dbSchemaValidation, false);
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Changes the ApplicationContext to the new dataSource
|
250
|
* @param dataSource
|
251
|
* @param dbSchemaValidation
|
252
|
* @param omitTermLoading
|
253
|
*/
|
254
|
public boolean changeDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading){
|
255
|
logger.info("Change datasource to : " + dataSource);
|
256
|
return setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading);
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Sets a new application Context.
|
261
|
* @param ac
|
262
|
*/
|
263
|
public void setApplicationContext(AbstractApplicationContext ac){
|
264
|
closeApplicationContext(); //closes old application context if necessary
|
265
|
applicationContext = ac;
|
266
|
applicationContext.registerShutdownHook();
|
267
|
init();
|
268
|
}
|
269
|
|
270
|
/* (non-Javadoc)
|
271
|
* @see java.lang.Object#finalize()
|
272
|
*/
|
273
|
public void finalize(){
|
274
|
close();
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* closes the application
|
279
|
*/
|
280
|
public void close(){
|
281
|
closeApplicationContext();
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* closes the application context
|
286
|
*/
|
287
|
private void closeApplicationContext(){
|
288
|
if (applicationContext != null){
|
289
|
logger.info("Close ApplicationContext");
|
290
|
applicationContext.close();
|
291
|
}
|
292
|
}
|
293
|
|
294
|
private void init(){
|
295
|
logger.debug("Init " + this.getClass().getName() + " ... ");
|
296
|
if (logger.isDebugEnabled()){for (String beanName : applicationContext.getBeanDefinitionNames()){ logger.debug(beanName);}}
|
297
|
//TODO delete next row (was just for testing)
|
298
|
if (logger.isInfoEnabled()){
|
299
|
logger.info("Registered Beans: ");
|
300
|
String[] beans = applicationContext.getBeanDefinitionNames();
|
301
|
for (String bean:beans){
|
302
|
logger.info(bean);
|
303
|
}
|
304
|
}
|
305
|
configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationDefaultConfiguration");
|
306
|
getDatabaseService().setApplicationController(this);
|
307
|
}
|
308
|
|
309
|
|
310
|
|
311
|
/* ****** Services *********/
|
312
|
public final INameService getNameService(){
|
313
|
return configuration.getNameService();
|
314
|
}
|
315
|
|
316
|
public final ITaxonService getTaxonService(){
|
317
|
return configuration.getTaxonService();
|
318
|
}
|
319
|
|
320
|
public final ITaxonTreeService getTaxonTreeService(){
|
321
|
return configuration.getTaxonTreeService();
|
322
|
}
|
323
|
|
324
|
public final ITaxonNodeService getTaxonNodeService(){
|
325
|
return configuration.getTaxonNodeService();
|
326
|
}
|
327
|
|
328
|
public final IReferenceService getReferenceService(){
|
329
|
return configuration.getReferenceService();
|
330
|
}
|
331
|
|
332
|
public final IAgentService getAgentService(){
|
333
|
return configuration.getAgentService();
|
334
|
}
|
335
|
|
336
|
public final IDatabaseService getDatabaseService(){
|
337
|
return configuration.getDatabaseService();
|
338
|
}
|
339
|
|
340
|
public final ITermService getTermService(){
|
341
|
return configuration.getTermService();
|
342
|
}
|
343
|
|
344
|
public final IDescriptionService getDescriptionService(){
|
345
|
return configuration.getDescriptionService();
|
346
|
}
|
347
|
|
348
|
public final IOccurrenceService getOccurrenceService(){
|
349
|
return configuration.getOccurrenceService();
|
350
|
}
|
351
|
|
352
|
public final IMediaService getMediaService(){
|
353
|
return configuration.getMediaService();
|
354
|
}
|
355
|
|
356
|
public final ICommonService getCommonService(){
|
357
|
return configuration.getCommonService();
|
358
|
}
|
359
|
|
360
|
public final ILocationService getLocationService(){
|
361
|
return configuration.getLocationService();
|
362
|
}
|
363
|
|
364
|
public final IUserService getUserService(){
|
365
|
return configuration.getUserService();
|
366
|
}
|
367
|
|
368
|
public final ICollectionService getCollectionService(){
|
369
|
return configuration.getCollectionService();
|
370
|
}
|
371
|
|
372
|
public final IFeatureTreeService getFeatureTreeService(){
|
373
|
return configuration.getFeatureTreeService();
|
374
|
}
|
375
|
|
376
|
public final IFeatureNodeService getFeatureNodeService(){
|
377
|
return configuration.getFeatureNodeService();
|
378
|
}
|
379
|
|
380
|
public final IVocabularyService getVocabularyService(){
|
381
|
return configuration.getVocabularyService();
|
382
|
}
|
383
|
|
384
|
public final IService<CdmBase> getMainService(){
|
385
|
return configuration.getMainService();
|
386
|
}
|
387
|
|
388
|
public final ConversationHolder NewConversation(){
|
389
|
//return (ConversationHolder)applicationContext.getBean("conversationHolder");
|
390
|
return configuration.NewConversation();
|
391
|
}
|
392
|
|
393
|
|
394
|
public final ProviderManager getAuthenticationManager(){
|
395
|
return configuration.getAuthenticationManager();
|
396
|
}
|
397
|
|
398
|
public final Object getBean(String name){
|
399
|
return this.applicationContext.getBean(name);
|
400
|
}
|
401
|
|
402
|
/*
|
403
|
* OLD TRANSACTION STUFF
|
404
|
*/
|
405
|
|
406
|
/* **** flush ***********/
|
407
|
public void flush() {
|
408
|
SessionFactory sf = (SessionFactory)applicationContext.getBean("sessionFactory");
|
409
|
sf.getCurrentSession().flush();
|
410
|
}
|
411
|
|
412
|
public SessionFactory getSessionFactory(){
|
413
|
return (SessionFactory)applicationContext.getBean("sessionFactory");
|
414
|
}
|
415
|
|
416
|
public TransactionStatus startTransaction() {
|
417
|
|
418
|
return startTransaction(false);
|
419
|
}
|
420
|
|
421
|
public TransactionStatus startTransaction(Boolean readOnly) {
|
422
|
|
423
|
PlatformTransactionManager txManager = configuration.getTransactionManager();
|
424
|
|
425
|
DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
|
426
|
defaultTxDef.setReadOnly(readOnly);
|
427
|
TransactionDefinition txDef = defaultTxDef;
|
428
|
|
429
|
// Log some transaction-related debug information.
|
430
|
if (logger.isDebugEnabled()) {
|
431
|
logger.debug("Transaction name = " + txDef.getName());
|
432
|
logger.debug("Transaction facets:");
|
433
|
logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
|
434
|
logger.debug("Isolation level = " + txDef.getIsolationLevel());
|
435
|
logger.debug("Timeout = " + txDef.getTimeout());
|
436
|
logger.debug("Read Only = " + txDef.isReadOnly());
|
437
|
// org.springframework.orm.hibernate3.HibernateTransactionManager
|
438
|
// provides more transaction/session-related debug information.
|
439
|
}
|
440
|
|
441
|
TransactionStatus txStatus = txManager.getTransaction(txDef);
|
442
|
return txStatus;
|
443
|
}
|
444
|
|
445
|
public void commitTransaction(TransactionStatus txStatus){
|
446
|
PlatformTransactionManager txManager = configuration.getTransactionManager();
|
447
|
txManager.commit(txStatus);
|
448
|
return;
|
449
|
}
|
450
|
}
|