Skip to content

Commit 209b6a2

Browse files
jansupolsenivam
authored andcommitted
Cache Application#getSingletons not be called twice
Signed-off-by: jansupol <[email protected]>
1 parent 2c3618f commit 209b6a2

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -42,6 +42,7 @@
4242
import org.glassfish.jersey.internal.inject.Binder;
4343
import org.glassfish.jersey.internal.inject.InjectionManager;
4444
import org.glassfish.jersey.internal.spi.AutoDiscoverable;
45+
import org.glassfish.jersey.internal.util.Producer;
4546
import org.glassfish.jersey.internal.util.PropertiesHelper;
4647
import org.glassfish.jersey.internal.util.ReflectionHelper;
4748
import org.glassfish.jersey.internal.util.Tokenizer;
@@ -1194,16 +1195,18 @@ private RuntimeConfig(final ResourceConfig original) {
11941195
this.application = original;
11951196

11961197
final Application customRootApp = ResourceConfig.unwrapCustomRootApplication(original);
1197-
if (customRootApp != null) {
1198-
registerComponentsOf(customRootApp);
1199-
}
1198+
1199+
final Set<Object> rootSingletons = customRootApp != null ? registerComponentsOf(customRootApp) : null;
12001200

12011201
originalRegistrations = Collections.newSetFromMap(new IdentityHashMap<>());
12021202
originalRegistrations.addAll(super.getRegisteredClasses());
12031203

1204+
// Do not call the same Application#getSingletons twice
1205+
final Set<Object> origSingletons = customRootApp != null ? rootSingletons : original.getSingletons();
1206+
12041207
// Register externally provided instances.
12051208
final Set<Object> externalInstances =
1206-
original.getSingletons().stream()
1209+
origSingletons.stream()
12071210
.filter(external -> !originalRegistrations.contains(external.getClass()))
12081211
.collect(Collectors.toSet());
12091212

@@ -1216,10 +1219,10 @@ private RuntimeConfig(final ResourceConfig original) {
12161219
registerClasses(externalClasses);
12171220
}
12181221

1219-
private void registerComponentsOf(final Application application) {
1220-
Errors.processWithException(new Runnable() {
1222+
private Set<Object> registerComponentsOf(final Application application) {
1223+
return Errors.process(new Producer<Set<Object>>() {
12211224
@Override
1222-
public void run() {
1225+
public Set<Object> call() {
12231226
// First register instances that should take precedence over classes
12241227
// in case of duplicate registrations
12251228
final Set<Object> singletons = application.getSingletons();
@@ -1248,8 +1251,10 @@ public void run() {
12481251
})
12491252
.collect(Collectors.toSet()));
12501253
}
1254+
return singletons;
12511255
}
12521256
});
1257+
12531258
}
12541259

12551260
private RuntimeConfig(final Application application) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.tests.api;
18+
19+
import org.glassfish.jersey.server.ServerProperties;
20+
import org.glassfish.jersey.test.JerseyTest;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import javax.ws.rs.GET;
25+
import javax.ws.rs.Path;
26+
import javax.ws.rs.container.ContainerRequestContext;
27+
import javax.ws.rs.container.ContainerRequestFilter;
28+
import javax.ws.rs.core.Application;
29+
import javax.ws.rs.core.Response;
30+
import java.io.IOException;
31+
import java.util.Collections;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.Set;
35+
import java.util.concurrent.atomic.AtomicInteger;
36+
37+
public class ApplicationCachingTest extends JerseyTest {
38+
39+
private static AtomicInteger singletonCounter = new AtomicInteger(0);
40+
41+
public static class ApplicationCachingTestFilter implements ContainerRequestFilter {
42+
@Override
43+
public void filter(ContainerRequestContext requestContext) throws IOException {
44+
45+
}
46+
}
47+
48+
@Path("/")
49+
public static class ApplicationCachingTestResource {
50+
@GET
51+
public String get() {
52+
return "GET";
53+
}
54+
}
55+
56+
public static class OneTimeCalledApplication extends Application {
57+
@Override
58+
public Map<String, Object> getProperties() {
59+
Map<String, Object> map = new HashMap<>();
60+
map.put(ServerProperties.WADL_FEATURE_DISABLE, true);
61+
return map;
62+
}
63+
64+
@Override
65+
public Set<Object> getSingletons() {
66+
singletonCounter.incrementAndGet();
67+
return Collections.singleton(new ApplicationCachingTestFilter());
68+
}
69+
70+
@Override
71+
public Set<Class<?>> getClasses() {
72+
return Collections.singleton(ApplicationCachingTestResource.class);
73+
}
74+
}
75+
76+
@Override
77+
protected Application configure() {
78+
return new OneTimeCalledApplication();
79+
}
80+
81+
@Test
82+
public void testOneTimeCalled() {
83+
try (Response r = target().request().get()) {
84+
Assert.assertEquals(200, r.getStatus());
85+
}
86+
Assert.assertEquals(1, singletonCounter.get());
87+
}
88+
}

0 commit comments

Comments
 (0)