-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport Payara CDI Extension code to GlassFish
Signed-off-by: smillidge <steve.millidge@payara.fish>
- Loading branch information
Showing
3 changed files
with
95 additions
and
1 deletion.
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
88 changes: 88 additions & 0 deletions
88
src/main/java/org/glassfish/enterprise/concurrent/cdi/ConcurrentCDIExtension.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,88 @@ | ||
/* | ||
* Copyright (c) 2022 Payara Foundation and/or its affiliates. | ||
* | ||
* 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.enterprise.concurrent.cdi; | ||
|
||
import jakarta.enterprise.concurrent.Asynchronous; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.spi.AnnotatedMethod; | ||
import jakarta.enterprise.inject.spi.AnnotatedType; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery; | ||
import jakarta.enterprise.inject.spi.Extension; | ||
import jakarta.enterprise.inject.spi.ProcessAnnotatedType; | ||
import jakarta.enterprise.inject.spi.WithAnnotations; | ||
import jakarta.transaction.Transactional; | ||
import java.lang.reflect.Method; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.logging.Logger; | ||
import org.glassfish.enterprise.concurrent.AsynchronousInterceptor; | ||
|
||
/** | ||
* CDI Extension for Jakarta Concurrent implementation backported from Payara. | ||
* | ||
* @author Petr Aubrecht (Payara) | ||
*/ | ||
public class ConcurrentCDIExtension implements Extension { | ||
|
||
private static final Logger log = Logger.getLogger(ConcurrentCDIExtension.class.getName()); | ||
|
||
void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) { | ||
log.finest("ConcurrentCDIExtension.beforeBeanDiscovery"); | ||
// Add each of the Concurrent interceptors | ||
beforeBeanDiscovery.addInterceptorBinding(Asynchronous.class); | ||
AnnotatedType<AsynchronousInterceptor> asynchronousInterceptor | ||
= beanManager.createAnnotatedType(AsynchronousInterceptor.class); | ||
beforeBeanDiscovery.addAnnotatedType(asynchronousInterceptor, AsynchronousInterceptor.class.getName()); | ||
} | ||
|
||
<T> void processAnnotatedType(@Observes @WithAnnotations({Asynchronous.class}) ProcessAnnotatedType<T> processAnnotatedType, | ||
BeanManager beanManager) throws Exception { | ||
log.finest("ConcurrentCDIExtension.processAnnotatedType"); | ||
AnnotatedType<T> annotatedType = processAnnotatedType.getAnnotatedType(); | ||
|
||
// Validate the Asynchronous annotations for each annotated method | ||
Set<AnnotatedMethod<? super T>> annotatedMethods = annotatedType.getMethods(); | ||
for (AnnotatedMethod<?> annotatedMethod : annotatedMethods) { | ||
Method method = annotatedMethod.getJavaMember(); | ||
if (method.getDeclaringClass().equals(AsynchronousInterceptor.class)) { | ||
// skip interceptor | ||
continue; | ||
} | ||
Asynchronous annotation = method.getAnnotation(Asynchronous.class); | ||
if (annotation == null) { | ||
// method in the class, which is NOT annotated @Asynchronous | ||
continue; | ||
} | ||
Class<?> returnType = method.getReturnType(); | ||
boolean validReturnType = returnType.equals(Void.TYPE) | ||
|| returnType.equals(CompletableFuture.class) | ||
|| returnType.equals(CompletionStage.class); | ||
if (!validReturnType) { | ||
throw new UnsupportedOperationException("Method \"" + method.getName() + "\"" | ||
+ " annotated with " + Asynchronous.class.getCanonicalName() + " does not return a CompletableFuture, CompletableFuture or void."); | ||
} | ||
Transactional transactionalAnnotation = annotatedMethod.getAnnotation(Transactional.class); | ||
if (transactionalAnnotation != null | ||
&& transactionalAnnotation.value() != Transactional.TxType.REQUIRES_NEW | ||
&& transactionalAnnotation.value() != Transactional.TxType.NOT_SUPPORTED) { | ||
throw new UnsupportedOperationException("Method \"" + method.getName() + "\"" | ||
+ " annotated with " + Asynchronous.class.getCanonicalName() + " is annotated with @Transactional, but not one of the allowed types: REQUIRES_NEW or NOT_SUPPORTED."); | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/jakarta.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.enterprise.concurrent.cdi.ConcurrentCDIExtension |