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.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
|
|
61
|
|
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");
|
65
|
}
|
66
|
|
67
|
if(contextHelper == null) {
|
68
|
ServletContext sc = VaadinServlet.getCurrent().getServletContext();
|
69
|
try {
|
70
|
contextHelper = new CdmSpringContextHelper(sc);
|
71
|
} catch (SQLException e) {
|
72
|
// TODO Auto-generated catch block
|
73
|
e.printStackTrace();
|
74
|
}
|
75
|
}
|
76
|
return contextHelper;
|
77
|
|
78
|
}
|
79
|
|
80
|
public Object getBean(final String beanRef) {
|
81
|
return context.getBean(beanRef);
|
82
|
}
|
83
|
|
84
|
public Object getBean(Class clazz) {
|
85
|
return context.getBean(clazz);
|
86
|
}
|
87
|
|
88
|
public <T extends IService> T getService(Class<T> clazz) {
|
89
|
return context.getBean(clazz);
|
90
|
}
|
91
|
|
92
|
public DataSource getDataSource() {
|
93
|
return dataSource;
|
94
|
}
|
95
|
|
96
|
public JDBCConnectionPool getConnectionPool() {
|
97
|
return connPool;
|
98
|
}
|
99
|
|
100
|
|
101
|
// public static JDBCConnectionPool createConnectionPool() {
|
102
|
// return new J2EEConnectionPool(getCurrent().getDataSource());
|
103
|
// }
|
104
|
|
105
|
// public static JDBCConnectionPool getConnectionPool() {
|
106
|
// return new J2EEConnectionPool(getCurrent().getDataSource());
|
107
|
// }
|
108
|
|
109
|
|
110
|
|
111
|
public static Connection getConnection() throws SQLException {
|
112
|
return getCurrent().getDataSource().getConnection();
|
113
|
}
|
114
|
|
115
|
public static DatabaseMetaData getDatabaseMetaData() throws SQLException {
|
116
|
if(databaseMetaData == null) {
|
117
|
Connection conn = getConnection();
|
118
|
databaseMetaData = conn.getMetaData();
|
119
|
conn.close();
|
120
|
}
|
121
|
return databaseMetaData;
|
122
|
}
|
123
|
|
124
|
public static ICdmRepository getApplicationConfiguration() {
|
125
|
return (ICdmRepository) getCurrent().getBean("cdmRepository");
|
126
|
}
|
127
|
public static ITaxonService getTaxonService() {
|
128
|
return (ITaxonService)getCurrent().getBean(ITaxonService.class);
|
129
|
}
|
130
|
|
131
|
public static ITaxonNodeService getTaxonNodeService() {
|
132
|
return (ITaxonNodeService)getCurrent().getBean(ITaxonNodeService.class);
|
133
|
}
|
134
|
|
135
|
public static IReferenceService getReferenceService() {
|
136
|
return (IReferenceService)getCurrent().getBean(IReferenceService.class);
|
137
|
}
|
138
|
|
139
|
public static INameService getNameService() {
|
140
|
return (INameService)getCurrent().getBean(INameService.class);
|
141
|
}
|
142
|
|
143
|
public static ICommonService getCommonService() {
|
144
|
return (ICommonService)getCurrent().getBean(ICommonService.class);
|
145
|
}
|
146
|
|
147
|
public static IClassificationService getClassificationService() {
|
148
|
return (IClassificationService)getCurrent().getBean(IClassificationService.class);
|
149
|
}
|
150
|
|
151
|
public static IVocabularyService getVocabularyService() {
|
152
|
return (IVocabularyService)getCurrent().getBean(IVocabularyService.class);
|
153
|
}
|
154
|
|
155
|
public static ITermService getTermService() {
|
156
|
return (ITermService)getCurrent().getBean(ITermService.class);
|
157
|
}
|
158
|
|
159
|
public static IDescriptionService getDescriptionService() {
|
160
|
return (IDescriptionService)getCurrent().getBean(IDescriptionService.class);
|
161
|
}
|
162
|
|
163
|
public static IPreferenceService getPreferenceService() {
|
164
|
return (IPreferenceService)getCurrent().getBean(IPreferenceService.class);
|
165
|
}
|
166
|
|
167
|
|
168
|
}
|