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
.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
);
62 public static CdmSpringContextHelper
getCurrent() {
63 if(VaadinServlet
.getCurrent() == null || VaadinServlet
.getCurrent().getServletContext() == null) {
64 throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
67 if(contextHelper
== null) {
68 ServletContext sc
= VaadinServlet
.getCurrent().getServletContext();
70 contextHelper
= new CdmSpringContextHelper(sc
);
71 } catch (SQLException e
) {
72 // TODO Auto-generated catch block
80 public Object
getBean(final String beanRef
) {
81 return context
.getBean(beanRef
);
84 public Object
getBean(Class clazz
) {
85 return context
.getBean(clazz
);
88 public <T
extends IService
> T
getService(Class
<T
> clazz
) {
89 return context
.getBean(clazz
);
92 public DataSource
getDataSource() {
96 public JDBCConnectionPool
getConnectionPool() {
101 // public static JDBCConnectionPool createConnectionPool() {
102 // return new J2EEConnectionPool(getCurrent().getDataSource());
105 // public static JDBCConnectionPool getConnectionPool() {
106 // return new J2EEConnectionPool(getCurrent().getDataSource());
111 public static Connection
getConnection() throws SQLException
{
112 return getCurrent().getDataSource().getConnection();
115 public static DatabaseMetaData
getDatabaseMetaData() throws SQLException
{
116 if(databaseMetaData
== null) {
117 Connection conn
= getConnection();
118 databaseMetaData
= conn
.getMetaData();
121 return databaseMetaData
;
124 public static ICdmRepository
getApplicationConfiguration() {
125 return (ICdmRepository
) getCurrent().getBean("cdmRepository");
127 public static ITaxonService
getTaxonService() {
128 return (ITaxonService
)getCurrent().getBean(ITaxonService
.class);
131 public static ITaxonNodeService
getTaxonNodeService() {
132 return (ITaxonNodeService
)getCurrent().getBean(ITaxonNodeService
.class);
135 public static IReferenceService
getReferenceService() {
136 return (IReferenceService
)getCurrent().getBean(IReferenceService
.class);
139 public static INameService
getNameService() {
140 return (INameService
)getCurrent().getBean(INameService
.class);
143 public static ICommonService
getCommonService() {
144 return (ICommonService
)getCurrent().getBean(ICommonService
.class);
147 public static IClassificationService
getClassificationService() {
148 return (IClassificationService
)getCurrent().getBean(IClassificationService
.class);
151 public static IVocabularyService
getVocabularyService() {
152 return (IVocabularyService
)getCurrent().getBean(IVocabularyService
.class);
155 public static ITermService
getTermService() {
156 return (ITermService
)getCurrent().getBean(ITermService
.class);
159 public static IDescriptionService
getDescriptionService() {
160 return (IDescriptionService
)getCurrent().getBean(IDescriptionService
.class);
163 public static IPreferenceService
getPreferenceService() {
164 return (IPreferenceService
)getCurrent().getBean(IPreferenceService
.class);