-
Notifications
You must be signed in to change notification settings - Fork 221
/
SymmetricEngineContextLoaderListener.java
104 lines (92 loc) · 4.47 KB
/
SymmetricEngineContextLoaderListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* This is the standard way to bootstrap Symmetric in a web container. Symmetric uses
* Spring's WebApplicationContext for access to symmetric from its Servlets.
* This servlet context listener forces the contextConfigLocation for Spring to be load
* symmetric.xml.
* <p/>
* Developers have the option to subclass off of this listener and override the createConfigureAndStartEngine()
* method.
*/
public class SymmetricEngineContextLoaderListener extends ContextLoaderListener {
static final String SYMMETRIC_SPRING_LOCATION = "classpath:/symmetric.xml";
@Override
final public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(event.getServletContext());
createConfigureAndStartEngine(ctx);
}
protected void createConfigureAndStartEngine(ApplicationContext ctx) {
SymmetricEngine engine = new SymmetricEngine(ctx);
engine.start();
}
@Override
protected ContextLoader createContextLoader() {
return new ContextLoader() {
@SuppressWarnings("unchecked")
protected WebApplicationContext createWebApplicationContext(
ServletContext servletContext, ApplicationContext parent)
throws BeansException {
Class contextClass = determineContextClass(servletContext);
if (!ConfigurableWebApplicationContext.class
.isAssignableFrom(contextClass)) {
throw new ApplicationContextException(
"Custom context class ["
+ contextClass.getName()
+ "] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils
.instantiateClass(contextClass);
wac.setParent(parent);
wac.setServletContext(servletContext);
String configLocation = servletContext
.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocation == null) {
configLocation = SYMMETRIC_SPRING_LOCATION;
} else if (!configLocation.contains(SYMMETRIC_SPRING_LOCATION)) {
configLocation = SYMMETRIC_SPRING_LOCATION + ","
+ configLocation;
}
wac
.setConfigLocations(StringUtils
.tokenizeToStringArray(
configLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
wac.refresh();
return wac;
}
};
}
}