1 package eu
.etaxonomy
.cdm
.vaadin
.util
;
3 import java
.sql
.Connection
;
4 import java
.sql
.DatabaseMetaData
;
5 import java
.sql
.SQLException
;
7 import javax
.servlet
.ServletContext
;
8 import javax
.sql
.DataSource
;
10 import org
.springframework
.context
.ApplicationContext
;
11 import org
.springframework
.web
.context
.support
.WebApplicationContextUtils
;
13 import com
.vaadin
.data
.util
.sqlcontainer
.connection
.J2EEConnectionPool
;
14 import com
.vaadin
.data
.util
.sqlcontainer
.connection
.JDBCConnectionPool
;
15 import com
.vaadin
.server
.VaadinServlet
;
17 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplication
;
18 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
19 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IPreferenceService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
31 * This helper relates to the problem that in the Vaadin framework it is
32 * not possible to autowire beans from the underlying application context
33 * as Vaadin prevents this possibility. To overcome this problem, this singleton
35 * been written to retrieve the beans given the bean name.
39 * TODO This class may no longer needed in a couple of cases since vaadin-spring
40 * is being used and spring beans can be injected now.
43 public class CdmSpringContextHelper
{
45 private final ApplicationContext context
;
46 private final DataSource dataSource
;
48 private final JDBCConnectionPool connPool
;
49 private static CdmSpringContextHelper contextHelper
;
51 private static DatabaseMetaData databaseMetaData
;
53 private CdmSpringContextHelper(ServletContext servletContext
) throws SQLException
{
54 context
= WebApplicationContextUtils
.
55 getRequiredWebApplicationContext(servletContext
);
56 dataSource
= (DataSource
)getBean("dataSource");
57 connPool
= new J2EEConnectionPool(dataSource
);
60 public static CdmSpringContextHelper
getCurrent() {
61 if(VaadinServlet
.getCurrent() == null || VaadinServlet
.getCurrent().getServletContext() == null) {
62 throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
65 if(contextHelper
== null) {
66 ServletContext sc
= VaadinServlet
.getCurrent().getServletContext();
68 contextHelper
= new CdmSpringContextHelper(sc
);
69 } catch (SQLException e
) {
70 // TODO Auto-generated catch block
78 public Object
getBean(final String beanRef
) {
79 return context
.getBean(beanRef
);
82 public Object
getBean(Class clazz
) {
83 return context
.getBean(clazz
);
86 public <T
extends IService
> T
getService(Class
<T
> clazz
) {
87 return context
.getBean(clazz
);
90 public DataSource
getDataSource() {
94 public JDBCConnectionPool
getConnectionPool() {
99 // public static JDBCConnectionPool createConnectionPool() {
100 // return new J2EEConnectionPool(getCurrent().getDataSource());
103 // public static JDBCConnectionPool getConnectionPool() {
104 // return new J2EEConnectionPool(getCurrent().getDataSource());
109 public static Connection
getConnection() throws SQLException
{
110 return getCurrent().getDataSource().getConnection();
113 public static DatabaseMetaData
getDatabaseMetaData() throws SQLException
{
114 if(databaseMetaData
== null) {
115 Connection conn
= getConnection();
116 databaseMetaData
= conn
.getMetaData();
119 return databaseMetaData
;
122 public static ICdmApplication
getApplicationConfiguration() {
123 return (ICdmApplication
) getCurrent().getBean("cdmRepository");
125 public static ITaxonService
getTaxonService() {
126 return (ITaxonService
)getCurrent().getBean(ITaxonService
.class);
129 public static ITaxonNodeService
getTaxonNodeService() {
130 return (ITaxonNodeService
)getCurrent().getBean(ITaxonNodeService
.class);
133 public static IReferenceService
getReferenceService() {
134 return (IReferenceService
)getCurrent().getBean(IReferenceService
.class);
137 public static INameService
getNameService() {
138 return (INameService
)getCurrent().getBean(INameService
.class);
141 public static ICommonService
getCommonService() {
142 return (ICommonService
)getCurrent().getBean(ICommonService
.class);
145 public static IClassificationService
getClassificationService() {
146 return (IClassificationService
)getCurrent().getBean(IClassificationService
.class);
149 public static IVocabularyService
getVocabularyService() {
150 return (IVocabularyService
)getCurrent().getBean(IVocabularyService
.class);
153 public static ITermService
getTermService() {
154 return (ITermService
)getCurrent().getBean(ITermService
.class);
157 public static IDescriptionService
getDescriptionService() {
158 return (IDescriptionService
)getCurrent().getBean(IDescriptionService
.class);
161 public static IPreferenceService
getPreferenceService() {
162 return (IPreferenceService
)getCurrent().getBean(IPreferenceService
.class);