This is an automated email from the ASF dual-hosted git repository.

jlmonteiro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/master by this push:
     new 7e83701  Fix classloading issue by using a different approach to 
discover annotations.
7e83701 is described below

commit 7e8370155d4431e03e6e0e38a7377426f5db8a6c
Author: Jean-Louis Monteiro <jeano...@gmail.com>
AuthorDate: Wed Jul 15 23:46:56 2020 +0200

    Fix classloading issue by using a different approach to discover 
annotations.
---
 .../tomee/security/cdi/TomEESecurityExtension.java | 107 +++++++++------------
 1 file changed, 47 insertions(+), 60 deletions(-)

diff --git 
a/tomee/tomee-security/src/main/java/org/apache/tomee/security/cdi/TomEESecurityExtension.java
 
b/tomee/tomee-security/src/main/java/org/apache/tomee/security/cdi/TomEESecurityExtension.java
index 7a315be..591523f 100644
--- 
a/tomee/tomee-security/src/main/java/org/apache/tomee/security/cdi/TomEESecurityExtension.java
+++ 
b/tomee/tomee-security/src/main/java/org/apache/tomee/security/cdi/TomEESecurityExtension.java
@@ -31,13 +31,13 @@ import javax.enterprise.event.Observes;
 import javax.enterprise.inject.Any;
 import javax.enterprise.inject.Default;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Annotated;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.BeanAttributes;
 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.enterprise.inject.spi.WithAnnotations;
+import javax.enterprise.inject.spi.ProcessBean;
 import javax.enterprise.util.TypeLiteral;
 import 
javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition;
 import 
javax.security.enterprise.authentication.mechanism.http.CustomFormAuthenticationMechanismDefinition;
@@ -47,19 +47,28 @@ import 
javax.security.enterprise.authentication.mechanism.http.LoginToContinue;
 import javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition;
 import javax.security.enterprise.identitystore.IdentityStore;
 import javax.security.enterprise.identitystore.LdapIdentityStoreDefinition;
-import java.util.HashSet;
-import java.util.Set;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 public class TomEESecurityExtension implements Extension {
 
-    private final Set<AnnotatedType> basicAuthentication = new HashSet<>();
-    private final Set<AnnotatedType> formAuthentication = new HashSet<>();
-    private final Set<AnnotatedType> customAuthentication = new HashSet<>();
+    final List<Class<? extends Annotation>> annotationsToFind = 
Arrays.asList(TomcatUserIdentityStoreDefinition.class,
+                                                                              
DatabaseIdentityStoreDefinition.class,
+                                                                              
LdapIdentityStoreDefinition.class,
+                                                                              
BasicAuthenticationMechanismDefinition.class,
+                                                                              
FormAuthenticationMechanismDefinition.class,
+                                                                              
CustomFormAuthenticationMechanismDefinition.class);
 
-    private final Set<AnnotatedType> tomcatUserIdentityStore = new HashSet<>();
-    private final Set<AnnotatedType> databaseIdentityStore = new HashSet<>();
-    private final Set<AnnotatedType> ldapIdentityStore = new HashSet<>();
+    private final AtomicReference<Annotated> basicMechanism = new 
AtomicReference<>();
+    private final AtomicReference<Annotated> formMechanism = new 
AtomicReference<>();
+    private final AtomicReference<Annotated> customMechanism = new 
AtomicReference<>();
+
+    private final AtomicReference<Annotated> tomcatUserStore = new 
AtomicReference<>();
+    private final AtomicReference<Annotated> databaseStore = new 
AtomicReference<>();
+    private final AtomicReference<Annotated> ldapStore = new 
AtomicReference<>();
 
     void observeBeforeBeanDiscovery(
         @Observes final BeforeBeanDiscovery beforeBeanDiscovery,
@@ -67,7 +76,6 @@ public class TomEESecurityExtension implements Extension {
 
         
beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(DefaultAuthenticationMechanism.class));
         
beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(TomEESecurityServletAuthenticationMechanismMapper.class));
-        // 
beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(TomEEDefaultIdentityStore.class));
 // only if at least idstore was found?
         
beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(TomEEIdentityStoreHandler.class));
 
         
beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(TomEEPbkdf2PasswordHash.class));
@@ -80,50 +88,34 @@ public class TomEESecurityExtension implements Extension {
         
beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(TomEESecurityContext.class));
     }
 
