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