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