-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
- Loading branch information
Showing
39 changed files
with
2,249 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-inject</artifactId> | ||
<name>jersey-ext-cdi-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> |
113 changes: 113 additions & 0 deletions
113
...-inject/src/main/java/org/glassfish/jersey/ext/cdi1x/inject/internal/InjectExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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 java.lang.reflect.Type; | ||
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 Type baseType = processAnnotatedType.getAnnotatedType().getBaseType(); | ||
if (Application.class.isAssignableFrom((Class<?>) baseType) | ||
&& Configuration.class.isAssignableFrom((Class<?>) baseType)) { | ||
if (!((Class<?>) baseType).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(javax.ws.rs.ext.Providers.class); | ||
injectables.add(Request.class); | ||
injectables.add(ResourceContext.class); | ||
injectables.add(ResourceInfo.class); | ||
injectables.add(SecurityContext.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); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...sey-cdi-inject/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.glassfish.jersey.ext.cdi1x.inject.internal.InjectExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.