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

Allow to use @Inject instead of @Context with CDI #4749

Merged
merged 3 commits into from
Apr 15, 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
7 changes: 6 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2013, 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 @@ -243,6 +243,11 @@
<artifactId>jersey-cdi1x-ban-custom-hk2-binding</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi-rs-inject</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.rx</groupId>
<artifactId>jersey-rx-client-guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 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 @@ -30,6 +30,7 @@

import org.glassfish.jersey.internal.BootstrapBag;
import org.glassfish.jersey.internal.BootstrapConfigurator;
import org.glassfish.jersey.internal.PropertiesDelegate;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.process.internal.RequestScoped;
Expand Down Expand Up @@ -132,7 +133,7 @@ protected void configure() {

// Bind proxiable HttpHeaders, Request and ContainerRequestContext injection injection points
bindFactory(ContainerRequestFactory.class)
.to(HttpHeaders.class).to(Request.class)
.to(HttpHeaders.class).to(Request.class).to(PropertiesDelegate.class)
.proxy(true).proxyForSameScope(false)
.in(RequestScoped.class);

Expand Down
50 changes: 50 additions & 0 deletions ext/cdi/jersey-cdi-rs-inject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<version>2.34-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jersey-cdi-rs-inject</artifactId>
<name>jersey-ext-cdi-rs-inject</name>
<description>Allow to annotate by Inject instead of Context</description>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.ext.cdi1x.inject.internal;

import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.collection.LazyValue;
import org.glassfish.jersey.internal.util.collection.Value;
import org.glassfish.jersey.internal.util.collection.Values;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Providers;
import javax.ws.rs.sse.Sse;
import java.security.AccessController;
import java.util.HashSet;
import java.util.Set;

/**
* <p>
* A utility class that makes sure {@code @Inject} can be used instead of {@code @Context} for the Jakarta REST API classes
* and interfaces, such as for {@code Configuration}, or {@code Providers}.
* </p>
* <p>
* Note that {@code ContextResolver} can be injected using {@code @Context}, but the Jakarta REST specification does not require
* the implementation to be capable of doing so. Since {@code ContextResolver} is parametrized type, the injection using CDI's
* {@Inject} is not supported. The {@code ContextResolver} can be obtained from {@code Providers}.
* </p>
*/
@SuppressWarnings("unused")
class InjectExtension implements Extension {
private void processAnnotatedType(@Observes ProcessAnnotatedType<?> processAnnotatedType, BeanManager beanManager) {
final Class<?> baseClass = (Class<?>) processAnnotatedType.getAnnotatedType().getBaseType();
if (Application.class.isAssignableFrom(baseClass) && Configuration.class.isAssignableFrom(baseClass)) {
if (!baseClass.isAnnotationPresent(Alternative.class)) {
processAnnotatedType.veto(); // Filter bean annotated ResourceConfig
}
}
}

private void beforeDiscoveryObserver(@Observes final BeforeBeanDiscovery bbf, final BeanManager beanManager) {
final CdiComponentProvider cdiComponentProvider = beanManager.getExtension(CdiComponentProvider.class);
cdiComponentProvider.addHK2DepenendencyCheck(InjectExtension::isHK2Dependency);
}

private static final boolean isHK2Dependency(Class<?> clazz) {
return JERSEY_BOUND_INJECTABLES.get().contains(clazz);
}

private static final LazyValue<Set<Class<?>>> JERSEY_BOUND_INJECTABLES
= Values.lazy((Value<Set<Class<?>>>) () -> sumNonJerseyBoundInjectables());

private static Set<Class<?>> sumNonJerseyBoundInjectables() {
final Set<Class<?>> injectables = new HashSet<>();

//JAX-RS
injectables.add(Application.class);
injectables.add(Configuration.class);
injectables.add(ContainerRequestContext.class);
injectables.add(HttpHeaders.class);
injectables.add(ParamConverterProvider.class);
injectables.add(Providers.class);
injectables.add(Request.class);
injectables.add(ResourceContext.class);
injectables.add(ResourceInfo.class);
injectables.add(SecurityContext.class);
injectables.add(Sse.class);
injectables.add(UriInfo.class);

//Servlet if available
addOptionally("javax.servlet.http.HttpServletRequest", injectables);
addOptionally("javax.servlet.http.HttpServletResponse", injectables);
addOptionally("javax.servlet.ServletConfig", injectables);
addOptionally("javax.servlet.ServletContext", injectables);
addOptionally("javax.servlet.FilterConfig", injectables);

return injectables;
}

private static void addOptionally(String className, Set<Class<?>> set) {
final Class<?> optionalClass = AccessController.doPrivileged(ReflectionHelper.classForNamePA(className));
if (optionalClass != null) {
set.add(optionalClass);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.ext.cdi1x.inject.internal.InjectExtension
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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 All @@ -19,11 +19,14 @@
import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.InjectionTargetFactory;

Expand Down Expand Up @@ -83,11 +86,17 @@ public void preDestroy(final T instance) {
public T getInstance(final Class<T> clazz) {
final CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
final T instance = injectionTarget.produce(creationalContext);
injectionTarget.inject(instance, creationalContext);
if (injectionManager != null) {
injectionManager.inject(instance, CdiComponentProvider.CDI_CLASS_ANALYZER);
}
injectionTarget.postConstruct(instance);
final CdiComponentProvider cdiComponentProvider = beanManager.getExtension(CdiComponentProvider.class);
final CdiComponentProvider.InjectionManagerInjectedCdiTarget hk2managedTarget =
cdiComponentProvider.new InjectionManagerInjectedCdiTarget(injectionTarget) {
@Override
public Set<InjectionPoint> getInjectionPoints() {
return injectionTarget.getInjectionPoints();
}
};
hk2managedTarget.setInjectionManager(injectionManager);
hk2managedTarget.inject(instance, creationalContext);
hk2managedTarget.postConstruct(instance);
return instance;
}

Expand Down
Loading