jenkins merging release branch into master (strategy: theirs)
[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.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;
29
30 /**
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
34 * helper class has
35 * been written to retrieve the beans given the bean name.
36 *
37 * @author c.mathew
38 *
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.
41 *
42 */
43 public class CdmSpringContextHelper {
44
45 private final ApplicationContext context;
46 private final DataSource dataSource;
47
48 private final JDBCConnectionPool connPool;
49 private static CdmSpringContextHelper contextHelper;
50
51 private static DatabaseMetaData databaseMetaData;
52
53 private CdmSpringContextHelper(ServletContext servletContext) throws SQLException {
54 context = WebApplicationContextUtils.
55 getRequiredWebApplicationContext(servletContext);
56 dataSource = (DataSource)getBean("dataSource");
57 connPool = new J2EEConnectionPool(dataSource);
58 }
59
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");
63 }
64
65 if(contextHelper == null) {
66 ServletContext sc = VaadinServlet.getCurrent().getServletContext();
67 try {
68 contextHelper = new CdmSpringContextHelper(sc);
69 } catch (SQLException e) {
70 // TODO Auto-generated catch block
71 e.printStackTrace();
72 }
73 }
74 return contextHelper;
75
76 }
77
78 public Object getBean(final String beanRef) {
79 return context.getBean(beanRef);
80 }
81
82 public Object getBean(Class clazz) {
83 return context.getBean(clazz);
84 }
85
86 public <T extends IService> T getService(Class<T> clazz) {
87 return context.getBean(clazz);
88 }
89
90 public DataSource getDataSource() {
91 return dataSource;
92 }
93
94 public JDBCConnectionPool getConnectionPool() {
95 return connPool;
96 }
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 ICdmApplication getApplicationConfiguration() {
123 return (ICdmApplication) getCurrent().getBean("cdmRepository");
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 public static IPreferenceService getPreferenceService() {
162 return (IPreferenceService)getCurrent().getBean(IPreferenceService.class);
163 }
164
165
166 }