-
Notifications
You must be signed in to change notification settings - Fork 188
/
MidPointSpringApplication.java
221 lines (187 loc) · 9.05 KB
/
MidPointSpringApplication.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.boot;
import java.lang.management.ManagementFactory;
import java.time.Duration;
import java.util.Collections;
import org.apache.catalina.Context;
import org.apache.catalina.Manager;
import org.apache.catalina.Valve;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.gui.impl.factory.panel.TextAreaPanelFactory;
import com.evolveum.midpoint.gui.impl.registry.GuiComponentRegistryImpl;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* Created by Viliam Repan (lazyman).
* Modified by Lukas Skublik.
*/
@ImportResource(locations = {
"classpath:ctx-common.xml",
"classpath:ctx-configuration.xml",
"classpath*:ctx-repository.xml",
"classpath:ctx-repo-cache.xml",
"classpath:ctx-repo-common.xml",
"classpath:ctx-task.xml",
"classpath:ctx-provisioning.xml",
"classpath:ctx-ucf-connid.xml",
"classpath:ctx-ucf-builtin.xml",
"classpath:ctx-audit.xml",
"classpath:ctx-security.xml",
"classpath:ctx-security-enforcer.xml",
"classpath:ctx-model.xml",
"classpath:ctx-model-common.xml",
"classpath:ctx-report.xml",
"classpath*:ctx-workflow.xml",
"classpath*:ctx-notifications.xml",
"classpath:ctx-certification.xml",
"classpath:ctx-interceptor.xml",
"classpath*:ctx-overlay.xml",
"classpath:ctx-init.xml",
"classpath:ctx-webapp.xml"
})
@Profile("!test")
@SpringBootConfiguration
@ComponentScan(
basePackages = {
"com.evolveum.midpoint.web.security.factory",
"com.evolveum.midpoint.gui",
"com.evolveum.midpoint.gui.api"
},
basePackageClasses = {
TextAreaPanelFactory.class,
GuiComponentRegistryImpl.class
})
@EnableScheduling
public class MidPointSpringApplication extends AbstractSpringBootApplication {
private static final Trace LOGGER = TraceManager.getTrace(MidPointSpringApplication.class);
private static ConfigurableApplicationContext applicationContext = null;
private Context tomcatContext;
public static void main(String[] args) {
System.out.println("ClassPath: " + System.getProperty("java.class.path"));
System.setProperty("xml.catalog.className", "com.evolveum.midpoint.prism.impl.schema.CatalogImpl");
String mode = args != null && args.length > 0 ? args[0] : null;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() +
" Application mode:" + mode + " context:" + applicationContext);
}
if (applicationContext != null && "stop".equals(mode)) {
System.exit(SpringApplication.exit(applicationContext, () -> 0));
} else {
applicationContext = configureApplication(new SpringApplicationBuilder()).run(args);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() +
" Application started context:" + applicationContext);
}
}
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return configureApplication(application);
}
private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder application) {
String mpHome = System.getProperty(MidpointConfiguration.MIDPOINT_HOME_PROPERTY);
if (StringUtils.isEmpty(mpHome)) {
LOGGER.info("{} system property is not set, using default configuration",
MidpointConfiguration.MIDPOINT_HOME_PROPERTY);
mpHome = System.getProperty(MidpointConfiguration.USER_HOME_PROPERTY);
if (!mpHome.endsWith("/")) {
mpHome += "/";
}
mpHome += "midpoint";
System.setProperty(MidpointConfiguration.MIDPOINT_HOME_PROPERTY, mpHome);
}
System.setProperty("spring.config.additional-location", "${midpoint.home}/");
application.bannerMode(Banner.Mode.LOG);
return application.sources(MidPointSpringApplication.class);
}
private void setTomcatContext(Context context) {
this.tomcatContext = context;
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
@Scheduled(fixedDelayString = "${server.tomcat.session-manager-delay:10000}", initialDelayString = "${server.tomcat.session-manager-delay:10000}")
public void invalidExpiredSessions() {
Context context = this.tomcatContext;
if (context != null) {
Manager manager = context.getManager();
if (manager != null) {
try {
manager.backgroundProcess();
} catch (Exception e) {
LOGGER.error("Couldn't execute backgroundProcess on session manager.", e);
}
}
}
}
@Component
@EnableConfigurationProperties(ServerProperties.class)
public class ServerCustomization implements WebServerFactoryCustomizer<MidPointTomcatServletWebServerFactory> {
@Value("${server.servlet.session.timeout}")
private int sessionTimeout;
@Value("${server.servlet.context-path}")
private String servletPath;
@Autowired
private ServerProperties serverProperties;
@Autowired
private TaskManager taskManager;
@Autowired
private Environment env;
@Override
public void customize(MidPointTomcatServletWebServerFactory serverFactory) {
ServletWebServerFactoryCustomizer webServletWebServerFactoryCustomizer = new ServletWebServerFactoryCustomizer(serverProperties);
webServletWebServerFactoryCustomizer.customize(serverFactory);
TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer = new TomcatWebServerFactoryCustomizer(env, serverProperties);
tomcatWebServerFactoryCustomizer.customize(serverFactory);
serverFactory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401"));
serverFactory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/error/403"));
serverFactory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
serverFactory.addErrorPages(new ErrorPage(HttpStatus.GONE, "/error/410"));
serverFactory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error"));
Session session = new Session();
session.setTimeout(Duration.ofMinutes(sessionTimeout));
serverFactory.setSession(session);
serverFactory.setTomcatContextCustomizers(
Collections.singleton(MidPointSpringApplication.this::setTomcatContext));
// Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/).
// See comments in TomcatRootValve
Valve rootValve = new TomcatRootValve(servletPath);
serverFactory.addEngineValves(rootValve);
Valve nodeIdHeaderValve = new NodeIdHeaderValve(taskManager);
serverFactory.addEngineValves(nodeIdHeaderValve);
}
}
}