Project

General

Profile

Download (6.32 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.addon.config;
10

    
11
import java.io.IOException;
12
import java.util.Arrays;
13
import java.util.Properties;
14

    
15
import javax.servlet.annotation.WebServlet;
16

    
17
import org.apache.log4j.Logger;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.context.annotation.Bean;
20
import org.springframework.context.annotation.ComponentScan;
21
import org.springframework.context.annotation.ComponentScan.Filter;
22
import org.springframework.context.annotation.Configuration;
23
import org.springframework.context.annotation.FilterType;
24
import org.springframework.context.annotation.Lazy;
25

    
26
import com.vaadin.spring.annotation.EnableVaadin;
27
import com.vaadin.spring.annotation.SpringUI;
28
import com.vaadin.spring.annotation.UIScope;
29
import com.vaadin.spring.server.SpringVaadinServlet;
30
import com.vaadin.ui.UI;
31

    
32
import eu.etaxonomy.cdm.opt.config.DataSourceConfigurer;
33
import eu.etaxonomy.cdm.vaadin.security.annotation.EnableAnnotationBasedAccessControl;
34
import eu.etaxonomy.cdm.vaadin.ui.ConceptRelationshipUI;
35
import eu.etaxonomy.cdm.vaadin.ui.DistributionStatusUI;
36
import eu.etaxonomy.cdm.vaadin.ui.InactiveUIException;
37
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUI;
38
import eu.etaxonomy.cdm.vaadin.ui.StatusEditorUI;
39
import eu.etaxonomy.cdm.vaadin.util.ConfigFileUtils;
40
import eu.etaxonomy.vaadin.ui.annotation.EnableVaadinSpringNavigation;
41

    
42
/**
43
 *
44
 * @author a.kohlbecker
45
 * @since Feb 8, 2017
46
 *
47
 */
48
@Configuration
49
@ComponentScan(basePackages={
50
        "eu.etaxonomy.cdm.vaadin",
51
        "eu.etaxonomy.vaadin.ui",
52
        "eu.etaxonomy.cdm.mock" // FIXME remove once mocks are no longer needed
53
        },
54
        // exclude UI classes, these are provided via the @Bean annotated methods below
55
        excludeFilters={@Filter(
56
                pattern="eu\\.etaxonomy\\.cdm\\.vaadin\\.ui\\..*",
57
                type=FilterType.REGEX
58
                )
59
            })
60
@EnableVaadin   // this imports VaadinConfiguration
61
@EnableVaadinSpringNavigation // activate the NavigationManagerBean
62
@EnableAnnotationBasedAccessControl // enable annotation based per view access control
63
public class CdmVaadinConfiguration {
64

    
65
    /**
66
     *
67
     */
68
    private static final String CDM_VAADIN_UI_ACTIVATED = "cdm-vaadin.ui.activated";
69

    
70
    public static final Logger logger = Logger.getLogger(CdmVaadinConfiguration.class);
71

    
72
    @Autowired
73
    @Lazy
74
    //FIXME consider to set the instanceName (instanceID) in the spring environment to avoid a bean reference here
75
    private DataSourceConfigurer dataSourceConfigurer;
76

    
77
    /*
78
     * NOTE: It is necessary to map the URLs starting with /VAADIN/* since none of the
79
     * @WebServlets is mapped to the root path. It is sufficient to configure one of the
80
     * servlets with this path see BookOfVaadin 5.9.5. Servlet Mapping with URL Patterns
81
     */
82
    @WebServlet(value = {"/app/*", "/VAADIN/*"}, asyncSupported = true)
83
    public static class Servlet extends SpringVaadinServlet {
84

    
85
        private static final long serialVersionUID = -2615042297393028775L;
86

    
87
        /**
88
         *
89
        @SuppressWarnings("serial")
90
        @Override
91
        protected void servletInitialized() throws ServletException {
92
            getService().addSessionInitListener(new SessionInitListener() {
93

    
94
                @Override
95
                public void sessionInit(SessionInitEvent sessionInitEvent) throws ServiceException {
96
                    VaadinSession session = sessionInitEvent.getSession();
97
                    session.setErrorHandler(new DefaultErrorHandler(){
98

    
99
                        @Override
100
                        public void error(ErrorEvent errorEvent) {
101
                            if(errorEvent.getThrowable() instanceof InactiveUIException){
102
                                //TODO redirect to an ErrorUI or show and error Page
103
                                // better use Spring MVC Error handlers instead?
104
                            } else {
105
                                doDefault(errorEvent);
106
                            }
107
                        }
108

    
109
                    });
110

    
111
                }});
112

    
113
        }
114
         */
115

    
116
    }
117

    
118
    public CdmVaadinConfiguration() {
119
        logger.debug("CdmVaadinConfiguration enabled");
120
    }
121

    
122
    @Bean
123
    @UIScope
124
    public ConceptRelationshipUI conceptRelationshipUI() throws InactiveUIException {
125
        if(isUIEnabled(ConceptRelationshipUI.class)){
126
            return new ConceptRelationshipUI();
127
        }
128
        return null;
129
    }
130

    
131
    @Bean
132
    @UIScope
133
    public RegistrationUI registrationUI() throws InactiveUIException {
134
        if(isUIEnabled(RegistrationUI.class)){
135
            return new RegistrationUI();
136
        }
137
        return null;
138
    }
139

    
140
    @Bean
141
    @UIScope
142
    public DistributionStatusUI distributionStatusUI() throws InactiveUIException {
143
        if(isUIEnabled(DistributionStatusUI.class)){
144
            return new DistributionStatusUI();
145
        }
146
        return null;
147
    }
148

    
149
    @Bean
150
    @UIScope
151
    public StatusEditorUI statusEditorUI() throws InactiveUIException {
152
        if(isUIEnabled(StatusEditorUI.class)){
153
            return new StatusEditorUI();
154
        }
155
        return null;
156
    }
157

    
158

    
159
    /**
160
     * Checks if the ui class supplied is activated by listing it in the properties by its {@link SpringUI#path()} value.
161
     *
162
     * @param type
163
     * @return
164
     * @throws InactiveUIException
165
     */
166
    private boolean isUIEnabled(Class<? extends UI>uiClass) throws InactiveUIException {
167
        String path = uiClass.getAnnotation(SpringUI.class).path();
168

    
169
        try {
170
            Properties appProps = ConfigFileUtils.getApplicationProperties(dataSourceConfigurer.dataSourceProperties().getCurrentDataSourceId());
171
            if(appProps.get(CDM_VAADIN_UI_ACTIVATED) != null){
172
                String[] uiPaths = appProps.get(CDM_VAADIN_UI_ACTIVATED).toString().split("\\s*,\\s*");
173
                return Arrays.asList(uiPaths).stream().anyMatch(p -> p.equals(path));
174
            }
175
            throw new InactiveUIException(path);
176
        } catch (IOException e) {
177
            logger.error("Error reading the vaadin ui properties file. File corrupted?. Stopping instance ...");
178
            throw new RuntimeException(e);
179
        }
180
    }
181

    
182

    
183
}
(1-1/2)