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