-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better @Inject support. For servlet classes, a qualifier is used.
Signed-off-by: jansupol <[email protected]>
- Loading branch information
Showing
14 changed files
with
619 additions
and
55 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
61 changes: 61 additions & 0 deletions
61
...rs-inject/src/main/java/org/glassfish/jersey/ext/cdi1x/inject/internal/JerseyContext.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,61 @@ | ||
/* | ||
* Copyright (c) 2022 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 javax.inject.Qualifier; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* <p> | ||
* Qualifier used for injecting the CDI beans using {@code @Inject}. Used only for beans that originate in Specification other | ||
* than Jakarta RESTful Web Services (such as Servlet). Such beans must not be produced with {@code @Default} qualifier so that | ||
* there is no ambiguity for the CDI injection. | ||
* </p> | ||
* <p> | ||
* Jakarta REST Spec. Section 11 demands {@code HttpServletRequest}, {@code HttpServletResponse}, {@code ServletContext}, | ||
* {@code ServletConfig}, and {@code FilterConfig} to be available by injections using {@code @Context}. For CDI, these | ||
* servlet classes are available with {@link JerseyContext} qualifier. | ||
* </p> | ||
* <p> | ||
* Note that {@code @Context} injection is not aware of the qualifier and using {@code @Context} in conjuction with | ||
* {@code @JerseyContext} will not work. | ||
* </p> | ||
* <p> | ||
* Can be used as e.g. | ||
* | ||
* <pre> | ||
* @Inject | ||
* @JerseyContext //internal | ||
* HttpServletRequest httpServletRequest; | ||
* </pre> | ||
* or as iterable of all {@code HttpServletRequest} beans | ||
* <pre> | ||
* @Inject | ||
* @Any | ||
* Instance<HttpServletRequest> httpServletRequests; | ||
* </pre> | ||
* </p> | ||
* @since 2.38 | ||
*/ | ||
@Qualifier | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) | ||
public @interface JerseyContext { | ||
} |
84 changes: 84 additions & 0 deletions
84
...rc/main/java/org/glassfish/jersey/ext/cdi1x/inject/internal/ServletReferenceProducer.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,84 @@ | ||
/* | ||
* Copyright (c) 2022 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.inject.InjectionManager; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.inject.Produces; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.inject.Inject; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletContext; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* A CDI producer producing servlet beans in a {@code RequestScope}. | ||
*/ | ||
class ServletReferenceProducer { | ||
@Inject | ||
InjectionManager injectionManager; | ||
|
||
@Inject | ||
BeanManager beanManager; | ||
|
||
@Produces | ||
@JerseyContext | ||
@RequestScoped | ||
public HttpServletRequest produceHttpServletRequest() { | ||
return injectionManager().getInstance(HttpServletRequest.class); | ||
} | ||
|
||
@Produces | ||
@JerseyContext | ||
@RequestScoped | ||
public HttpServletResponse produceHttpServletResponse() { | ||
return injectionManager().getInstance(HttpServletResponse.class); | ||
} | ||
|
||
@Produces | ||
@JerseyContext | ||
@RequestScoped | ||
public ServletContext produceServletContext() { | ||
return injectionManager().getInstance(ServletContext.class); | ||
} | ||
|
||
@Produces | ||
@JerseyContext | ||
@RequestScoped | ||
public ServletConfig produceServletConfig() { | ||
return injectionManager().getInstance(ServletConfig.class); | ||
} | ||
|
||
@Produces | ||
@JerseyContext | ||
@RequestScoped | ||
public FilterConfig produceFilterConfig() { | ||
return injectionManager().getInstance(FilterConfig.class); | ||
} | ||
|
||
private InjectionManager injectionManager() { | ||
InjectionManager injectionManager = beanManager.getExtension(CdiComponentProvider.class).getEffectiveInjectionManager(); | ||
if (injectionManager != null && !injectionManager.isShutdown()) { | ||
return injectionManager; | ||
} | ||
return this.injectionManager; | ||
} | ||
} |
Oops, something went wrong.