Project

General

Profile

Download (3.33 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.application;
2

    
3
import org.apache.log4j.Logger;
4
import org.hibernate.SessionFactory;
5
import org.hsqldb.Server;
6
import org.springframework.context.support.ClassPathXmlApplicationContext;
7

    
8
import eu.etaxonomy.cdm.api.service.IAgentService;
9
import eu.etaxonomy.cdm.api.service.IDatabaseService;
10
import eu.etaxonomy.cdm.api.service.INameService;
11
import eu.etaxonomy.cdm.database.CdmDataSource;
12

    
13

    
14
/**
15
 * @author a.mueller
16
 *
17
 */
18
public class CdmApplicationController {
19
	private static final Logger logger = Logger.getLogger(CdmApplicationController.class);
20
	
21
	static final String APP_CONTEXT_FILE_NAME = "applicationContext.xml";
22
	
23
	
24
	
25
	private ClassPathXmlApplicationContext applicationContext;
26
	private INameService nameService;
27
	private IAgentService agentService;
28
	private IDatabaseService databaseService;
29
	
30
	
31
	private Server hsqldbServer;
32
	
33
	
34
	/**
35
	 * Constructor, opens an spring 2.5 ApplicationContext by using the default data source
36
	 * @param dataSource
37
	 */
38
	public CdmApplicationController() {
39
		//logger.info("Start HSQLDB Server");
40
		//startHsqldbServer();
41
		
42
		//TODO find out if DataSource is localHsqldb,
43
		//if yes then find out if Server is running
44
		//if not running, start server
45

    
46
		logger.info("Start CdmApplicationController with default data source");
47
		CdmDataSource dataSource = CdmDataSource.getDefaultDataSource();
48
		dataSource.updateSessionFactory();
49
		String appContextFileName = APP_CONTEXT_FILE_NAME;
50
		setApplicationContext(new ClassPathXmlApplicationContext(appContextFileName));
51
	}
52
	
53
	/**
54
	 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
55
	 * @param dataSource
56
	 */
57
	public CdmApplicationController(CdmDataSource dataSource) {
58
		logger.info("Start CdmApplicationController with datasource: " + dataSource);
59
		dataSource.updateSessionFactory();
60
		String appContextFileName = APP_CONTEXT_FILE_NAME;
61
		setApplicationContext(new ClassPathXmlApplicationContext(appContextFileName));
62
	}
63

    
64
	
65
	/**
66
	 * Sets a new application Context.
67
	 * @param appCtx
68
	 */
69
	public void setApplicationContext(ClassPathXmlApplicationContext appCtx){
70
		applicationContext = appCtx;
71
		applicationContext.registerShutdownHook();
72
		setServices();
73
	}
74
	
75
	/* (non-Javadoc)
76
	 * @see java.lang.Object#finalize()
77
	 */
78
	public void finalize(){
79
		close();
80
	}
81
	
82
	/**
83
	 * closes the application
84
	 */
85
	public void close(){
86
		if (applicationContext != null)
87
			logger.info("Close ApplicationContext");
88
			applicationContext.close();
89
	}
90
	
91
	private void setServices(){
92
		//TODO ? also possible via SPRING?
93
		nameService = (INameService)applicationContext.getBean("nameServiceImpl");
94
		agentService = (IAgentService)applicationContext.getBean("agentServiceImpl");
95
		databaseService = (IDatabaseService)applicationContext.getBean("databaseServiceHibernateImpl");
96
	}
97
	
98

    
99
	
100
	/* ******  Services *********/
101
	public final INameService getNameService(){
102
		return this.nameService;
103
	}
104
	
105
	public final IAgentService getAgentService(){
106
		return this.agentService;
107
	}
108
	
109
	public final IDatabaseService getDatabaseService(){
110
		return this.databaseService;
111
	}
112
	
113
	
114
	/* **** flush ***********/
115
	public void flush() {
116
		SessionFactory sf = (SessionFactory)applicationContext.getBean("sessionFactory");
117
		sf.getCurrentSession().flush();
118
	}
119
		
120
		
121
	
122
}
    (1-1/1)