ref
[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.ICdmApplicationConfiguration;
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 // public static JDBCConnectionPool createConnectionPool() {
100 // return new J2EEConnectionPool(getCurrent().getDataSource());
101 // }
102
103 // public static JDBCConnectionPool getConnectionPool() {
104 // return new J2EEConnectionPool(getCurrent().getDataSource());
105 // }
106
107
108
109 public static Connection getConnection() throws SQLException {
110 return getCurrent().getDataSource().getConnection();
111 }
112
113 public static DatabaseMetaData getDatabaseMetaData() throws SQLException {
114 if(databaseMetaData == null) {
115 Connection conn = getConnection();
116 databaseMetaData = conn.getMetaData();
117 conn.close();
118 }
119 return databaseMetaData;
120 }
121
122 public static ICdmApplicationConfiguration getApplicationConfiguration() {
123 return (ICdmApplicationConfiguration) getCurrent().getBean("cdmApplicationDefaultConfiguration");
124 }
125 public static ITaxonService getTaxonService() {
126 return (ITaxonService)getCurrent().getBean(ITaxonService.class);
127 }
128
129 public static ITaxonNodeService getTaxonNodeService() {
130 return (ITaxonNodeService)getCurrent().getBean(ITaxonNodeService.class);
131 }
132
133 public static IReferenceService getReferenceService() {
134 return (IReferenceService)getCurrent().getBean(IReferenceService.class);
135 }
136
137 public static INameService getNameService() {
138 return (INameService)getCurrent().getBean(INameService.class);
139 }
140
141 public static ICommonService getCommonService() {
142 return (ICommonService)getCurrent().getBean(ICommonService.class);
143 }
144
145 public static IClassificationService getClassificationService() {
146 return (IClassificationService)getCurrent().getBean(IClassificationService.class);
147 }
148
149 public static IVocabularyService getVocabularyService() {
150 return (IVocabularyService)getCurrent().getBean(IVocabularyService.class);
151 }
152
153 public static ITermService getTermService() {
154 return (ITermService)getCurrent().getBean(ITermService.class);
155 }
156
157 public static IDescriptionService getDescriptionService() {
158 return (IDescriptionService)getCurrent().getBean(IDescriptionService.class);
159 }
160
161
162
163 }