ref #6903
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / util / CdmSpringContextHelper.java
1 package eu.etaxonomy.cdm.vaadin.util;
2
3 import java.sql.Connection;
4 import java.sql.DatabaseMetaData;
5 import java.sql.SQLException;
6
7 import javax.servlet.ServletContext;
8 import javax.sql.DataSource;
9
10 import org.springframework.context.ApplicationContext;
11 import org.springframework.web.context.support.WebApplicationContextUtils;
12
13 import com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPool;
14 import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
15 import com.vaadin.server.VaadinServlet;
16
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;
28
29 /**
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
33 * helper class has
34 * been written to retrieve the beans given the bean name.
35 *
36 * @author c.mathew
37 *
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.
40 *
41 */
42 public class CdmSpringContextHelper {
43
44 private final ApplicationContext context;
45 private final DataSource dataSource;
46
47 private final JDBCConnectionPool connPool;
48 private static CdmSpringContextHelper contextHelper;
49
50 private static DatabaseMetaData databaseMetaData;
51
52 private CdmSpringContextHelper(ServletContext servletContext) throws SQLException {
53 context = WebApplicationContextUtils.
54 getRequiredWebApplicationContext(servletContext);
55 dataSource = (DataSource)getBean("dataSource");
56 connPool = new J2EEConnectionPool(dataSource);
57
58 }
59
60
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");
64 }
65
66 if(contextHelper == null) {
67 ServletContext sc = VaadinServlet.getCurrent().getServletContext();
68 try {
69 contextHelper = new CdmSpringContextHelper(sc);
70 } catch (SQLException e) {
71 // TODO Auto-generated catch block
72 e.printStackTrace();
73 }
74 }
75 return contextHelper;
76
77 }
78
79 public Object getBean(final String beanRef) {
80 return context.getBean(beanRef);
81 }
82
83 public Object getBean(Class clazz) {
84 return context.getBean(clazz);
85 }
86
87 public <T extends IService> T getService(Class<T> clazz) {
88 return context.getBean(clazz);
89 }
90
91 public DataSource getDataSource() {
92 return dataSource;
93 }
94
95 public JDBCConnectionPool getConnectionPool() {
96 return connPool;
97 }
98
99
100 // public static JDBCConnectionPool createConnectionPool() {
101 // return new J2EEConnectionPool(getCurrent().getDataSource());
102 // }
103
104 // public static JDBCConnectionPool getConnectionPool() {
105 // return new J2EEConnectionPool(getCurrent().getDataSource());
106 // }
107
108
109
110 public static Connection getConnection() throws SQLException {
111 return getCurrent().getDataSource().getConnection();
112 }
113
114 public static DatabaseMetaData getDatabaseMetaData() throws SQLException {
115 if(databaseMetaData == null) {
116 Connection conn = getConnection();
117 databaseMetaData = conn.getMetaData();
118 conn.close();
119 }
120 return databaseMetaData;
121 }
122
123 public static ICdmRepository getApplicationConfiguration() {
124 return (ICdmRepository) getCurrent().getBean("cdmRepository");
125 }
126 public static ITaxonService getTaxonService() {
127 return (ITaxonService)getCurrent().getBean(ITaxonService.class);
128 }
129
130 public static ITaxonNodeService getTaxonNodeService() {
131 return (ITaxonNodeService)getCurrent().getBean(ITaxonNodeService.class);
132 }
133
134 public static IReferenceService getReferenceService() {
135 return (IReferenceService)getCurrent().getBean(IReferenceService.class);
136 }
137
138 public static INameService getNameService() {
139 return (INameService)getCurrent().getBean(INameService.class);
140 }
141
142 public static ICommonService getCommonService() {
143 return (ICommonService)getCurrent().getBean(ICommonService.class);
144 }
145
146 public static IClassificationService getClassificationService() {
147 return (IClassificationService)getCurrent().getBean(IClassificationService.class);
148 }
149
150 public static IVocabularyService getVocabularyService() {
151 return (IVocabularyService)getCurrent().getBean(IVocabularyService.class);
152 }
153
154 public static ITermService getTermService() {
155 return (ITermService)getCurrent().getBean(ITermService.class);
156 }
157
158 public static IDescriptionService getDescriptionService() {
159 return (IDescriptionService)getCurrent().getBean(IDescriptionService.class);
160 }
161
162
163 }