Project

General

Profile

Download (13.3 KB) Statistics
| Branch: | Tag: | Revision:
1 25663b56 Andreas Müller
/**
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 2d993c6e Andreas Müller
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 d0fa1dab a.babadshanjan
import org.springframework.beans.MutablePropertyValues;
17 2d993c6e Andreas Müller
import org.springframework.beans.factory.BeanCreationException;
18 5d488fcd m.doering
import org.springframework.beans.factory.config.BeanDefinition;
19
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
20 2d993c6e Andreas Müller
import org.springframework.context.support.AbstractApplicationContext;
21 5d488fcd m.doering
import org.springframework.context.support.GenericApplicationContext;
22
import org.springframework.core.io.ClassPathResource;
23 3ebef624 Andreas Müller
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 2d993c6e Andreas Müller
28 8987f16f Andreas Müller
import eu.etaxonomy.cdm.api.application.eclipse.EclipseRcpSaveGenericApplicationContext;
29 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.api.service.IAgentService;
30 6a2356a9 Andreas Müller
import eu.etaxonomy.cdm.api.service.ICommonService;
31 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.api.service.IDatabaseService;
32 b3b6d9c1 Andreas Müller
import eu.etaxonomy.cdm.api.service.IDescriptionService;
33 fdf5453b a.babadshanjan
import eu.etaxonomy.cdm.api.service.IMediaService;
34 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.api.service.INameService;
35 33e9f6a3 a.babadshanjan
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
36 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.api.service.IReferenceService;
37 e7f8dc70 a.babadshanjan
import eu.etaxonomy.cdm.api.service.IService;
38 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.api.service.ITaxonService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40 130645df Andreas Müller
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
41 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
42 e80e2674 Andreas Müller
import eu.etaxonomy.cdm.database.DbSchemaValidation;
43 6a2356a9 Andreas Müller
import eu.etaxonomy.cdm.database.ICdmDataSource;
44 e7f8dc70 a.babadshanjan
import eu.etaxonomy.cdm.model.common.CdmBase;
45 2d993c6e Andreas Müller
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
46 003c33af Andreas Müller
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
47 130645df Andreas Müller
48 2d993c6e Andreas Müller
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 d8e15f8a Andreas Müller
	private ICdmApplicationConfiguration configuration;
58 2d993c6e Andreas Müller
	
59 80be8b22 Andreas Müller
	final static DbSchemaValidation defaultDbSchemaValidation = DbSchemaValidation.VALIDATE;
60 8dd5dcc7 Andreas Müller
	
61
	
62 2d993c6e Andreas Müller
	/**
63
	 * Constructor, opens an spring 2.5 ApplicationContext by using the default data source
64
	 */
65 003c33af Andreas Müller
	public static CdmApplicationController NewInstance()  throws DataSourceNotFoundException, TermNotFoundException {
66 2d993c6e Andreas Müller
		logger.info("Start CdmApplicationController with default data source");
67 130645df Andreas Müller
		CdmPersistentDataSource dataSource = CdmPersistentDataSource.NewDefaultInstance();
68 80be8b22 Andreas Müller
		DbSchemaValidation dbSchemaValidation = defaultDbSchemaValidation;
69
		return new CdmApplicationController(dataSource, dbSchemaValidation);
70 2d993c6e Andreas Müller
	}
71 8dd5dcc7 Andreas Müller
72 2d993c6e Andreas Müller
	
73 a9f25d61 Andreas Müller
	
74
	/**
75
	 * Constructor, opens an spring 2.5 ApplicationContext by using the default data source
76 80be8b22 Andreas Müller
	 * @param dbSchemaValidation validation type for database schema
77 a9f25d61 Andreas Müller
	 */
78 003c33af Andreas Müller
	public static CdmApplicationController NewInstance(DbSchemaValidation dbSchemaValidation)  throws DataSourceNotFoundException, TermNotFoundException {
79 a9f25d61 Andreas Müller
		logger.info("Start CdmApplicationController with default data source");
80 130645df Andreas Müller
		CdmPersistentDataSource dataSource = CdmPersistentDataSource.NewDefaultInstance();
81 80be8b22 Andreas Müller
		if (dbSchemaValidation == null){
82
			dbSchemaValidation = defaultDbSchemaValidation;
83 a9f25d61 Andreas Müller
		}
84 80be8b22 Andreas Müller
		return new CdmApplicationController(dataSource, dbSchemaValidation);
85 a9f25d61 Andreas Müller
	}
86 8dd5dcc7 Andreas Müller
87 a9f25d61 Andreas Müller
	
88 2d993c6e Andreas Müller
	/**
89 8dd5dcc7 Andreas Müller
	 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source and the
90
	 * default database schema validation type
91 2d993c6e Andreas Müller
	 * @param dataSource
92
	 */
93 069095b0 a.babadshanjan
	public static CdmApplicationController NewInstance(ICdmDataSource dataSource) 
94
	throws DataSourceNotFoundException, TermNotFoundException{
95 80be8b22 Andreas Müller
		return new CdmApplicationController(dataSource, defaultDbSchemaValidation);
96 add8b0a9 Andreas Müller
	}
