Skip to content

Commit 334d141

Browse files
author
Denis
authored
fix: use ServletContext classloader as a default classloader for service (#11308)
Fixes #4367
1 parent d197919 commit 334d141

5 files changed

Lines changed: 79 additions & 22 deletions

File tree

‎flow-lit-template/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ protected Instantiator createInstantiator() throws ServiceException {
107107
return super.createInstantiator();
108108
}
109109

110+
@Override
111+
public void setClassLoader(ClassLoader classLoader) {
112+
if (classLoader != null) {
113+
super.setClassLoader(classLoader);
114+
}
115+
}
116+
110117
@Override
111118
public void init() {
112119
try {

‎flow-polymer-template/src/test/java/com/vaadin/flow/server/MockVaadinServletService.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ protected Instantiator createInstantiator() throws ServiceException {
107107
return super.createInstantiator();
108108
}
109109

110+
@Override
111+
public void setClassLoader(ClassLoader classLoader) {
112+
if (classLoader != null) {
113+
super.setClassLoader(classLoader);
114+
}
115+
}
116+
110117
@Override
111118
public void init() {
112119
try {

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,28 +201,6 @@ public abstract class VaadinService implements Serializable {
201201
*/
202202
public VaadinService(DeploymentConfiguration deploymentConfiguration) {
203203
this.deploymentConfiguration = deploymentConfiguration;
204-
205-
final String classLoaderName = getDeploymentConfiguration()
206-
.getClassLoaderName();
207-
if (classLoaderName != null) {
208-
try {
209-
final Class<?> classLoaderClass = getClass().getClassLoader()
210-
.loadClass(classLoaderName);
211-
final Constructor<?> c = classLoaderClass
212-
.getConstructor(ClassLoader.class);
213-
setClassLoader((ClassLoader) c.newInstance(
214-
new Object[] { getClass().getClassLoader() }));
215-
} catch (final Exception e) {
216-
throw new RuntimeException(
217-
"Could not find specified class loader: "
218-
+ classLoaderName,
219-
e);
220-
}
221-
}
222-
223-
if (getClassLoader() == null) {
224-
setDefaultClassLoader();
225-
}
226204
}
227205

228206
/**
@@ -246,6 +224,7 @@ protected VaadinService() {
246224
* if a problem occurs when creating the service
247225
*/
248226
public void init() throws ServiceException {
227+
doSetClassLoader();
249228
instantiator = createInstantiator();
250229

251230
// init the router now so that registry will be available for
@@ -2408,4 +2387,29 @@ public static String getCsrfTokenAttributeName() {
24082387
return VaadinSession.class.getName() + "."
24092388
+ ApplicationConstants.CSRF_TOKEN;
24102389
}
2390+
2391+
private void doSetClassLoader() {
2392+
final String classLoaderName = getDeploymentConfiguration() == null
2393+
? null
2394+
: getDeploymentConfiguration().getClassLoaderName();
2395+
if (classLoaderName != null) {
2396+
try {
2397+
final Class<?> classLoaderClass = getClass().getClassLoader()
2398+
.loadClass(classLoaderName);
2399+
final Constructor<?> c = classLoaderClass
2400+
.getConstructor(ClassLoader.class);
2401+
setClassLoader((ClassLoader) c.newInstance(
2402+
new Object[] { getClass().getClassLoader() }));
2403+
} catch (final Exception e) {
2404+
throw new RuntimeException(
2405+
"Could not find specified class loader: "
2406+
+ classLoaderName,
2407+
e);
2408+
}
2409+
}
2410+
2411+
if (getClassLoader() == null) {
2412+
setDefaultClassLoader();
2413+
}
2414+
}
24112415
}

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinServletService.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,9 @@ public String getContextRootRelativePath(VaadinRequest request) {
314314
protected VaadinContext constructVaadinContext() {
315315
return new VaadinServletContext(getServlet().getServletContext());
316316
}
317+
318+
@Override
319+
protected void setDefaultClassLoader() {
320+
setClassLoader(getServlet().getServletContext().getClassLoader());
321+
}
317322
}

‎flow-server/src/test/java/com/vaadin/flow/server/VaadinServletServiceTest.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.vaadin.flow.server;
22

3+
import javax.servlet.ServletContext;
34
import javax.servlet.http.HttpServletRequest;
45

56
import java.net.MalformedURLException;
67
import java.net.URL;
8+
import java.util.Collections;
9+
import java.util.List;
710

811
import org.junit.After;
912
import org.junit.Assert;
1013
import org.junit.Before;
1114
import org.junit.Test;
1215
import org.mockito.Mockito;
1316

17+
import com.vaadin.flow.di.Instantiator;
1418
import com.vaadin.flow.internal.UsageStatistics;
1519
import com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServletService;
1620
import com.vaadin.flow.theme.AbstractTheme;
@@ -126,6 +130,36 @@ public void should_report_flow_bootstrapHandler() {
126130
.equals(e.getName())));
127131
}
128132

133+
@Test
134+
public void init_classLoaderIsSetUsingServletContext()
135+
throws ServiceException {
136+
VaadinServlet servlet = Mockito.mock(VaadinServlet.class);
137+
ServletContext context = Mockito.mock(ServletContext.class);
138+
Mockito.when(servlet.getServletContext()).thenReturn(context);
139+
140+
ClassLoader loader = Mockito.mock(ClassLoader.class);
141+
Mockito.when(context.getClassLoader()).thenReturn(loader);
142+
143+
VaadinServletService service = new VaadinServletService(servlet,
144+
mocks.getDeploymentConfiguration()) {
145+
@Override
146+
protected Instantiator createInstantiator()
147+
throws ServiceException {
148+
return Mockito.mock(Instantiator.class);
149+
}
150+
151+
@Override
152+
protected List<RequestHandler> createRequestHandlers()
153+
throws ServiceException {
154+
return Collections.emptyList();
155+
}
156+
};
157+
158+
service.init();
159+
160+
Assert.assertSame(loader, service.getClassLoader());
161+
}
162+
129163
private String testLocation(String base, String contextPath,
130164
String servletPath, String pathInfo) throws Exception {
131165

0 commit comments

Comments
 (0)