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.vaadin.container.CdmSpringConnectionPool;
|
15
|
|
16
|
public class CdmSpringContextHelper {
|
17
|
|
18
|
private final ApplicationContext context;
|
19
|
|
20
|
private static CdmSpringContextHelper contextHelper;
|
21
|
|
22
|
private CdmSpringContextHelper(ServletContext servletContext) {
|
23
|
context = WebApplicationContextUtils.
|
24
|
getRequiredWebApplicationContext(servletContext);
|
25
|
|
26
|
}
|
27
|
|
28
|
|
29
|
public static CdmSpringContextHelper newInstance() {
|
30
|
if(VaadinServlet.getCurrent() == null || VaadinServlet.getCurrent().getServletContext() == null) {
|
31
|
throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
|
32
|
}
|
33
|
|
34
|
if(contextHelper == null) {
|
35
|
ServletContext sc = VaadinServlet.getCurrent().getServletContext();
|
36
|
contextHelper = new CdmSpringContextHelper(sc);
|
37
|
return contextHelper;
|
38
|
} else {
|
39
|
return contextHelper;
|
40
|
}
|
41
|
}
|
42
|
|
43
|
public Object getBean(final String beanRef) {
|
44
|
return context.getBean(beanRef);
|
45
|
}
|
46
|
|
47
|
public static JDBCConnectionPool getConnectionPool() throws SQLException {
|
48
|
DataSource bean = (DataSource) newInstance().getBean("dataSource");
|
49
|
JDBCConnectionPool connectionPool = new CdmSpringConnectionPool(bean.getConnection());
|
50
|
return connectionPool;
|
51
|
}
|
52
|
}
|