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.hibernate.SessionFactory;
|
20
|
import org.hibernate.event.service.spi.EventListenerRegistry;
|
21
|
import org.hibernate.event.spi.EventType;
|
22
|
import org.hibernate.internal.SessionFactoryImpl;
|
23
|
import org.springframework.beans.BeansException;
|
24
|
import org.springframework.beans.factory.annotation.Autowired;
|
25
|
import org.springframework.context.ApplicationContext;
|
26
|
import org.springframework.context.ApplicationContextAware;
|
27
|
import org.springframework.context.annotation.Bean;
|
28
|
import org.springframework.context.annotation.ComponentScan;
|
29
|
import org.springframework.context.annotation.ComponentScan.Filter;
|
30
|
import org.springframework.context.annotation.Configuration;
|
31
|
import org.springframework.context.annotation.FilterType;
|
32
|
import org.springframework.core.env.Environment;
|
33
|
import org.springframework.security.authentication.AuthenticationProvider;
|
34
|
import org.vaadin.spring.events.annotation.EnableEventBus;
|
35
|
|
36
|
import com.vaadin.spring.annotation.EnableVaadin;
|
37
|
import com.vaadin.spring.annotation.SpringUI;
|
38
|
import com.vaadin.spring.annotation.UIScope;
|
39
|
import com.vaadin.spring.server.SpringVaadinServlet;
|
40
|
import com.vaadin.ui.UI;
|
41
|
|
42
|
import eu.etaxonomy.cdm.api.application.AbstractDataInserter;
|
43
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
44
|
import eu.etaxonomy.cdm.api.application.DummyDataInserter;
|
45
|
import eu.etaxonomy.cdm.api.cache.CdmCacher;
|
46
|
import eu.etaxonomy.cdm.api.config.ApplicationConfiguration;
|
47
|
import eu.etaxonomy.cdm.api.config.ApplicationConfigurationFile;
|
48
|
import eu.etaxonomy.cdm.api.service.idminter.RegistrationIdentifierMinter;
|
49
|
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
|
50
|
import eu.etaxonomy.cdm.dataInserter.RegistrationRequiredDataInserter;
|
51
|
import eu.etaxonomy.cdm.persistence.hibernate.GrantedAuthorityRevokingRegistrationUpdateLister;
|
52
|
import eu.etaxonomy.cdm.vaadin.security.annotation.EnableAnnotationBasedAccessControl;
|
53
|
import eu.etaxonomy.cdm.vaadin.ui.ConceptRelationshipUI;
|
54
|
import eu.etaxonomy.cdm.vaadin.ui.DistributionStatusUI;
|
55
|
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUI;
|
56
|
import eu.etaxonomy.cdm.vaadin.ui.StatusEditorUI;
|
57
|
import eu.etaxonomy.vaadin.ui.annotation.EnableVaadinSpringNavigation;
|
58
|
|
59
|
/**
|
60
|
*
|
61
|
* @author a.kohlbecker
|
62
|
* @since Feb 8, 2017
|
63
|
*
|
64
|
*/
|
65
|
@Configuration
|
66
|
@ComponentScan(basePackages={
|
67
|
"eu.etaxonomy.vaadin.ui",
|
68
|
"eu.etaxonomy.cdm.vaadin",
|
69
|
"eu.etaxonomy.cdm.service",
|
70
|
"org.springframework.context.event",
|
71
|
"eu.etaxonomy.cdm.ext.registration.messages.redmine" // FIXME needed???
|
72
|
},
|
73
|
// exclude UI classes, these are provided via the @Bean annotated methods below
|
74
|
excludeFilters={@Filter(
|
75
|
pattern="eu\\.etaxonomy\\.cdm\\.vaadin\\.ui\\..*",
|
76
|
type=FilterType.REGEX
|
77
|
)
|
78
|
})
|
79
|
@EnableVaadin // this imports VaadinConfiguration
|
80
|
@EnableVaadinSpringNavigation // activate the NavigationManagerBean
|
81
|
@EnableAnnotationBasedAccessControl // enable annotation based per view access control
|
82
|
@EnableEventBus // enable the vaadin spring event bus
|
83
|
public class CdmVaadinConfiguration implements ApplicationContextAware {
|
84
|
|
85
|
public static final String CDM_VAADIN_UI_ACTIVATED = "cdm-vaadin.ui.activated";
|
86
|
public static final String CDM_SERVICE_MINTER_REGSTRATION_MINID = "cdm.service.minter.registration.minLocalId";
|
87
|
public static final String CDM_SERVICE_MINTER_REGSTRATION_MAXID = "cdm.service.minter.registration.maxLocalId";
|
88
|
public static final String CDM_SERVICE_MINTER_REGSTRATION_IDFORMAT = "cdm.service.minter.registration.identifierFormatString";
|
89
|
|
90
|
public static final Logger logger = Logger.getLogger(CdmVaadinConfiguration.class);
|
91
|
|
92
|
@Autowired
|
93
|
Environment env;
|
94
|
|
95
|
@Autowired
|
96
|
private SessionFactory sessionFactory;
|
97
|
|
98
|
@Autowired
|
99
|
private ApplicationConfiguration appConfig;
|
100
|
|
101
|
@Autowired
|
102
|
private void setTermCacher(CdmCacher termCacher){
|
103
|
CdmTransientEntityCacher.setDefaultCacher(termCacher);
|
104
|
}
|
105
|
|
106
|
private boolean registrationUiHibernateEventListenersDone = false;
|
107
|
|
108
|
|
109
|
ApplicationConfigurationFile configFile = new ApplicationConfigurationFile(PROPERTIES_FILE_NAME, APP_FILE_CONTENT);
|
110
|
|
111
|
/*
|
112
|
* NOTE: It is necessary to map the URLs starting with /VAADIN/* since none of the
|
113
|
* @WebServlets is mapped to the root path. It is sufficient to configure one of the
|
114
|
* servlets with this path see BookOfVaadin 5.9.5. Servlet Mapping with URL Patterns
|
115
|
*/
|
116
|
@WebServlet(value = {"/app/*", "/VAADIN/*"}, asyncSupported = true)
|
117
|
public static class Servlet extends SpringVaadinServlet {
|
118
|
|
119
|
private static final long serialVersionUID = -2615042297393028775L;
|
120
|
|
121
|
|
122
|
/**
|
123
|
*
|
124
|
@SuppressWarnings("serial")
|
125
|
@Override
|
126
|
protected void servletInitialized() throws ServletException {
|
127
|
getService().addSessionInitListener(new SessionInitListener() {
|
128
|
|
129
|
@Override
|
130
|
public void sessionInit(SessionInitEvent sessionInitEvent) throws ServiceException {
|
131
|
VaadinSession session = sessionInitEvent.getSession();
|
132
|
session.setErrorHandler(new DefaultErrorHandler(){
|
133
|
|
134
|
@Override
|
135
|
public void error(ErrorEvent errorEvent) {
|
136
|
// ...
|
137
|
}
|
138
|
|
139
|
});
|
140
|
).getServiceRegistry().getService
|
141
|
|
142
|
}});
|
143
|
|
144
|
}
|
145
|
*/
|
146
|
|
147
|
}
|
148
|
|
149
|
public CdmVaadinConfiguration() {
|
150
|
logger.debug("CdmVaadinConfiguration enabled");
|
151
|
}
|
152
|
|
153
|
@Bean
|
154
|
@UIScope
|
155
|
public ConceptRelationshipUI conceptRelationshipUI() {
|
156
|
if(isUIEnabled(ConceptRelationshipUI.class)){
|
157
|
return new ConceptRelationshipUI();
|
158
|
}
|
159
|
return null;
|
160
|
}
|
161
|
|
162
|
@Bean
|
163
|
@UIScope
|
164
|
public RegistrationUI registrationUI() {
|
165
|
if(isUIEnabled(RegistrationUI.class)){
|
166
|
registerRegistrationUiHibernateEventListeners();
|
167
|
|
168
|
return new RegistrationUI();
|
169
|
}
|
170
|
return null;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* this is only a quick implementation for testing,
|
175
|
* TODO see also the NOTE on CdmListenerIntegrator class declaration for a prospective better solution
|
176
|
*/
|
177
|
protected void registerRegistrationUiHibernateEventListeners() {
|
178
|
if(!registrationUiHibernateEventListenersDone){
|
179
|
EventListenerRegistry listenerRegistry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(
|
180
|
EventListenerRegistry.class);
|
181
|
GrantedAuthorityRevokingRegistrationUpdateLister listener = new GrantedAuthorityRevokingRegistrationUpdateLister();
|
182
|
listenerRegistry.appendListeners(EventType.POST_UPDATE, listener);
|
183
|
registrationUiHibernateEventListenersDone = true;
|
184
|
}
|
185
|
}
|
186
|
|
187
|
@Bean
|
188
|
public AbstractDataInserter registrationRequiredDataInserter() throws BeansException{
|
189
|
if(isUIEnabled(RegistrationUI.class)){
|
190
|
RegistrationRequiredDataInserter inserter = new RegistrationRequiredDataInserter();
|
191
|
|
192
|
inserter.setRunAsAuthenticationProvider((AuthenticationProvider) applicationContext.getBean("runAsAuthenticationProvider"));
|
193
|
inserter.setCdmRepository((CdmRepository) applicationContext.getBean("cdmRepository"));
|
194
|
return inserter;
|
195
|
} else {
|
196
|
// the return type implements ApplicationListener and thus must not be null,
|
197
|
// therefore we return a empty dummy implementation.
|
198
|
return new DummyDataInserter();
|
199
|
}
|
200
|
}
|
201
|
|
202
|
@Bean
|
203
|
public RegistrationIdentifierMinter registrationIdentifierMinter() throws IOException {
|
204
|
RegistrationIdentifierMinter minter = new RegistrationIdentifierMinter();
|
205
|
|
206
|
minter.setMinLocalId(appConfig.getProperty(configFile , CDM_SERVICE_MINTER_REGSTRATION_MINID));
|
207
|
minter.setMaxLocalId(appConfig.getProperty(configFile , CDM_SERVICE_MINTER_REGSTRATION_MAXID));
|
208
|
minter.setIdentifierFormatString(appConfig.getProperty(configFile , CDM_SERVICE_MINTER_REGSTRATION_IDFORMAT));
|
209
|
return minter;
|
210
|
}
|
211
|
|
212
|
@Bean
|
213
|
@UIScope
|
214
|
public DistributionStatusUI distributionStatusUI() {
|
215
|
if(isUIEnabled(DistributionStatusUI.class)){
|
216
|
return new DistributionStatusUI();
|
217
|
}
|
218
|
return null;
|
219
|
}
|
220
|
|
221
|
@Bean
|
222
|
@UIScope
|
223
|
public StatusEditorUI statusEditorUI() {
|
224
|
if(isUIEnabled(StatusEditorUI.class)){
|
225
|
return new StatusEditorUI();
|
226
|
}
|
227
|
return null;
|
228
|
}
|
229
|
|
230
|
|
231
|
|
232
|
|
233
|
static final String PROPERTIES_FILE_NAME = "vaadin-apps";
|
234
|
|
235
|
private Properties appProps = null;
|
236
|
|
237
|
private ApplicationContext applicationContext;
|
238
|
|
239
|
private List<String> activeUIpaths;
|
240
|
|
241
|
//@formatter:off
|
242
|
private static final String APP_FILE_CONTENT=
|
243
|
"########################################################\n"+
|
244
|
"# \n"+
|
245
|
"# Vaadin application specific configurations \n"+
|
246
|
"# \n"+
|
247
|
"########################################################\n"+
|
248
|
" \n"+
|
249
|
"# Enablement of vaadin uis. \n"+
|
250
|
"# \n"+
|
251
|
"# Multiple uis can be defined as comma separated list. \n"+
|
252
|
"# Whitespace before and after the comma will be ignored.\n"+
|
253
|
"# Valid values are the path properties of the @SpringUI \n"+
|
254
|
"# annotation which is used for UI classes. \n"+
|
255
|
"cdm-vaadin.ui.activated=concept,distribution,editstatus \n";
|
256
|
//@formatter:on
|
257
|
|
258
|
/**
|
259
|
* Checks if the ui class supplied is activated by listing it in the properties by its {@link SpringUI#path()} value.
|
260
|
*
|
261
|
* TODO see https://dev.e-taxonomy.eu/redmine/issues/7139 (consider using spring profiles to enable vaadin UI contexts)
|
262
|
*
|
263
|
* @param type
|
264
|
* @return
|
265
|
*/
|
266
|
private boolean isUIEnabled(Class<? extends UI>uiClass) {
|
267
|
|
268
|
String path = uiClass.getAnnotation(SpringUI.class).path().trim();
|
269
|
|
270
|
if(activeUIpaths == null){
|
271
|
String activatedVaadinUIs = env.getProperty(CDM_VAADIN_UI_ACTIVATED);
|
272
|
if(activatedVaadinUIs == null){
|
273
|
// not in environment? Read it from the config file!
|
274
|
activatedVaadinUIs = appConfig.getProperty(configFile , CDM_VAADIN_UI_ACTIVATED);
|
275
|
}
|
276
|
|
277
|
if(activatedVaadinUIs != null) {
|
278
|
String[] uiPaths = activatedVaadinUIs.split("\\s*,\\s*");
|
279
|
this.activeUIpaths = Arrays.asList(uiPaths);
|
280
|
}
|
281
|
}
|
282
|
if(activeUIpaths.stream().anyMatch(p -> p.trim().equals(path))){
|
283
|
return true;
|
284
|
}
|
285
|
return false;
|
286
|
|
287
|
}
|
288
|
|
289
|
|
290
|
|
291
|
/**
|
292
|
* {@inheritDoc}
|
293
|
*/
|
294
|
@Override
|
295
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
296
|
this.applicationContext = applicationContext;
|
297
|
}
|
298
|
|
299
|
|
300
|
}
|