Project

General

Profile

Download (4.55 KB) Statistics
| Branch: | Tag: | Revision:
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.application.ICdmApplicationConfiguration;
17
import eu.etaxonomy.cdm.api.service.IClassificationService;
18
import eu.etaxonomy.cdm.api.service.ICommonService;
19
import eu.etaxonomy.cdm.api.service.IDescriptionService;
20
import eu.etaxonomy.cdm.api.service.INameService;
21
import eu.etaxonomy.cdm.api.service.IReferenceService;
22
import eu.etaxonomy.cdm.api.service.IService;
23
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
24
import eu.etaxonomy.cdm.api.service.ITaxonService;
25
import eu.etaxonomy.cdm.api.service.ITermService;
26
import eu.etaxonomy.cdm.api.service.IVocabularyService;
27

    
28
public class CdmSpringContextHelper {
29

    
30
    private final ApplicationContext context;
31
    private final DataSource dataSource;
32

    
33
    private final JDBCConnectionPool connPool;
34
    private static CdmSpringContextHelper contextHelper;
35

    
36
    private CdmSpringContextHelper(ServletContext servletContext) throws SQLException {
37
        context = WebApplicationContextUtils.
38
                getRequiredWebApplicationContext(servletContext);
39
        dataSource = (DataSource)getBean("dataSource");
40
        connPool = new J2EEConnectionPool(dataSource);
41

    
42
    }
43

    
44

    
45
    public static CdmSpringContextHelper getCurrent() {
46
        if(VaadinServlet.getCurrent() == null || VaadinServlet.getCurrent().getServletContext() == null) {
47
            throw new RuntimeException("Vaadin Servlet or Vaadin Servlet Context not initialized");
48
        }
49

    
50
        if(contextHelper == null) {
51
            ServletContext sc = VaadinServlet.getCurrent().getServletContext();
52
            try {
53
                contextHelper = new CdmSpringContextHelper(sc);
54
            } catch (SQLException e) {
55
                // TODO Auto-generated catch block
56
                e.printStackTrace();
57
            }
58
        }
59
        return contextHelper;
60

    
61
    }
62

    
63
    public Object getBean(final String beanRef) {
64
        return context.getBean(beanRef);
65
    }
66

    
67
    public Object getBean(Class clazz) {
68
        return context.getBean(clazz);
69
    }
70

    
71
    public <T extends IService> T getService(Class<T> clazz) {
72
        return context.getBean(clazz);
73
    }
74

    
75
    public DataSource getDataSource() {
76
        return dataSource;
77
    }
78

    
79
    public JDBCConnectionPool getConnectionPool() {
80
        return connPool;
81
    }
82

    
83
//    public static JDBCConnectionPool createConnectionPool() {
84
//        return new J2EEConnectionPool(getCurrent().getDataSource());
85
//    }
86

    
87
//    public static JDBCConnectionPool getConnectionPool() {
88
//        return new J2EEConnectionPool(getCurrent().getDataSource());
89
//    }
90

    
91

    
92

    
93
    public static Connection getConnection() throws SQLException {
94
        return getCurrent().getDataSource().getConnection();
95
    }
96

    
97
    public static ICdmApplicationConfiguration getApplicationConfiguration() {
98
        return (ICdmApplicationConfiguration) getCurrent().getBean("cdmApplicationDefaultConfiguration");
99
    }
100
    public static ITaxonService getTaxonService() {
101
        return (ITaxonService)getCurrent().getBean(ITaxonService.class);
102
    }
103

    
104
    public static ITaxonNodeService getTaxonNodeService() {
105
        return (ITaxonNodeService)getCurrent().getBean(ITaxonNodeService.class);
106
    }
107

    
108
    public static IReferenceService getReferenceService() {
109
        return (IReferenceService)getCurrent().getBean(IReferenceService.class);
110
    }
111

    
112
    public static INameService getNameService() {
113
        return (INameService)getCurrent().getBean(INameService.class);
114
    }
115

    
116
    public static ICommonService getCommonService() {
117
        return (ICommonService)getCurrent().getBean(ICommonService.class);
118
    }
119

    
120
    public static IClassificationService getClassificationService() {
121
        return (IClassificationService)getCurrent().getBean(IClassificationService.class);
122
    }
123

    
124
    public static IVocabularyService getVocabularyService() {
125
        return (IVocabularyService)getCurrent().getBean(IVocabularyService.class);
126
    }
127

    
128
    public static ITermService getTermService() {
129
        return (ITermService)getCurrent().getBean(ITermService.class);
130
    }
131

    
132
    public static IDescriptionService getDescriptionService() {
133
        return (IDescriptionService)getCurrent().getBean(IDescriptionService.class);
134
    }
135

    
136

    
137

    
138
}
(3-3/6)