1
|
package eu.etaxonomy.cdm.vaadin.util;
|
2
|
|
3
|
import java.sql.SQLException;
|
4
|
|
5
|
import javax.servlet.ServletContext;
|
6
|
import javax.sql.DataSource;
|
7
|
|
8
|
import org.springframework.context.ApplicationContext;
|
9
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
10
|
|
11
|
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
|
12
|
import com.vaadin.server.VaadinServlet;
|
13
|
|
14
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
15
|
import eu.etaxonomy.cdm.vaadin.container.CdmSpringConnectionPool;
|
16
|
|
17
|
public class CdmSpringContextHelper {
|
18
|
|
19
|
private final ApplicationContext context;
|
20
|
|
21
|
private static CdmSpringContextHelper contextHelper;
|
22
|
|
23
|
private CdmSpringContextHelper(ServletContext servletContext) {
|
24
|
context = WebApplicationContextUtils.
|
25
|
getRequiredWebApplicationContext(servletContext);
|
26
|
|
27
|
}
|
28
|
|
29
|
|
30
|
public static CdmSpringContextHelper newInstance() {
|
31
|
if(VaadinServlet.getCurrent() == null || VaadinServlet.getCurrent().getServletContext() == null) {
|
32
|
throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
|
33
|
}
|
34
|
|
35
|
if(contextHelper == null) {
|
36
|
ServletContext sc = VaadinServlet.getCurrent().getServletContext();
|
37
|
contextHelper = new CdmSpringContextHelper(sc);
|
38
|
return contextHelper;
|
39
|
} else {
|
40
|
return contextHelper;
|
41
|
}
|
42
|
}
|
43
|
|
44
|
public Object getBean(final String beanRef) {
|
45
|
return context.getBean(beanRef);
|
46
|
}
|
47
|
|
48
|
public static JDBCConnectionPool getConnectionPool() throws SQLException {
|
49
|
DataSource bean = (DataSource) newInstance().getBean("dataSource");
|
50
|
JDBCConnectionPool connectionPool = new CdmSpringConnectionPool(bean.getConnection());
|
51
|
return connectionPool;
|
52
|
}
|
53
|
|
54
|
public static ITaxonService getTaxonService() {
|
55
|
return (ITaxonService)CdmSpringContextHelper.newInstance().getBean("taxonServiceImpl");
|
56
|
}
|
57
|
}
|