Project

General

Profile

Download (3.78 KB) Statistics
| Branch: | Tag: | Revision:
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.service.ITaxonNodeService;
18

    
19
/**
20
 * This helper was related to the problem that in the Vaadin framework it was
21
 * not possible to autowire beans from the underlying application context
22
 * as Vaadin preventes this possibility. To overcome this problem, this singleton
23
 * helper class has been written to retrieve the beans given the bean name.
24
 * <p>
25
 * Now that <code>vaadin-spring</code> is being used this class is in principle
26
 * no longer needed. It never the less been kept for some time to continue
27
 * provide the J2EEConnectionPool. Once a clear concept exist about to handle and
28
 * inject it in a proper way this class can be removed completely.
29
 *
30
 * @author c.mathew
31
 *
32
 * TODO This class may no longer needed in a couple of cases since <code>vaadin-spring</code>
33
 * is being used and spring beans can be injected now.
34
 *
35
 */
36
public class CdmSpringContextHelper {
37

    
38
    private final ApplicationContext context;
39
    private final DataSource dataSource;
40

    
41
    private final JDBCConnectionPool connPool;
42
    private static CdmSpringContextHelper contextHelper;
43

    
44
    private static DatabaseMetaData databaseMetaData;
45

    
46
    private CdmSpringContextHelper(ServletContext servletContext) throws SQLException {
47
        context = WebApplicationContextUtils.
48
                getRequiredWebApplicationContext(servletContext);
49
        dataSource = (DataSource)getBean("dataSource");
50
        connPool = new J2EEConnectionPool(dataSource);
51

    
52
    }
53

    
54

    
55
    public static CdmSpringContextHelper getCurrent() {
56
        if(VaadinServlet.getCurrent() == null || VaadinServlet.getCurrent().getServletContext() == null) {
57
            throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
58
        }
59

    
60
        if(contextHelper == null) {
61
            ServletContext sc = VaadinServlet.getCurrent().getServletContext();
62
            try {
63
                contextHelper = new CdmSpringContextHelper(sc);
64
            } catch (SQLException e) {
65
                // TODO Auto-generated catch block
66
                e.printStackTrace();
67
            }
68
        }
69
        return contextHelper;
70

    
71
    }
72

    
73
    private Object getBean(final String beanRef) {
74
        return context.getBean(beanRef);
75
    }
76
    
77
    private Object getBean(Class clazz) {
78
        return context.getBean(clazz);
79
    }
80

    
81
    public DataSource getDataSource() {
82
        return dataSource;
83
    }
84

    
85
    public JDBCConnectionPool getConnectionPool() {
86
        return connPool;
87
    }
88

    
89
//    public static JDBCConnectionPool createConnectionPool() {
90
//        return new J2EEConnectionPool(getCurrent().getDataSource());
91
//    }
92

    
93
//    public static JDBCConnectionPool getConnectionPool() {
94
//        return new J2EEConnectionPool(getCurrent().getDataSource());
95
//    }
96

    
97
    public static Connection getConnection() throws SQLException {
98
        return getCurrent().getDataSource().getConnection();
99
    }
100

    
101
    public static DatabaseMetaData getDatabaseMetaData() throws SQLException {
102
        if(databaseMetaData == null) {
103
            Connection conn = getConnection();
104
            databaseMetaData = conn.getMetaData();
105
            conn.close();
106
        }
107
        return databaseMetaData;
108
    }
109

    
110
    @Deprecated
111
    public static ITaxonNodeService getTaxonNodeService() {
112
        return (ITaxonNodeService)getCurrent().getBean(ITaxonNodeService.class);
113
    }
114
}
(3-3/10)