Project

General

Profile

« Previous | Next » 

Revision 10f9654c

Added by Andreas Kohlbecker almost 7 years ago

fix #6628 UI enablement via per instance property files, UIs are now inactive per default

View differences:

src/main/java/eu/etaxonomy/cdm/addon/config/CdmVaadinConfiguration.java
8 8
*/
9 9
package eu.etaxonomy.cdm.addon.config;
10 10

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

  
11 15
import javax.servlet.annotation.WebServlet;
12 16

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

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

  
32
import eu.etaxonomy.cdm.opt.config.DataSourceConfigurer;
20 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;
21 40
import eu.etaxonomy.vaadin.ui.annotation.EnableVaadinSpringNavigation;
22 41

  
23 42
/**
......
31 50
        "eu.etaxonomy.cdm.vaadin",
32 51
        "eu.etaxonomy.vaadin.ui",
33 52
        "eu.etaxonomy.cdm.mock" // FIXME remove once mocks are no longer needed
34
        })
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
            })
35 60
@EnableVaadin   // this imports VaadinConfiguration
36 61
@EnableVaadinSpringNavigation // activate the NavigationManagerBean
37 62
@EnableAnnotationBasedAccessControl // enable annotation based per view access control
38 63
public class CdmVaadinConfiguration {
39 64

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

  
40 70
    public static final Logger logger = Logger.getLogger(CdmVaadinConfiguration.class);
41 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

  
42 77
    /*
43 78
     * NOTE: It is necessary to map the URLs starting with /VAADIN/* since none of the
44 79
     * @WebServlets is mapped to the root path. It is sufficient to configure one of the
......
46 81
     */
47 82
    @WebServlet(value = {"/app/*", "/VAADIN/*"}, asyncSupported = true)
48 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

  
49 116
    }
50 117

  
51 118
    public CdmVaadinConfiguration() {
52 119
        logger.debug("CdmVaadinConfiguration enabled");
53 120
    }
54 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

  
55 182

  
56 183
}
src/main/java/eu/etaxonomy/cdm/vaadin/ui/InactiveUIException.java
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.vaadin.ui;
10

  
11
/**
12
 * @author a.kohlbecker
13
 * @since May 8, 2017
14
 *
15
 */
16
public class InactiveUIException extends Exception {
17

  
18

  
19
    private static final long serialVersionUID = 3225226104624596439L;
20

  
21
    String viewName;
22

  
23
    public InactiveUIException(String viewName){
24
        super("The requested view '" + viewName + "' is not active for this web application instance.");
25
        this.viewName = viewName;
26
    }
27

  
28
    public String getViewName() {
29
        return viewName;
30
    }
31

  
32

  
33

  
34
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/ConfigFileUtils.java
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.vaadin.util;
10

  
11
import java.io.File;
12
import java.io.FileInputStream;
13
import java.io.FileNotFoundException;
14
import java.io.IOException;
15
import java.util.Properties;
16

  
17
import eu.etaxonomy.cdm.common.CdmUtils;
18

  
19
/**
20
 * @author a.kohlbecker
21
 * @since May 8, 2017
22
 *
23
 */
24
public class ConfigFileUtils extends CdmUtils {
25

  
26
    private static final String CDM_VAADIN_CONFIG_FOLDER = "cdm-vaadin";
27

  
28
    //TODO better store in VaadinSession?
29
    static Properties uiprops = null;
30

  
31
    public static File getPropertiesFile(String instanceName, String propertiesSet) {
32

  
33
        File vaadinConfigFolder = getCdmSubDir(CDM_VAADIN_CONFIG_FOLDER);
34
        return new File(vaadinConfigFolder, instanceName + (propertiesSet == null? "" : "-" + propertiesSet) + ".properties");
35

  
36
    }
37

  
38
    public static Properties getApplicationProperties(String instanceName) throws IOException {
39
        if(uiprops == null){
40
            uiprops = new Properties();
41
            File uiPropertiesFile = getPropertiesFile(instanceName, "app");
42
            if(uiPropertiesFile.exists()){
43
                try {
44
                    uiprops.load(new FileInputStream(uiPropertiesFile));
45
                } catch (FileNotFoundException e) {
46
                    // must not happen since we checked before
47
                }
48
            }
49
        }
50
        return uiprops;
51
    }
52
}

Also available in: Unified diff