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
.ICdmRepository
;
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
.IReferenceService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
30 * This helper relates to the problem that in the Vaadin framework it is
31 * not possible to autowire beans from the underlying application context
32 * as Vaadin prevents this possibility. To overcome this problem, this singleton
34 * been written to retrieve the beans given the bean name.
38 * TODO This class may no longer needed in a couple of cases since vaadin-spring
39 * is being used and spring beans can be injected now.
42 public class CdmSpringContextHelper
{
44 private final ApplicationContext context
;
45 private final DataSource dataSource
;
47 private final JDBCConnectionPool connPool
;
48 private static CdmSpringContextHelper contextHelper
;
50 private static DatabaseMetaData databaseMetaData
;
52 private CdmSpringContextHelper(ServletContext servletContext
) throws SQLException
{
53 context
= WebApplicationContextUtils
.
54 getRequiredWebApplicationContext(servletContext
);
55 dataSource
= (DataSource
)getBean("dataSource");
56 connPool
= new J2EEConnectionPool(dataSource
);
61 public static CdmSpringContextHelper
getCurrent() {
62 if(VaadinServlet
.getCurrent() == null || VaadinServlet
.getCurrent().getServletContext() == null) {
63 throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
66 if(contextHelper
== null) {
67 ServletContext sc
= VaadinServlet
.getCurrent().getServletContext();
69 contextHelper
= new CdmSpringContextHelper(sc
);
70 } catch (SQLException e
) {
71 // TODO Auto-generated catch block
79 public Object
getBean(final String beanRef
) {
80 return context
.getBean(beanRef
);
83 public Object
getBean(Class clazz
) {
84 return context
.getBean(clazz
);
87 public <T
extends IService
> T
getService(Class
<T
> clazz
) {
88 return context
.getBean(clazz
);
91 public DataSource
getDataSource() {
95 public JDBCConnectionPool
getConnectionPool() {
100 // public static JDBCConnectionPool createConnectionPool() {
101 // return new J2EEConnectionPool(getCurrent().getDataSource());
104 // public static JDBCConnectionPool getConnectionPool() {
105 // return new J2EEConnectionPool(getCurrent().getDataSource());
110 public static Connection
getConnection() throws SQLException
{
111 return getCurrent().getDataSource().getConnection();
114 public static DatabaseMetaData
getDatabaseMetaData() throws SQLException
{
115 if(databaseMetaData
== null) {
116 Connection conn
= getConnection();
117 databaseMetaData
= conn
.getMetaData();
120 return databaseMetaData
;
123 public static ICdmRepository
getApplicationConfiguration() {
124 return (ICdmRepository
) getCurrent().getBean("cdmRepository");
126 public static ITaxonService
getTaxonService() {
127 return (ITaxonService
)getCurrent().getBean(ITaxonService
.class);
130 public static ITaxonNodeService
getTaxonNodeService() {
131 return (ITaxonNodeService
)getCurrent().getBean(ITaxonNodeService
.class);
134 public static IReferenceService
getReferenceService() {
135 return (IReferenceService
)getCurrent().getBean(IReferenceService
.class);
138 public static INameService
getNameService() {
139 return (INameService
)getCurrent().getBean(INameService
.class);
142 public static ICommonService
getCommonService() {
143 return (ICommonService
)getCurrent().getBean(ICommonService
.class);
146 public static IClassificationService
getClassificationService() {
147 return (IClassificationService
)getCurrent().getBean(IClassificationService
.class);
150 public static IVocabularyService
getVocabularyService() {
151 return (IVocabularyService
)getCurrent().getBean(IVocabularyService
.class);
154 public static ITermService
getTermService() {
155 return (ITermService
)getCurrent().getBean(ITermService
.class);
158 public static IDescriptionService
getDescriptionService() {
159 return (IDescriptionService
)getCurrent().getBean(IDescriptionService
.class);