97
	
98 069095b0 a.babadshanjan
	public static CdmApplicationController NewInstance(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation) 
99
	throws DataSourceNotFoundException, TermNotFoundException{
100 80be8b22 Andreas Müller
		return new CdmApplicationController(dataSource, dbSchemaValidation);
101 8dd5dcc7 Andreas Müller
	}
102
103 069095b0 a.babadshanjan
	public static CdmApplicationController NewInstance(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading) 
104
	throws DataSourceNotFoundException, TermNotFoundException{
105
		return new CdmApplicationController(dataSource, dbSchemaValidation, omitTermLoading);
106
	}
107
	
108 add8b0a9 Andreas Müller
	/**
109
	 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
110
	 * @param dataSource
111 069095b0 a.babadshanjan
	 * @param dbSchemaValidation
112 add8b0a9 Andreas Müller
	 */
113 003c33af Andreas Müller
	private CdmApplicationController(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation) throws DataSourceNotFoundException, TermNotFoundException{
114 069095b0 a.babadshanjan
        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 8dd5dcc7 Andreas Müller
		logger.info("Start CdmApplicationController with datasource: " + dataSource.getName());
126 069095b0 a.babadshanjan
		if (setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading) == false){
127 2d993c6e Andreas Müller
			throw new DataSourceNotFoundException("Wrong datasource: " + dataSource );
128
		}
129 8dd5dcc7 Andreas Müller
	}
130 2d993c6e Andreas Müller
	
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 069095b0 a.babadshanjan
	private boolean setNewDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading) throws TermNotFoundException {
136 80be8b22 Andreas Müller
		if (dbSchemaValidation == null){
137
			dbSchemaValidation = defaultDbSchemaValidation;
138 add8b0a9 Andreas Müller
		}
139 2d993c6e Andreas Müller
		logger.info("Connecting to '" + dataSource.getName() + "'");
140 5d488fcd m.doering
141
142
		GenericApplicationContext appContext;
143 2d993c6e Andreas Müller
		try {
144 8987f16f Andreas Müller
			appContext = new EclipseRcpSaveGenericApplicationContext();
145 5d488fcd m.doering
			
146
			BeanDefinition datasourceBean = dataSource.getDatasourceBean();
147 373ab72b Andreas Müller
			datasourceBean.setAttribute("isLazy", false);
148 5d488fcd m.doering
			appContext.registerBeanDefinition("dataSource", datasourceBean);
149
			
150 80be8b22 Andreas Müller
			BeanDefinition hibernatePropBean= dataSource.getHibernatePropertiesBean(dbSchemaValidation);
151 5d488fcd m.doering
			appContext.registerBeanDefinition("hibernateProperties", hibernatePropBean);
152
			
153
			XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext);
154 13422b4b Andreas Müller
			xmlReader.loadBeanDefinitions(new ClassPathResource("/eu/etaxonomy/cdm/defaultApplicationContext.xml"));		 
155 5d488fcd m.doering
			
156 d0fa1dab a.babadshanjan
			//omitTerms
157
			String initializerName = "persistentTermInitializer";
158
			BeanDefinition beanDef = appContext.getBeanDefinition(initializerName);
159
			MutablePropertyValues values = beanDef.getPropertyValues();
160
			values.addPropertyValue("omit", omitTermLoading);
161 069095b0 a.babadshanjan
			
162 5d488fcd m.doering
			appContext.refresh();
163
			appContext.start();
164 069095b0 a.babadshanjan
			
165 2d993c6e Andreas Müller
		} catch (BeanCreationException e) {
166
			// create new schema
167 80be8b22 Andreas Müller
			if (dbSchemaValidation == DbSchemaValidation.VALIDATE) {
168 add8b0a9 Andreas Müller
				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 80be8b22 Andreas Müller
					" Try to run CdmApplicationController with dbSchemaValidation.CREATE or dbSchemaValidation.UPDATE option. ");
172 add8b0a9 Andreas Müller
			} else {
173 80be8b22 Andreas Müller
				logger.error("BeanCreationException (CdmApplicationController startet with " + dbSchemaValidation.toString() + " option.");
174 add8b0a9 Andreas Müller
			}
175
			e.printStackTrace();
176
			return false;
177 2d993c6e Andreas Müller
		}
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 800cb472 Andreas Müller
		DefinedTermBase<?> english = this.getTermService().getTermByUri(englishUuid.toString());
190 2d993c6e Andreas Müller
		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 401f33de Andreas Müller
	public boolean changeDataSource(ICdmDataSource dataSource) throws TermNotFoundException {
203 069095b0 a.babadshanjan
		//logger.info("Change datasource to : " + dataSource);
204
		return setNewDataSource(dataSource, DbSchemaValidation.VALIDATE, false);
205 add8b0a9 Andreas Müller
	}
206
	
207
	/**
208
	 * Changes the ApplicationContext to the new dataSource
209
	 * @param dataSource
210 069095b0 a.babadshanjan
	 * @param dbSchemaValidation
211 add8b0a9 Andreas Müller
	 */
