Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Application#getSingletons not be called twice #4845

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -42,6 +42,7 @@
import org.glassfish.jersey.internal.inject.Binder;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.spi.AutoDiscoverable;
import org.glassfish.jersey.internal.util.Producer;
import org.glassfish.jersey.internal.util.PropertiesHelper;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.Tokenizer;
Expand Down Expand Up @@ -1194,16 +1195,18 @@ private RuntimeConfig(final ResourceConfig original) {
this.application = original;

final Application customRootApp = ResourceConfig.unwrapCustomRootApplication(original);
if (customRootApp != null) {
registerComponentsOf(customRootApp);
}

final Set<Object> rootSingletons = customRootApp != null ? registerComponentsOf(customRootApp) : null;

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

// Do not call the same Application#getSingletons twice
final Set<Object> origSingletons = customRootApp != null ? rootSingletons : original.getSingletons();

// Register externally provided instances.
final Set<Object> externalInstances =
original.getSingletons().stream()
origSingletons.stream()
.filter(external -> !originalRegistrations.contains(external.getClass()))
.collect(Collectors.toSet());

Expand All @@ -1216,10 +1219,10 @@ private RuntimeConfig(final ResourceConfig original) {
registerClasses(externalClasses);
}

private void registerComponentsOf(final Application application) {
Errors.processWithException(new Runnable() {
private Set<Object> registerComponentsOf(final Application application) {
return Errors.process(new Producer<Set<Object>>() {
@Override
public void run() {
public Set<Object> call() {
// First register instances that should take precedence over classes
// in case of duplicate registrations
final Set<Object> singletons = application.getSingletons();
Expand Down Expand Up @@ -1248,8 +1251,10 @@ public void run() {
})
.collect(Collectors.toSet()));
}
return singletons;
}
});

}

private RuntimeConfig(final Application application) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.api;

import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public class ApplicationCachingTest extends JerseyTest {

private static AtomicInteger singletonCounter = new AtomicInteger(0);

public static class ApplicationCachingTestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {

}
}

@Path("/")
public static class ApplicationCachingTestResource {
@GET
public String get() {
return "GET";
}
}

public static class OneTimeCalledApplication extends Application {
@Override
public Map<String, Object> getProperties() {
Map<String, Object> map = new HashMap<>();
map.put(ServerProperties.WADL_FEATURE_DISABLE, true);
return map;
}

@Override
public Set<Object> getSingletons() {
singletonCounter.incrementAndGet();
return Collections.singleton(new ApplicationCachingTestFilter());
}

@Override
public Set<Class<?>> getClasses() {
return Collections.singleton(ApplicationCachingTestResource.class);
}
}

@Override
protected Application configure() {
return new OneTimeCalledApplication();
}

@Test
public void testOneTimeCalled() {
try (Response r = target().request().get()) {
Assert.assertEquals(200, r.getStatus());
}
Assert.assertEquals(1, singletonCounter.get());
}
}