You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2019/11/13 13:36:40 UTC

[myfaces-tobago] branch tobago-4.x updated: TOBAGO-1935: Tobago should be runnable without CDI

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

lofwyr pushed a commit to branch tobago-4.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/tobago-4.x by this push:
     new 3b420ca  TOBAGO-1935: Tobago should be runnable without CDI
3b420ca is described below

commit 3b420ca3cc96f61253b580a7d35f48ac277240ba
Author: Udo Schnurpfeil <ud...@irian.eu>
AuthorDate: Wed Nov 13 14:35:23 2019 +0100

    TOBAGO-1935: Tobago should be runnable without CDI
    
    moving package javax.enterprise.inject.spi in separate class
    to avoid class-loading exceptions.
    
    (cherry picked from commit e213a0215f3579c8e3bd74b90be12b09ac4c3a6e)
---
 .../tobago/internal/util/AuthorizationHelper.java  | 51 ++++----------
 .../internal/util/AuthorizationHelperCdi.java      | 80 ++++++++++++++++++++++
 2 files changed, 95 insertions(+), 36 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelper.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelper.java
index 48dcb6d..07b9f97 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelper.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelper.java
@@ -25,11 +25,8 @@ import org.slf4j.LoggerFactory;
 import javax.annotation.security.DenyAll;
 import javax.annotation.security.PermitAll;
 import javax.annotation.security.RolesAllowed;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
 import javax.faces.bean.ManagedBean;
 import javax.faces.context.FacesContext;
-import javax.naming.InitialContext;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Method;
@@ -50,7 +47,7 @@ public class AuthorizationHelper {
 
   public static final String AUTHORIZATION_HELPER = "authorizationHelper";
 
-  private static final Pattern PATTERN = Pattern.compile("#\\{(\\w+(?:\\.\\w+)*)\\.(\\w+)(?:\\(.*\\))?\\}");
+  private static final Pattern PATTERN = Pattern.compile("#\\{(\\w+(?:\\.\\w+)*)\\.(\\w+)(?:\\(.*\\))?}");
 
   private static final Annotation NULL_VALUE = new Annotation() {
     @Override
@@ -66,26 +63,19 @@ public class AuthorizationHelper {
 
   private final Map<String, Object> cache = new ConcurrentHashMap<>();
 
-  private BeanManager beanManager;
+  private AuthorizationHelperCdi cdi;
 
   public AuthorizationHelper() {
 
     try {
-      // XXX this is easier with CDI 1.1
-      // beanManager = CDI.context().getBeanManager();
-      final InitialContext context = new InitialContext();
-      beanManager = (BeanManager) context.lookup("java:comp/BeanManager");
-    } catch (final Exception exception) {
-      LOG.warn("Can't obtain 'java:comp/BeanManager'");
-    }
-
-    if (beanManager == null) {
-      // this works with Jetty 9
-      beanManager = (BeanManager)
-          FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(BeanManager.class.getName());
+      Class.forName("javax.enterprise.inject.spi.BeanManager");
+      cdi = new AuthorizationHelperCdi();
+      if (!cdi.hasBeanManager()) {
+        cdi = null;
+      }
+    } catch (ClassNotFoundException e) {
+      // no cdi available
     }
-
-    LOG.info("Using bean manager: '{}'", beanManager);
   }
 
   public static AuthorizationHelper getInstance(final FacesContext facesContext) {
@@ -142,17 +132,9 @@ public class AuthorizationHelper {
         final String beanString = matcher.group(1);
         final String methodString = matcher.group(2);
 
-        Object bean = null;
-
-        if (beanManager != null) { // CDI case
-          for (final Bean<?> entry : beanManager.getBeans(beanString)) {
-            if (bean == null) {
-              bean = entry;
-            } else {
-              LOG.warn("Bean name ambiguous: '{}'", beanString);
-            }
-          }
-
+        final Object bean;
+        if (cdi != null) { // CDI case
+          bean = cdi.getObject(beanString);
         } else { // JSF case
           bean = facesContext.getELContext().getELResolver().getValue(facesContext.getELContext(), null, beanString);
         }
@@ -200,16 +182,13 @@ public class AuthorizationHelper {
       return annotation;
     }
     annotation = annotatedElement.getAnnotation(PermitAll.class);
-    if (annotation != null) {
-      return annotation;
-    }
-    return null;
+    return annotation;
   }
 
   private List<Method> findMethods(final Object bean, final String name) {
     final Class clazz;
-    if (bean instanceof Bean) {
-      clazz = ((Bean) bean).getBeanClass();
+    if (cdi != null) {
+      clazz = cdi.getBeanClass(bean);
     } else {
       clazz = bean.getClass();
     }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelperCdi.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelperCdi.java
new file mode 100644
index 0000000..671e4dc
--- /dev/null
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/AuthorizationHelperCdi.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.myfaces.tobago.internal.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.faces.context.FacesContext;
+import javax.naming.InitialContext;
+import java.lang.invoke.MethodHandles;
+
+/**
+ * The code is this class is to help the {@link AuthorizationHelper} in the case of CDI. In case of no CDI, this class
+ * will never loaded to prevent problems the import of class of the package javax.enterprise.inject.spi.
+ */
+class AuthorizationHelperCdi {
+
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private BeanManager beanManager;
+
+  AuthorizationHelperCdi() {
+
+    try {
+      // XXX this is easier with CDI 1.1
+      // beanManager = CDI.context().getBeanManager();
+      final InitialContext context = new InitialContext();
+      beanManager = (BeanManager) context.lookup("java:comp/BeanManager");
+    } catch (final Exception exception) {
+      LOG.warn("Can't obtain 'java:comp/BeanManager'");
+    }
+
+    if (beanManager == null) {
+      // this works with Jetty 9
+      beanManager = (BeanManager)
+          FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(BeanManager.class.getName());
+    }
+
+    LOG.info("Using bean manager: '{}'", beanManager);
+  }
+
+  Object getObject(String beanString) {
+    Object bean = null;
+    for (final Bean<?> entry : beanManager.getBeans(beanString)) {
+      if (bean == null) {
+        bean = entry;
+      } else {
+        LOG.warn("Bean name ambiguous: '{}'", beanString);
+      }
+    }
+    return bean;
+  }
+
+  Class getBeanClass(Object bean) {
+    return ((Bean) bean).getBeanClass();
+  }
+
+  boolean hasBeanManager() {
+    return beanManager != null;
+  }
+}