1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.api.application;
|
11
|
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.hibernate.SessionFactory;
|
16
|
import org.springframework.beans.MutablePropertyValues;
|
17
|
import org.springframework.beans.factory.BeanCreationException;
|
18
|
import org.springframework.beans.factory.config.BeanDefinition;
|
19
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
20
|
import org.springframework.context.support.AbstractApplicationContext;
|
21
|
import org.springframework.context.support.GenericApplicationContext;
|
22
|
import org.springframework.core.io.ClassPathResource;
|
23
|
import org.springframework.transaction.PlatformTransactionManager;
|
24
|
import org.springframework.transaction.TransactionDefinition;
|
25
|
import org.springframework.transaction.TransactionStatus;
|
26
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.application.eclipse.EclipseRcpSaveGenericApplicationContext;
|
29
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
30
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
31
|
import eu.etaxonomy.cdm.api.service.IDatabaseService;
|
32
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
33
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
34
|
import eu.etaxonomy.cdm.api.service.INameService;
|
35
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
36
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
37
|
import eu.etaxonomy.cdm.api.service.IService;
|
38
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
39
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
40
|
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
|
41
|
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
|
42
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
43
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
44
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
45
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
46
|
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
|
47
|
|
48
|
|
49
|
/**
|
50
|
* @author a.mueller
|
51
|
*
|
52
|
*/
|
53
|
public class CdmApplicationController {
|
54
|
private static final Logger logger = Logger.getLogger(CdmApplicationController.class);
|
55
|
|
56
|
public AbstractApplicationContext applicationContext;
|
57
|
private ICdmApplicationConfiguration configuration;
|
58
|
|
59
|
final static DbSchemaValidation defaultDbSchemaValidation = DbSchemaValidation.VALIDATE;
|
60
|
|
61
|
|
62
|
/**
|
63
|
* Constructor, opens an spring 2.5 ApplicationContext by using the default data source
|
64
|
*/
|
65
|
public static CdmApplicationController NewInstance() throws DataSourceNotFoundException, TermNotFoundException {
|
66
|
logger.info("Start CdmApplicationController with default data source");
|
67
|
CdmPersistentDataSource dataSource = CdmPersistentDataSource.NewDefaultInstance();
|
68
|
DbSchemaValidation dbSchemaValidation = defaultDbSchemaValidation;
|
69
|
return new CdmApplicationController(dataSource, dbSchemaValidation);
|
70
|
}
|
71
|
|
72
|
|
73
|
|
74
|
/**
|
75
|
* Constructor, opens an spring 2.5 ApplicationContext by using the default data source
|
76
|
* @param dbSchemaValidation validation type for database schema
|
77
|
*/
|
78
|
public static CdmApplicationController NewInstance(DbSchemaValidation dbSchemaValidation) throws DataSourceNotFoundException, TermNotFoundException {
|
79
|
logger.info("Start CdmApplicationController with default data source");
|
80
|
CdmPersistentDataSource dataSource = CdmPersistentDataSource.NewDefaultInstance();
|
81
|
if (dbSchemaValidation == null){
|
82
|
dbSchemaValidation = defaultDbSchemaValidation;
|
83
|
}
|
84
|
return new CdmApplicationController(dataSource, dbSchemaValidation);
|
85
|
}
|
86
|
|
87
|
|
88
|
/**
|
89
|
* Constructor, opens an spring 2.5 ApplicationContext by using the according data source and the
|
90
|
* default database schema validation type
|
91
|
* @param dataSource
|
92
|
*/
|
93
|
public static CdmApplicationController NewInstance(ICdmDataSource dataSource)
|
94
|
throws DataSourceNotFoundException, TermNotFoundException{
|
95
|
return new CdmApplicationController(dataSource, defaultDbSchemaValidation);
|
96
|
}
|
97
|
|
98
|
public static CdmApplicationController NewInstance(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation)
|
99
|
throws DataSourceNotFoundException, TermNotFoundException{
|
100
|
return new CdmApplicationController(dataSource, dbSchemaValidation);
|
101
|
}
|
102
|
|
103
|
public static CdmApplicationController NewInstance(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
|
104
|
throws DataSourceNotFoundException, TermNotFoundException{
|
105
|
return new CdmApplicationController(dataSource, dbSchemaValidation, omitTermLoading);
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Constructor, opens an spring 2.5 ApplicationContext by using the according data source
|
110
|
* @param dataSource
|
111
|
* @param dbSchemaValidation
|
112
|
*/
|
113
|
private CdmApplicationController(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation) throws DataSourceNotFoundException, TermNotFoundException{
|
114
|
this(dataSource, dbSchemaValidation, false);
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Constructor, opens an spring 2.5 ApplicationContext by using the according data source
|
119
|
* @param dataSource
|
120
|
* @param dbSchemaValidation
|
121
|
* @param omitTermLoading
|
122
|
*/
|
123
|
private CdmApplicationController(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
|
124
|
throws DataSourceNotFoundException, TermNotFoundException{
|
125
|
logger.info("Start CdmApplicationController with datasource: " + dataSource.getName());
|
126
|
if (setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading) == false){
|
127
|
throw new DataSourceNotFoundException("Wrong datasource: " + dataSource );
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Sets the application context to a new spring ApplicationContext by using the according data source and initializes the Controller.
|
133
|
* @param dataSource
|
134
|
*/
|
135
|
private boolean setNewDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading) throws TermNotFoundException {
|
136
|
if (dbSchemaValidation == null){
|
137
|
dbSchemaValidation = defaultDbSchemaValidation;
|
138
|
}
|
139
|
logger.info("Connecting to '" + dataSource.getName() + "'");
|
140
|
|
141
|
|
142
|
GenericApplicationContext appContext;
|
143
|
try {
|
144
|
appContext = new EclipseRcpSaveGenericApplicationContext();
|
145
|
|
146
|
BeanDefinition datasourceBean = dataSource.getDatasourceBean();
|
147
|
datasourceBean.setAttribute("isLazy", false);
|
148
|
appContext.registerBeanDefinition("dataSource", datasourceBean);
|
149
|
|
150
|
BeanDefinition hibernatePropBean= dataSource.getHibernatePropertiesBean(dbSchemaValidation);
|
151
|
appContext.registerBeanDefinition("hibernateProperties", hibernatePropBean);
|
152
|
|
153
|
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext);
|
154
|
xmlReader.loadBeanDefinitions(new ClassPathResource("/eu/etaxonomy/cdm/defaultApplicationContext.xml"));
|
155
|
|
156
|
//omitTerms
|
157
|
String initializerName = "persistentTermInitializer";
|
158
|
BeanDefinition beanDef = appContext.getBeanDefinition(initializerName);
|
159
|
MutablePropertyValues values = beanDef.getPropertyValues();
|
160
|
values.addPropertyValue("omit", omitTermLoading);
|
161
|
|
162
|
appContext.refresh();
|
163
|
appContext.start();
|
164
|
|
165
|
} catch (BeanCreationException e) {
|
166
|
// create new schema
|
167
|
if (dbSchemaValidation == DbSchemaValidation.VALIDATE) {
|
168
|
logger.error("ApplicationContext could not be created. " +
|
169
|
" Maybe your database schema is not up-to-date, " +
|
170
|
" but there might be other BeanCreation problems too." +
|
171
|
" Try to run CdmApplicationController with dbSchemaValidation.CREATE or dbSchemaValidation.UPDATE option. ");
|
172
|
} else {
|
173
|
logger.error("BeanCreationException (CdmApplicationController startet with " + dbSchemaValidation.toString() + " option.");
|
174
|
}
|
175
|
e.printStackTrace();
|
176
|
return false;
|
177
|
}
|
178
|
setApplicationContext(appContext);
|
179
|
return true;
|
180
|
}
|
181
|
|
182
|
|
183
|
/**
|
184
|
* Tests if some DefinedTermsAreMissing.
|
185
|
* @return true, if at least one is missing, else false
|
186
|
*/
|
187
|
public boolean testDefinedTermsAreMissing(){
|
188
|
UUID englishUuid = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
|
189
|
DefinedTermBase<?> english = this.getTermService().getTermByUri(englishUuid.toString());
|
190
|
if ( english == null || ! english.getUuid().equals(englishUuid)){
|
191
|
return true;
|
192
|
}else{
|
193
|
return false;
|
194
|
}
|
195
|
}
|
196
|
|
197
|
|
198
|
/**
|
199
|
* Changes the ApplicationContext to the new dataSource
|
200
|
* @param dataSource
|
201
|
*/
|
202
|
public boolean changeDataSource(ICdmDataSource dataSource) throws TermNotFoundException {
|
203
|
//logger.info("Change datasource to : " + dataSource);
|
204
|
return setNewDataSource(dataSource, DbSchemaValidation.VALIDATE, false);
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Changes the ApplicationContext to the new dataSource
|
209
|
* @param dataSource
|
210
|
* @param dbSchemaValidation
|
211
|
*/
|
212
|
public boolean changeDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation) throws TermNotFoundException {
|
213
|
//logger.info("Change datasource to : " + dataSource);
|
214
|
return setNewDataSource(dataSource, dbSchemaValidation, false);
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Changes the ApplicationContext to the new dataSource
|
219
|
* @param dataSource
|
220
|
* @param dbSchemaValidation
|
221
|
* @param omitTermLoading
|
222
|
*/
|
223
|
public boolean changeDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
|
224
|
throws TermNotFoundException {
|
225
|
logger.info("Change datasource to : " + dataSource);
|
226
|
return setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading);
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Sets a new application Context.
|
231
|
* @param ac
|
232
|
*/
|
233
|
public void setApplicationContext(AbstractApplicationContext ac){
|
234
|
closeApplicationContext(); //closes old application context if necessary
|
235
|
applicationContext = ac;
|
236
|
applicationContext.registerShutdownHook();
|
237
|
init();
|
238
|
}
|
239
|
|
240
|
/* (non-Javadoc)
|
241
|
* @see java.lang.Object#finalize()
|
242
|
*/
|
243
|
public void finalize(){
|
244
|
close();
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* closes the application
|
249
|
*/
|
250
|
public void close(){
|
251
|
closeApplicationContext();
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* closes the application context
|
256
|
*/
|
257
|
private void closeApplicationContext(){
|
258
|
if (applicationContext != null){
|
259
|
logger.info("Close ApplicationContext");
|
260
|
applicationContext.close();
|
261
|
}
|
262
|
}
|
263
|
|
264
|
private void init(){
|
265
|
logger.debug("Init " + this.getClass().getName() + " ... ");
|
266
|
if (logger.isDebugEnabled()){for (String beanName : applicationContext.getBeanDefinitionNames()){ logger.debug(beanName);}}
|
267
|
//TODO delete next row (was just for testing)
|
268
|
if (logger.isInfoEnabled()){
|
269
|
logger.info("Registered Beans: ");
|
270
|
String[] beans = applicationContext.getBeanDefinitionNames();
|
271
|
for (String bean:beans){
|
272
|
logger.info(bean);
|
273
|
}
|
274
|
}
|
275
|
configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationDefaultConfiguration");
|
276
|
getDatabaseService().setApplicationController(this);
|
277
|
}
|
278
|
|
279
|
|
280
|
|
281
|
/* ****** Services *********/
|
282
|
public final INameService getNameService(){
|
283
|
return configuration.getNameService();
|
284
|
}
|
285
|
|
286
|
public final ITaxonService getTaxonService(){
|
287
|
return configuration.getTaxonService();
|
288
|
}
|
289
|
|
290
|
public final IReferenceService getReferenceService(){
|
291
|
return configuration.getReferenceService();
|
292
|
}
|
293
|
|
294
|
public final IAgentService getAgentService(){
|
295
|
return configuration.getAgentService();
|
296
|
}
|
297
|
|
298
|
public final IDatabaseService getDatabaseService(){
|
299
|
return configuration.getDatabaseService();
|
300
|
}
|
301
|
|
302
|
public final ITermService getTermService(){
|
303
|
return configuration.getTermService();
|
304
|
}
|
305
|
|
306
|
public final IDescriptionService getDescriptionService(){
|
307
|
return configuration.getDescriptionService();
|
308
|
}
|
309
|
|
310
|
public final IOccurrenceService getOccurrenceService(){
|
311
|
return configuration.getOccurrenceService();
|
312
|
}
|
313
|
|
314
|
public final IMediaService getMediaService(){
|
315
|
return configuration.getMediaService();
|
316
|
}
|
317
|
|
318
|
public final ICommonService getCommonService(){
|
319
|
return configuration.getCommonService();
|
320
|
}
|
321
|
|
322
|
public final IService<CdmBase> getMainService(){
|
323
|
return configuration.getMainService();
|
324
|
}
|
325
|
|
326
|
/*
|
327
|
* Conversation
|
328
|
*/
|
329
|
|
330
|
public final ConversationHolder NewConversation(){
|
331
|
return configuration.NewConversation();
|
332
|
}
|
333
|
|
334
|
|
335
|
/*
|
336
|
* OLD TRANSACTION STUFF
|
337
|
*/
|
338
|
|
339
|
/* **** flush ***********/
|
340
|
public void flush() {
|
341
|
SessionFactory sf = (SessionFactory)applicationContext.getBean("sessionFactory");
|
342
|
sf.getCurrentSession().flush();
|
343
|
}
|
344
|
|
345
|
public TransactionStatus startTransaction() {
|
346
|
|
347
|
return startTransaction(false);
|
348
|
}
|
349
|
|
350
|
public TransactionStatus startTransaction(Boolean readOnly) {
|
351
|
|
352
|
PlatformTransactionManager txManager = configuration.getTransactionManager();
|
353
|
|
354
|
DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
|
355
|
defaultTxDef.setReadOnly(readOnly);
|
356
|
TransactionDefinition txDef = defaultTxDef;
|
357
|
|
358
|
// Log some transaction-related debug information.
|
359
|
if (logger.isDebugEnabled()) {
|
360
|
logger.debug("Transaction name = " + txDef.getName());
|
361
|
logger.debug("Transaction facets:");
|
362
|
logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
|
363
|
logger.debug("Isolation level = " + txDef.getIsolationLevel());
|
364
|
logger.debug("Timeout = " + txDef.getTimeout());
|
365
|
logger.debug("Read Only = " + txDef.isReadOnly());
|
366
|
// org.springframework.orm.hibernate3.HibernateTransactionManager
|
367
|
// provides more transaction/session-related debug information.
|
368
|
}
|
369
|
|
370
|
TransactionStatus txStatus = txManager.getTransaction(txDef);
|
371
|
return txStatus;
|
372
|
}
|
373
|
|
374
|
public void commitTransaction(TransactionStatus txStatus){
|
375
|
PlatformTransactionManager txManager = configuration.getTransactionManager();
|
376
|
txManager.commit(txStatus);
|
377
|
return;
|
378
|
}
|
379
|
|
380
|
}
|