Skip to content

Commit

Permalink
Ensure the RequestScope and other singleton bindings are registered j…
Browse files Browse the repository at this point in the history
…ust once in HK2

Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Apr 24, 2024
1 parent 75a9755 commit 6d1b86d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2024 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 @@ -43,6 +43,10 @@ public class Hk2BootstrapBinder extends AbstractBinder {

@Override
protected void configure() {
// Singletons, install once
if (serviceLocator.getService(RequestScope.class) != null) {
return;
}
install(
// Jersey-like class analyzer that is able to choose the right services' constructor.
new JerseyClassAnalyzer.Binder(serviceLocator),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2024 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.e2e.inject;

import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.util.collection.Ref;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.jetty.JettyTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
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.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.GenericType;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SingleRequestScopeInjectionTest extends JerseyTest {
@Path("hello")
public static class HelloResource {
@GET
public String getHello() {
return "Hello World!";
}
}
@Override
protected Application configure() {
ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class);
resourceConfig.register(new InjectedFilterRegistrar(InjectedFilter.class));
return resourceConfig;
}
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new JettyTestContainerFactory();
}
@Test
public void test() {
final String hello = target("hello").request().get(String.class);
assertEquals("Hello World!", hello);
}
public static class InjectedFilter implements ContainerRequestFilter {
@Inject
private InjectionManager injectionManager;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
Ref<HttpServletRequest> requestRef =
injectionManager.getInstance((new GenericType<Ref<HttpServletRequest>>() {}).getType());
if (requestRef == null || requestRef.get() == null) {
throw new IllegalStateException("Request not injected");
}
}
}
public static class InjectedFilterRegistrar implements DynamicFeature {
private final Class<?> filterToRegister;
private final AtomicInteger ai = new AtomicInteger(0);
public InjectedFilterRegistrar(Class<?> filterToRegister) {
this.filterToRegister = filterToRegister;
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if (ai.get() == 1) {
context.register(filterToRegister);
}
}
}
}

0 comments on commit 6d1b86d

Please sign in to comment.