212 401f33de Andreas Müller
	public boolean changeDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation)  throws TermNotFoundException {
213 069095b0 a.babadshanjan
		//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 401f33de Andreas Müller
	public boolean changeDataSource(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)  
224 069095b0 a.babadshanjan
	throws TermNotFoundException {
225 add8b0a9 Andreas Müller
		logger.info("Change datasource to : " + dataSource);
226 069095b0 a.babadshanjan
		return setNewDataSource(dataSource, dbSchemaValidation, omitTermLoading);
227 2d993c6e Andreas Müller
	}
228
	
229
	/**
230
	 * Sets a new application Context.
231
	 * @param ac
232
	 */
233 5d488fcd m.doering
	public void setApplicationContext(AbstractApplicationContext ac){
234 2d993c6e Andreas Müller
		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 8987f16f Andreas Müller
		//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 d8e15f8a Andreas Müller
		configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationDefaultConfiguration");
276
		getDatabaseService().setApplicationController(this);
277 2d993c6e Andreas Müller
	}
278
	
279
280
	
281
	/* ******  Services *********/
282
	public final INameService getNameService(){
283 d8e15f8a Andreas Müller
		return configuration.getNameService();
284 2d993c6e Andreas Müller
	}
285
286
	public final ITaxonService getTaxonService(){
287 d8e15f8a Andreas Müller
		return configuration.getTaxonService();
288 2d993c6e Andreas Müller
	}
289
290
	public final IReferenceService getReferenceService(){
291 d8e15f8a Andreas Müller
		return configuration.getReferenceService();
292 2d993c6e Andreas Müller
	}
293
	
294
	public final IAgentService getAgentService(){
295 d8e15f8a Andreas Müller
		return configuration.getAgentService();
296 2d993c6e Andreas Müller
	}
297
	
298
	public final IDatabaseService getDatabaseService(){
299 d8e15f8a Andreas Müller
		return configuration.getDatabaseService();
300 2d993c6e Andreas Müller
	}
301
	
302
	public final ITermService getTermService(){
303 d8e15f8a Andreas Müller
		return configuration.getTermService();
304 2d993c6e Andreas Müller
	}
305 b3b6d9c1 Andreas Müller
306
	public final IDescriptionService getDescriptionService(){
307
		return configuration.getDescriptionService();
308
	}
309 2d993c6e Andreas Müller
	
310 33e9f6a3 a.babadshanjan
	public final IOccurrenceService getOccurrenceService(){
311
		return configuration.getOccurrenceService();
312
	}
313
314 fdf5453b a.babadshanjan
	public final IMediaService getMediaService(){
315
		return configuration.getMediaService();
316
	}
317
318 6a2356a9 Andreas Müller
	public final ICommonService getCommonService(){
319
		return configuration.getCommonService();
320
	}
321
	
322 e7f8dc70 a.babadshanjan
	public final IService<CdmBase> getMainService(){
323
		return configuration.getMainService();
324
	}
325 2c398184 p.kelbert
	
326 2d993c6e Andreas Müller
	/* **** flush ***********/
327
	public void flush() {
328
		SessionFactory sf = (SessionFactory)applicationContext.getBean("sessionFactory");
329
		sf.getCurrentSession().flush();
330
	}
331 3ebef624 Andreas Müller
	
332 e7e13200 a.babadshanjan
	public TransactionStatus startTransaction() {
333 3ebef624 Andreas Müller
		
334 e7e13200 a.babadshanjan
		return startTransaction(false);
335 3ebef624 Andreas Müller
	}
336
	
337 d470e656 a.babadshanjan
	public TransactionStatus startTransaction(Boolean readOnly) {
338 e7e13200 a.babadshanjan
		
339 d470e656 a.babadshanjan
		PlatformTransactionManager txManager = configuration.getTransactionManager();
340 e7e13200 a.babadshanjan
		
341 d470e656 a.babadshanjan
		DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
342
		defaultTxDef.setReadOnly(readOnly);
343
		TransactionDefinition txDef = defaultTxDef;
344 9121992f a.babadshanjan
345
		// Log some transaction-related debug information.
346 7b567c03 a.babadshanjan
		if (logger.isDebugEnabled()) {
347
			logger.debug("Transaction name = " + txDef.getName());
348
			logger.debug("Transaction facets:");
349
			logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
350
			logger.debug("Isolation level = " + txDef.getIsolationLevel());
351
			logger.debug("Timeout = " + txDef.getTimeout());
352
			logger.debug("Read Only = " + txDef.isReadOnly());
353
			// org.springframework.orm.hibernate3.HibernateTransactionManager
354
			// provides more transaction/session-related debug information.
355
		}
356 d470e656 a.babadshanjan
		
357
		TransactionStatus txStatus = txManager.getTransaction(txDef);
358
		return txStatus;
359
	}
360
361 3ebef624 Andreas Müller
	public void commitTransaction(TransactionStatus txStatus){
362
		PlatformTransactionManager txManager = configuration.getTransactionManager();
363
		txManager.commit(txStatus);
364
		return;
365
	}
366 d8e15f8a Andreas Müller
367 2d993c6e Andreas Müller
}