1 package eu
.etaxonomy
.cdm
.api
.application
;
3 import java
.io
.FileNotFoundException
;
6 import org
.apache
.log4j
.Logger
;
7 import org
.hibernate
.SessionFactory
;
8 import org
.hsqldb
.Server
;
9 import org
.springframework
.beans
.factory
.BeanCreationException
;
10 import org
.springframework
.context
.support
.AbstractApplicationContext
;
11 import org
.springframework
.context
.support
.AbstractXmlApplicationContext
;
12 import org
.springframework
.context
.support
.FileSystemXmlApplicationContext
;
14 import eu
.etaxonomy
.cdm
.api
.application
.eclipse
.EclipseRcpSaveFileSystemXmlApplicationContext
;
15 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
16 import eu
.etaxonomy
.cdm
.api
.service
.IDatabaseService
;
17 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
18 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
19 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
21 import eu
.etaxonomy
.cdm
.database
.CdmDataSource
;
22 import eu
.etaxonomy
.cdm
.database
.DataSourceNotFoundException
;
23 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.NoDefinedTermClassException
;
25 import eu
.etaxonomy
.cdm
.model
.common
.init
.TermLoader
;
32 public class CdmApplicationController
{
33 private static final Logger logger
= Logger
.getLogger(CdmApplicationController
.class);
35 public AbstractApplicationContext applicationContext
;
36 private INameService nameService
;
37 private ITaxonService taxonService
;
38 private IReferenceService referenceService
;
39 private IAgentService agentService
;
40 private IDatabaseService databaseService
;
41 private ITermService termService
;
43 private Server hsqldbServer
;
50 public String
getHibernateString(){
59 throw new IllegalArgumentException( "Unknown enumeration type" );
66 * Constructor, opens an spring 2.5 ApplicationContext by using the default data source
69 public CdmApplicationController() {
70 logger
.info("Start CdmApplicationController with default data source");
71 CdmDataSource dataSource
= CdmDataSource
.NewDefaultInstance();
72 HBM2DDL hbm2dll
= HBM2DDL
.VALIDATE
;
73 setNewDataSource(dataSource
, hbm2dll
);
77 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
80 public CdmApplicationController(CdmDataSource dataSource
)
81 throws DataSourceNotFoundException
{
82 logger
.info("Start CdmApplicationController with datasource: " + dataSource
);
83 HBM2DDL hbm2dll
= HBM2DDL
.VALIDATE
;
84 if (setNewDataSource(dataSource
, hbm2dll
) == false){
85 throw new DataSourceNotFoundException("Wrong datasource: " + dataSource
);
91 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
94 public CdmApplicationController(CdmDataSource dataSource
, HBM2DDL hbm2dll
)
95 throws DataSourceNotFoundException
{
96 logger
.info("Start CdmApplicationController with datasource: " + dataSource
);
97 if (setNewDataSource(dataSource
, hbm2dll
) == false){
98 throw new DataSourceNotFoundException("Wrong datasource: " + dataSource
);
104 * Sets the application context to a new spring ApplicationContext by using the according data source and initializes the Controller.
107 private boolean setNewDataSource(CdmDataSource dataSource
, HBM2DDL hbm2dll
) {
108 if (hbm2dll
== null){
109 hbm2dll
= hbm2dll
.VALIDATE
;
111 dataSource
.updateSessionFactory(hbm2dll
.getHibernateString());
112 logger
.info("Connecting to '" + dataSource
.getName() + "'");
113 FileSystemXmlApplicationContext appContext
;
115 //logger.debug("Start spring-2.5 ApplicationContex with 'hibernate.hbm2ddl.auto'='default'");
116 appContext
= new EclipseRcpSaveFileSystemXmlApplicationContext(CdmApplicationUtils
.getApplicationContextString());
117 } catch (BeanCreationException e
) {
119 if (hbm2dll
== HBM2DDL
.VALIDATE
) {
120 logger
.error("ApplicationContext could not be created. " +
121 " Maybe your database schema is not up-to-date, " +
122 " but there might be other BeanCreation problems too." +
123 " Try to run CdmApplicationController with hbm2dll.CREATE or hbm2dll.UPDATE option. ");
125 logger
.error("BeanCreationException (CdmApplicationController startet with " + hbm2dll
.getHibernateString() + " option.");
129 // logger.warn("Database schema not up-to-date. Schema must be updated. All DefindeTerms are deleted and created new!");
130 // logger.debug("Start spring-2.5 ApplicationContex with hibernate.hbm2ddl.auto 'CREATE' property");
131 // dataSource.updateSessionFactory("create");
132 // appContext = new EclipseRcpSaveFileSystemXmlApplicationContext(CdmApplicationUtils.getApplicationContextString());
134 setApplicationContext(appContext
);
135 // load defined terms if necessary
136 //TODO not necessary any more
137 if (testDefinedTermsAreMissing()){
138 TermLoader termLoader
= (TermLoader
) appContext
.getBean("termLoader");
140 termLoader
.loadAllDefaultTerms();
141 } catch (FileNotFoundException fileNotFoundException
) {
142 logger
.error("One or more DefinedTerm initialisation files could not be found");
143 fileNotFoundException
.printStackTrace();
145 } catch (NoDefinedTermClassException noDefinedTermClassException
) {
146 logger
.error("NoDefinedTermClassException");
147 noDefinedTermClassException
.printStackTrace();
156 * Tests if some DefinedTermsAreMissing.
157 * @return true, if at least one is missing, else false
159 public boolean testDefinedTermsAreMissing(){
160 UUID englishUuid
= UUID
.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
161 DefinedTermBase english
= this.getTermService().getTermByUri(englishUuid
.toString());
162 if ( english
== null || ! english
.getUuid().equals(englishUuid
)){
171 * Changes the ApplicationContext to the new dataSource
174 public boolean changeDataSource(CdmDataSource dataSource
) {
175 logger
.info("Change datasource to : " + dataSource
);
176 return setNewDataSource(dataSource
, HBM2DDL
.VALIDATE
);
180 * Changes the ApplicationContext to the new dataSource
183 public boolean changeDataSource(CdmDataSource dataSource
, HBM2DDL hbm2dll
) {
184 logger
.info("Change datasource to : " + dataSource
);
185 return setNewDataSource(dataSource
, hbm2dll
);
189 * Sets a new application Context.
192 public void setApplicationContext(AbstractXmlApplicationContext ac
){
193 closeApplicationContext(); //closes old application context if necessary
194 applicationContext
= ac
;
195 applicationContext
.registerShutdownHook();
200 * @see java.lang.Object#finalize()
202 public void finalize(){
207 * closes the application
210 closeApplicationContext();
214 * closes the application context
216 private void closeApplicationContext(){
217 if (applicationContext
!= null){
218 logger
.info("Close ApplicationContext");
219 applicationContext
.close();
224 logger
.debug("Init " + this.getClass().getName() + " ... ");
225 if (logger
.isDebugEnabled()){for (String beanName
: applicationContext
.getBeanDefinitionNames()){ logger
.debug(beanName
);}}
226 nameService
= (INameService
)applicationContext
.getBean("nameServiceImpl");
227 taxonService
= (ITaxonService
)applicationContext
.getBean("taxonServiceImpl");
228 referenceService
= (IReferenceService
)applicationContext
.getBean("referenceServiceImpl");
229 agentService
= (IAgentService
)applicationContext
.getBean("agentServiceImpl");
230 termService
= (ITermService
)applicationContext
.getBean("termServiceImpl");
231 databaseService
= (IDatabaseService
)applicationContext
.getBean("databaseServiceHibernateImpl");
232 databaseService
.setApplicationController(this);
237 /* ****** Services *********/
238 public final INameService
getNameService(){
239 return this.nameService
;
242 public final ITaxonService
getTaxonService(){
243 return this.taxonService
;
246 public final IReferenceService
getReferenceService(){
247 return this.referenceService
;
250 public final IAgentService
getAgentService(){
251 return this.agentService
;
254 public final IDatabaseService
getDatabaseService(){
255 return this.databaseService
;
258 public final ITermService
getTermService(){
259 return this.termService
;
262 /* **** flush ***********/
263 public void flush() {
264 SessionFactory sf
= (SessionFactory
)applicationContext
.getBean("sessionFactory");
265 sf
.getCurrentSession().flush();