1
|
package eu.etaxonomy.cdm.vaadin.util;
|
2
|
|
3
|
import java.sql.Connection;
|
4
|
import java.sql.SQLException;
|
5
|
|
6
|
import javax.servlet.ServletContext;
|
7
|
import javax.sql.DataSource;
|
8
|
|
9
|
import org.springframework.context.ApplicationContext;
|
10
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
11
|
|
12
|
import com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPool;
|
13
|
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
|
14
|
import com.vaadin.server.VaadinServlet;
|
15
|
|
16
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
17
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
18
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
19
|
import eu.etaxonomy.cdm.api.service.INameService;
|
20
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
21
|
import eu.etaxonomy.cdm.api.service.IService;
|
22
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
23
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
24
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
25
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
26
|
|
27
|
public class CdmSpringContextHelper {
|
28
|
|
29
|
private final ApplicationContext context;
|
30
|
private final DataSource dataSource;
|
31
|
|
32
|
private final JDBCConnectionPool connPool;
|
33
|
private static CdmSpringContextHelper contextHelper;
|
34
|
|
35
|
private CdmSpringContextHelper(ServletContext servletContext) throws SQLException {
|
36
|
context = WebApplicationContextUtils.
|
37
|
getRequiredWebApplicationContext(servletContext);
|
38
|
dataSource = (DataSource)getBean("dataSource");
|
39
|
connPool = new J2EEConnectionPool(dataSource);
|
40
|
|
41
|
}
|
42
|
|
43
|
|
44
|
public static CdmSpringContextHelper getCurrent() {
|
45
|
if(VaadinServlet.getCurrent() == null || VaadinServlet.getCurrent().getServletContext() == null) {
|
46
|
throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
|
47
|
}
|
48
|
|
49
|
if(contextHelper == null) {
|
50
|
ServletContext sc = VaadinServlet.getCurrent().getServletContext();
|
51
|
try {
|
52
|
contextHelper = new CdmSpringContextHelper(sc);
|
53
|
} catch (SQLException e) {
|
54
|
// TODO Auto-generated catch block
|
55
|
e.printStackTrace();
|
56
|
}
|
57
|
}
|
58
|
return contextHelper;
|
59
|
|
60
|
}
|
61
|
|
62
|
public Object getBean(final String beanRef) {
|
63
|
return context.getBean(beanRef);
|
64
|
}
|
65
|
|
66
|
public Object getBean(Class clazz) {
|
67
|
return context.getBean(clazz);
|
68
|
}
|
69
|
|
70
|
public <T extends IService> T getService(Class<T> clazz) {
|
71
|
return context.getBean(clazz);
|
72
|
}
|
73
|
|
74
|
public DataSource getDataSource() {
|
75
|
return dataSource;
|
76
|
}
|
77
|
|
78
|
public JDBCConnectionPool getConnectionPool() {
|
79
|
return connPool;
|
80
|
}
|
81
|
|
82
|
// public static JDBCConnectionPool createConnectionPool() {
|
83
|
// return new J2EEConnectionPool(getCurrent().getDataSource());
|
84
|
// }
|
85
|
|
86
|
// public static JDBCConnectionPool getConnectionPool() {
|
87
|
// return new J2EEConnectionPool(getCurrent().getDataSource());
|
88
|
// }
|
89
|
|
90
|
|
91
|
|
92
|
public static Connection getConnection() throws SQLException {
|
93
|
return getCurrent().getDataSource().getConnection();
|
94
|
}
|
95
|
|
96
|
public static ITaxonService getTaxonService() {
|
97
|
return (ITaxonService)getCurrent().getBean(ITaxonService.class);
|
98
|
}
|
99
|
|
100
|
public static ITaxonNodeService getTaxonNodeService() {
|
101
|
return (ITaxonNodeService)getCurrent().getBean(ITaxonNodeService.class);
|
102
|
}
|
103
|
|
104
|
public static IReferenceService getReferenceService() {
|
105
|
return (IReferenceService)getCurrent().getBean(IReferenceService.class);
|
106
|
}
|
107
|
|
108
|
public static INameService getNameService() {
|
109
|
return (INameService)getCurrent().getBean(INameService.class);
|
110
|
}
|
111
|
|
112
|
public static ICommonService getCommonService() {
|
113
|
return (ICommonService)getCurrent().getBean(ICommonService.class);
|
114
|
}
|
115
|
|
116
|
public static IClassificationService getClassificationService() {
|
117
|
return (IClassificationService)getCurrent().getBean(IClassificationService.class);
|
118
|
}
|
119
|
|
120
|
public static IVocabularyService getVocabularyService() {
|
121
|
return (IVocabularyService)getCurrent().getBean(IVocabularyService.class);
|
122
|
}
|
123
|
|
124
|
public static ITermService getTermService() {
|
125
|
return (ITermService)getCurrent().getBean(ITermService.class);
|
126
|
}
|
127
|
|
128
|
public static IDescriptionService getDescriptionService() {
|
129
|
return (IDescriptionService)getCurrent().getBean(IDescriptionService.class);
|
130
|
}
|
131
|
}
|