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.server.DeploymentConfiguration;
|
27
|
import com.vaadin.server.ServiceException;
|
28
|
import com.vaadin.server.VaadinServletService;
|
29
|
import com.vaadin.spring.annotation.EnableVaadin;
|
30
|
import com.vaadin.spring.annotation.SpringUI;
|
31
|
import com.vaadin.spring.annotation.UIScope;
|
32
|
import com.vaadin.spring.server.SpringVaadinServlet;
|
33
|
import com.vaadin.ui.UI;
|
34
|
|
35
|
import eu.etaxonomy.cdm.common.ConfigFileUtil;
|
36
|
import eu.etaxonomy.cdm.opt.config.DataSourceConfigurer;
|
37
|
import eu.etaxonomy.cdm.vaadin.security.annotation.EnableAnnotationBasedAccessControl;
|
38
|
import eu.etaxonomy.cdm.vaadin.server.CdmSpringVaadinServletService;
|
39
|
import eu.etaxonomy.cdm.vaadin.ui.ConceptRelationshipUI;
|
40
|
import eu.etaxonomy.cdm.vaadin.ui.DistributionStatusUI;
|
41
|
import eu.etaxonomy.cdm.vaadin.ui.InactiveUIException;
|
42
|
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUI;
|
43
|
import eu.etaxonomy.cdm.vaadin.ui.StatusEditorUI;
|
44
|
import eu.etaxonomy.vaadin.ui.annotation.EnableVaadinSpringNavigation;
|
45
|
|
46
|
/**
|
47
|
*
|
48
|
* @author a.kohlbecker
|
49
|
* @since Feb 8, 2017
|
50
|
*
|
51
|
*/
|
52
|
@Configuration
|
53
|
@ComponentScan(basePackages={
|
54
|
"eu.etaxonomy.vaadin.ui",
|
55
|
"eu.etaxonomy.cdm.vaadin",
|
56
|
"eu.etaxonomy.cdm.dataInserter",
|
57
|
"eu.etaxonomy.cdm.service",
|
58
|
"eu.etaxonomy.cdm.vaadin.component", // for the FieldFactories
|
59
|
"org.springframework.context.event"
|
60
|
},
|
61
|
// exclude UI classes, these are provided via the @Bean annotated methods below
|
62
|
excludeFilters={@Filter(
|
63
|
pattern="eu\\.etaxonomy\\.cdm\\.vaadin\\.ui\\..*",
|
64
|
type=FilterType.REGEX
|
65
|
)
|
66
|
})
|
67
|
@EnableVaadin // this imports VaadinConfiguration
|
68
|
@EnableVaadinSpringNavigation // activate the NavigationManagerBean
|
69
|
@EnableAnnotationBasedAccessControl // enable annotation based per view access control
|
70
|
public class CdmVaadinConfiguration {
|
71
|
|
72
|
/**
|
73
|
*
|
74
|
*/
|
75
|
private static final String CDM_VAADIN_UI_ACTIVATED = "cdm-vaadin.ui.activated";
|
76
|
|
77
|
public static final Logger logger = Logger.getLogger(CdmVaadinConfiguration.class);
|
78
|
|
79
|
@Autowired
|
80
|
@Lazy
|
81
|
//FIXME consider to set the instanceName (instanceID) in the spring environment to avoid a bean reference here
|
82
|
private DataSourceConfigurer dataSourceConfigurer;
|
83
|
|
84
|
/*
|
85
|
* NOTE: It is necessary to map the URLs starting with /VAADIN/* since none of the
|
86
|
* @WebServlets is mapped to the root path. It is sufficient to configure one of the
|
87
|
* servlets with this path see BookOfVaadin 5.9.5. Servlet Mapping with URL Patterns
|
88
|
*/
|
89
|
@WebServlet(value = {"/app/*", "/VAADIN/*"}, asyncSupported = true)
|
90
|
public static class Servlet extends SpringVaadinServlet {
|
91
|
|
92
|
private static final long serialVersionUID = -2615042297393028775L;
|
93
|
|
94
|
@Override
|
95
|
protected VaadinServletService createServletService(
|
96
|
DeploymentConfiguration deploymentConfiguration)
|
97
|
throws ServiceException {
|
98
|
|
99
|
// - The SpringVaadinServletService is needed when using a custom service URL
|
100
|
// - The CdmSpringVaadinServletService allows to attach listeners to the requestEnd and
|
101
|
// requestStart method this is important for proper unbinding of Conversations from
|
102
|
// the request threads.
|
103
|
// see ViewScopeConversationHolder
|
104
|
CdmSpringVaadinServletService service = new CdmSpringVaadinServletService(
|
105
|
this, deploymentConfiguration, getServiceUrlPath());
|
106
|
service.init();
|
107
|
return service;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
*
|
112
|
@SuppressWarnings("serial")
|
113
|
@Override
|
114
|
protected void servletInitialized() throws ServletException {
|
115
|
getService().addSessionInitListener(new SessionInitListener() {
|
116
|
|
117
|
@Override
|
118
|
public void sessionInit(SessionInitEvent sessionInitEvent) throws ServiceException {
|
119
|
VaadinSession session = sessionInitEvent.getSession();
|
120
|
session.setErrorHandler(new DefaultErrorHandler(){
|
121
|
|
122
|
@Override
|
123
|
public void error(ErrorEvent errorEvent) {
|
124
|
if(errorEvent.getThrowable() instanceof InactiveUIException){
|
125
|
//TODO redirect to an ErrorUI or show and error Page
|
126
|
// better use Spring MVC Error handlers instead?
|
127
|
} else {
|
128
|
doDefault(errorEvent);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
});
|
133
|
|
134
|
}});
|
135
|
|
136
|
}
|
137
|
*/
|
138
|
|
139
|
}
|
140
|
|
141
|
public CdmVaadinConfiguration() {
|
142
|
logger.debug("CdmVaadinConfiguration enabled");
|
143
|
}
|
144
|
|
145
|
@Bean
|
146
|
@UIScope
|
147
|
public ConceptRelationshipUI conceptRelationshipUI() throws InactiveUIException {
|
148
|
if(isUIEnabled(ConceptRelationshipUI.class)){
|
149
|
return new ConceptRelationshipUI();
|
150
|
}
|
151
|
return null;
|
152
|
}
|
153
|
|
154
|
@Bean
|
155
|
@UIScope
|
156
|
public RegistrationUI registrationUI() throws InactiveUIException {
|
157
|
if(isUIEnabled(RegistrationUI.class)){
|
158
|
return new RegistrationUI();
|
159
|
}
|
160
|
return null;
|
161
|
}
|
162
|
|
163
|
@Bean
|
164
|
@UIScope
|
165
|
public DistributionStatusUI distributionStatusUI() throws InactiveUIException {
|
166
|
if(isUIEnabled(DistributionStatusUI.class)){
|
167
|
return new DistributionStatusUI();
|
168
|
}
|
169
|
return null;
|
170
|
}
|
171
|
|
172
|
@Bean
|
173
|
@UIScope
|
174
|
public StatusEditorUI statusEditorUI() throws InactiveUIException {
|
175
|
if(isUIEnabled(StatusEditorUI.class)){
|
176
|
return new StatusEditorUI();
|
177
|
}
|
178
|
return null;
|
179
|
}
|
180
|
|
181
|
|
182
|
|
183
|
static final String PROPERTIES_NAME = "vaadin-apps";
|
184
|
|
185
|
private Properties appProps = null;
|
186
|
|
187
|
//@formatter:off
|
188
|
private static final String APP_FILE_CONTENT=
|
189
|
"########################################################\n"+
|
190
|
"# \n"+
|
191
|
"# Vaadin application specific configurations \n"+
|
192
|
"# \n"+
|
193
|
"########################################################\n"+
|
194
|
" \n"+
|
195
|
"# Enablement of vaadin uis. \n"+
|
196
|
"# \n"+
|
197
|
"# Multiple uis can be defined as comma separated list. \n"+
|
198
|
"# Whitespace before and after the comma will be ignored.\n"+
|
199
|
"# Valid values are the path properties of the @SpringUI \n"+
|
200
|
"# annotation which is used for UI classes. \n"+
|
201
|
"cdm-vaadin.ui.activated=concept,distribution,editstatus \n";
|
202
|
//@formatter:on
|
203
|
|
204
|
/**
|
205
|
* Checks if the ui class supplied is activated by listing it in the properties by its {@link SpringUI#path()} value.
|
206
|
*
|
207
|
* @param type
|
208
|
* @return
|
209
|
* @throws InactiveUIException
|
210
|
*/
|
211
|
private boolean isUIEnabled(Class<? extends UI>uiClass) throws InactiveUIException {
|
212
|
String path = uiClass.getAnnotation(SpringUI.class).path().trim();
|
213
|
|
214
|
try {
|
215
|
if(appProps == null){
|
216
|
String currentDataSourceId = dataSourceConfigurer.dataSourceProperties().getCurrentDataSourceId();
|
217
|
appProps = new ConfigFileUtil()
|
218
|
.setDefaultContent(APP_FILE_CONTENT)
|
219
|
.getProperties(currentDataSourceId, PROPERTIES_NAME);
|
220
|
}
|
221
|
if(appProps.get(CDM_VAADIN_UI_ACTIVATED) != null){
|
222
|
String[] uiPaths = appProps.get(CDM_VAADIN_UI_ACTIVATED).toString().split("\\s*,\\s*");
|
223
|
if(Arrays.asList(uiPaths).stream().anyMatch(p -> p.trim().equals(path))){
|
224
|
return true;
|
225
|
}
|
226
|
}
|
227
|
throw new InactiveUIException(path);
|
228
|
} catch (IOException e) {
|
229
|
logger.error("Error reading the vaadin ui properties file. File corrupted?. Stopping instance ...");
|
230
|
throw new RuntimeException(e);
|
231
|
}
|
232
|
}
|
233
|
|
234
|
|
235
|
}
|