myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lof...@apache.org
Subject [myfaces-tobago] branch master updated: TOBAGO-1935: Tobago should be runnable without CDI
Date Wed, 13 Nov 2019 13:35:39 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new e213a02  TOBAGO-1935: Tobago should be runnable without CDI
e213a02 is described below

commit e213a0215f3579c8e3bd74b90be12b09ac4c3a6e
Author: Udo Schnurpfeil <udo.schnurpfeil@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.
---
 .../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 d6051bf..6a40117 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.invoke.MethodHandles;
 import java.lang.reflect.AnnotatedElement;
@@ -51,7 +48,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
@@ -67,26 +64,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) {
@@ -143,17 +133,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);
         }
@@ -201,16 +183,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;
+  }
+}


Mime
View raw message