-    void processIdentityStores(
-        @Observes
-        @WithAnnotations({
-                             TomcatUserIdentityStoreDefinition.class,
-                             DatabaseIdentityStoreDefinition.class,
-                             LdapIdentityStoreDefinition.class
-                         }) final ProcessAnnotatedType<?> 
processAnnotatedType) {
+    // using CDI Observes with WithAnnotations seems to trigger loading of the 
ProcessAnnotatedType
+    // and it may fail into a NoClassDefFound pretty hard to pin down
+    public <T> void processBean(@Observes final ProcessBean<T> eventIn, final 
BeanManager beanManager) {
 
-        final AnnotatedType<?> annotatedType = 
processAnnotatedType.getAnnotatedType();
+        final Annotated annotatedType = eventIn.getAnnotated();
 
-        if 
(annotatedType.isAnnotationPresent(TomcatUserIdentityStoreDefinition.class)) {
-            tomcatUserIdentityStore.add(annotatedType);
+        if (tomcatUserStore.get() == null && 
annotatedType.isAnnotationPresent(TomcatUserIdentityStoreDefinition.class)) {
+            tomcatUserStore.set(annotatedType);
         }
 
-        if 
(annotatedType.isAnnotationPresent(DatabaseIdentityStoreDefinition.class)) {
-            databaseIdentityStore.add(annotatedType);
+        if (databaseStore.get() == null && 
annotatedType.isAnnotationPresent(DatabaseIdentityStoreDefinition.class)) {
+            databaseStore.set(annotatedType);
         }
 
-        if 
(annotatedType.isAnnotationPresent(LdapIdentityStoreDefinition.class)) {
-            ldapIdentityStore.add(annotatedType);
+        if (ldapStore.get() == null && 
annotatedType.isAnnotationPresent(LdapIdentityStoreDefinition.class)) {
+            ldapStore.set(annotatedType);
         }
 
-    }
-
-    void processAuthenticationMechanismDefinitions(
-        @Observes
-        @WithAnnotations({
-                             BasicAuthenticationMechanismDefinition.class,
-                             FormAuthenticationMechanismDefinition.class,
-                             CustomFormAuthenticationMechanismDefinition.class
-                         }) final ProcessAnnotatedType<?> 
processAnnotatedType) {
-
-        final AnnotatedType<?> annotatedType = 
processAnnotatedType.getAnnotatedType();
-
-        if 
(annotatedType.isAnnotationPresent(BasicAuthenticationMechanismDefinition.class))
 {
-            basicAuthentication.add(annotatedType);
+        if (basicMechanism.get() == null && 
annotatedType.isAnnotationPresent(BasicAuthenticationMechanismDefinition.class))
 {
+            basicMechanism.set(annotatedType);
         }
 
-        if 
(annotatedType.isAnnotationPresent(FormAuthenticationMechanismDefinition.class))
 {
-            formAuthentication.add(annotatedType);
+        if (formMechanism.get() == null && 
annotatedType.isAnnotationPresent(FormAuthenticationMechanismDefinition.class)) 
{
+            formMechanism.set(annotatedType);
         }
 
-        if 
(annotatedType.isAnnotationPresent(CustomFormAuthenticationMechanismDefinition.class))
 {
-            customAuthentication.add(annotatedType);
+        if (customMechanism.get() == null && 
annotatedType.isAnnotationPresent(CustomFormAuthenticationMechanismDefinition.class))
 {
+            customMechanism.set(annotatedType);
         }
     }
 
@@ -131,7 +123,7 @@ public class TomEESecurityExtension implements Extension {
         @Observes final AfterBeanDiscovery afterBeanDiscovery,
         final BeanManager beanManager) {
 
-        if (!tomcatUserIdentityStore.isEmpty()) {
+        if (tomcatUserStore.get() != null) {
             afterBeanDiscovery
                 .addBean()
                 .id(TomEEDefaultIdentityStore.class.getName() + "#" + 
TomcatUserIdentityStoreDefinition.class.getName())
@@ -161,7 +153,7 @@ public class TomEESecurityExtension implements Extension {
 
         }
 
-        if (!databaseIdentityStore.isEmpty()) {
+        if (databaseStore.get() != null) {
             afterBeanDiscovery
                 .addBean()
                 .id(TomEEDatabaseIdentityStore.class.getName() + "#" + 
DatabaseIdentityStoreDefinition.class.getName())
@@ -190,7 +182,7 @@ public class TomEESecurityExtension implements Extension {
                 });
         }
 
-        if (!ldapIdentityStore.isEmpty()) {
+        if (ldapStore.get() != null) {
             afterBeanDiscovery
                 .addBean()
                 .id(TomEELDAPIdentityStore.class.getName() + "#" + 
LdapIdentityStoreDefinition.class.getName())
@@ -219,7 +211,7 @@ public class TomEESecurityExtension implements Extension {
                 });
         }
 
-        if (!basicAuthentication.isEmpty()) {
+        if (basicMechanism.get() != null) {
             afterBeanDiscovery
                 .addBean()
                 .id(BasicAuthenticationMechanism.class.getName())
@@ -239,7 +231,7 @@ public class TomEESecurityExtension implements Extension {
 
         }
 
-        if (!formAuthentication.isEmpty()) {
+        if (formMechanism.get() != null) {
             afterBeanDiscovery
                 .addBean()
                 .id(FormAuthenticationMechanism.class.getName() + "#" + 
LoginToContinue.class.getName())
@@ -270,7 +262,7 @@ public class TomEESecurityExtension implements Extension {
 
         }
 
-        if (!customAuthentication.isEmpty()) {
+        if (customMechanism.get() != null) {
             afterBeanDiscovery
                 .addBean()
                 .id(CustomFormAuthenticationMechanism.class.getName() + "#" + 
LoginToContinue.class.getName())
@@ -302,13 +294,12 @@ public class TomEESecurityExtension implements Extension {
     }
 
     public boolean hasAuthenticationMechanisms() {
-        return (basicAuthentication.size() + formAuthentication.size() + 
customAuthentication.size()) > 0;
+        return basicMechanism.get() != null || formMechanism.get() != null || 
customMechanism.get() != null;
     }
 
     private Supplier<LoginToContinue> createFormLoginToContinueSupplier(final 
BeanManager beanManager) {
         return () -> {
-            final LoginToContinue loginToContinue = 
formAuthentication.iterator()
-                                                                      .next()
+            final LoginToContinue loginToContinue = formMechanism.get()
                                                                       
.getAnnotation(
                                                                           
FormAuthenticationMechanismDefinition.class)
                                                                       
.loginToContinue();
@@ -319,8 +310,7 @@ public class TomEESecurityExtension implements Extension {
 
     private Supplier<LoginToContinue> 
createCustomFormLoginToContinueSupplier(final BeanManager beanManager) {
         return () -> {
-            final LoginToContinue loginToContinue = 
customAuthentication.iterator()
-                                                                        .next()
+            final LoginToContinue loginToContinue = customMechanism.get()
                                                                         
.getAnnotation(
                                                                             
CustomFormAuthenticationMechanismDefinition.class)
                                                                         
.loginToContinue();
@@ -331,8 +321,7 @@ public class TomEESecurityExtension implements Extension {
 
     private Supplier<TomcatUserIdentityStoreDefinition> 
createTomcatUserIdentityStoreDefinitionSupplier(final BeanManager beanManager) {
         return () -> {
-            final TomcatUserIdentityStoreDefinition annotation = 
tomcatUserIdentityStore.iterator()
-                                                                               
     .next()
+            final TomcatUserIdentityStoreDefinition annotation = 
tomcatUserStore.get()
                                                                                
     .getAnnotation(
                                                                                
         TomcatUserIdentityStoreDefinition.class);
 
@@ -342,8 +331,7 @@ public class TomEESecurityExtension implements Extension {
 
     private Supplier<DatabaseIdentityStoreDefinition> 
createDatabaseIdentityStoreDefinitionSupplier(final BeanManager beanManager) {
         return () -> {
-            final DatabaseIdentityStoreDefinition annotation = 
databaseIdentityStore.iterator()
-                                                                               
     .next()
+            final DatabaseIdentityStoreDefinition annotation = 
databaseStore.get()
                                                                                
     .getAnnotation(
                                                                                
         DatabaseIdentityStoreDefinition.class);
 
@@ -353,8 +341,7 @@ public class TomEESecurityExtension implements Extension {
 
     private Supplier<LdapIdentityStoreDefinition> 
createLdapIdentityStoreDefinitionSupplier(final BeanManager beanManager) {
         return () -> {
-            final LdapIdentityStoreDefinition annotation = 
ldapIdentityStore.iterator()
-                                                                               
     .next()
+            final LdapIdentityStoreDefinition annotation = ldapStore.get()
                                                                                
     .getAnnotation(
                                                                                
         LdapIdentityStoreDefinition.class);
 

Reply via email to