All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.util.BeansLocal Maven / Gradle / Ivy

There is a newer version: 4.5.1
Show newest version
/*
 * Copyright OmniFaces
 *
 * Licensed 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
 *
 *     https://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.omnifaces.util;

import static java.util.logging.Level.FINE;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.logging.Logger;

import javax.enterprise.context.spi.AlterableContext;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;

import org.omnifaces.cdi.beans.InjectionPointGenerator;

/**
 * 

* Collection of utility methods for the CDI API that are mainly shortcuts for obtaining stuff from the * {@link BeanManager}. *

* The difference with {@link Beans} is that no one method of {@link BeansLocal} obtains the {@link BeanManager} from * JNDI. This job is up to the caller. This is more efficient in situations where multiple utility methods needs to be * called at the same time. * * @author Bauke Scholtz * @since 2.0 */ @Typed public final class BeansLocal { // Constants ------------------------------------------------------------------------------------------------------ private static final Logger logger = Logger.getLogger(BeansLocal.class.getName()); // Constructors --------------------------------------------------------------------------------------------------- private BeansLocal() { // Hide constructor. } // Utility -------------------------------------------------------------------------------------------------------- /** * @see Beans#resolve(Class, Annotation...) */ @SuppressWarnings("unchecked") public static Bean resolve(BeanManager beanManager, Class beanClass, Annotation... qualifiers) { Set> beans = beanManager.getBeans(beanClass, qualifiers); for (Bean bean : beans) { if (bean.getBeanClass() == beanClass) { return (Bean) beanManager.resolve(Collections.>singleton(bean)); } } return (Bean) beanManager.resolve(beans); } /** * @see Beans#getReference(Class, Annotation...) */ public static T getReference(BeanManager beanManager, Class beanClass, Annotation... qualifiers) { Bean bean = resolve(beanManager, beanClass, qualifiers); return (bean != null) ? getReference(beanManager, bean, beanClass) : null; } /** * @see Beans#getReference(Bean) */ public static T getReference(BeanManager beanManager, Bean bean) { return getReference(beanManager, bean, bean.getBeanClass()); } @SuppressWarnings("unchecked") private static T getReference(BeanManager beanManager, Bean bean, Class beanClass) { return (T) beanManager.getReference(bean, beanClass, beanManager.createCreationalContext(bean)); } /** * @see Beans#getInstance(Class, Annotation...) */ public static T getInstance(BeanManager beanManager, Class beanClass, Annotation... qualifiers) { return getInstance(beanManager, beanClass, true, qualifiers); } /** * @see Beans#getInstance(Class, boolean, Annotation...) */ public static T getInstance(BeanManager beanManager, Class beanClass, boolean create, Annotation... qualifiers) { Bean bean = resolve(beanManager, beanClass, qualifiers); return (bean != null) ? getInstance(beanManager, bean, create) : null; } /** * @see Beans#getInstance(Bean, boolean) */ public static T getInstance(BeanManager beanManager, Bean bean, boolean create) { Context context = beanManager.getContext(bean.getScope()); if (create) { return context.get(bean, beanManager.createCreationalContext(bean)); } else { return context.get(bean); } } /** * @see Beans#isActive(Class) */ public static boolean isActive(BeanManager beanManager, Class scope) { try { return beanManager.getContext(scope).isActive(); } catch (Exception ignore) { logger.log(FINE, "Ignoring thrown exception; given scope is very unlikely active anyway.", ignore); return false; } } /** * @see Beans#getActiveInstances(Class) */ public static Map getActiveInstances(BeanManager beanManager, Class scope) { Map activeInstances = new HashMap<>(); Set> beans = beanManager.getBeans(Object.class); Context context = beanManager.getContext(scope); for (Bean bean : beans) { Object instance = context.get(bean); if (instance != null) { activeInstances.put(instance, bean.getName()); } } return Collections.unmodifiableMap(activeInstances); } /** * @see Beans#destroy(Class, Annotation...) */ public static void destroy(BeanManager beanManager, Class beanClass, Annotation... qualifiers) { Bean bean = resolve(beanManager, beanClass, qualifiers); if (bean != null) { destroy(beanManager, bean); } } /** * @see Beans#destroy(Bean) */ public static void destroy(BeanManager beanManager, Bean bean) { Context context = beanManager.getContext(bean.getScope()); if (context instanceof AlterableContext) { ((AlterableContext) context).destroy(bean); } else { T instance = context.get(bean); if (instance != null) { destroy(beanManager, bean, instance); } } } /** * @see Beans#destroy(Object) */ @SuppressWarnings("unchecked") public static void destroy(BeanManager beanManager, T instance) { if (instance instanceof Class) { // Java prefers T over Class when varargs is not specified :( destroy(beanManager, (Class) instance, new Annotation[0]); } else { for (Class beanClass = instance.getClass(); beanClass != Object.class; beanClass = beanClass.getSuperclass()) { Bean bean = (Bean) resolve(beanManager, beanClass); if (bean != null) { destroy(beanManager, bean, instance); return; } } } } private static void destroy(BeanManager beanManager, Bean bean, T instance) { bean.destroy(instance, beanManager.createCreationalContext(bean)); } /** * @see Beans#getAnnotation(Annotated, Class) */ public static A getAnnotation(BeanManager beanManager, Annotated annotated, Class annotationType) { annotated.getAnnotation(annotationType); if (annotated.getAnnotations().isEmpty()) { return null; } if (annotated.isAnnotationPresent(annotationType)) { return annotated.getAnnotation(annotationType); } Queue annotations = new LinkedList<>(annotated.getAnnotations()); while (!annotations.isEmpty()) { Annotation annotation = annotations.remove(); if (annotation.annotationType().equals(annotationType)) { return annotationType.cast(annotation); } if (beanManager.isStereotype(annotation.annotationType())) { annotations.addAll(beanManager.getStereotypeDefinition(annotation.annotationType())); } } return null; } /** * @see Beans#getAnnotation(Annotated, Class) */ public static InjectionPoint getCurrentInjectionPoint(BeanManager beanManager, CreationalContext creationalContext) { Bean bean = resolve(beanManager, InjectionPointGenerator.class); return (bean != null) ? (InjectionPoint) beanManager.getInjectableReference(bean.getInjectionPoints().iterator().next(), creationalContext) : null; } /** * @see Beans#fireEvent(Object, Annotation...) */ public static void fireEvent(BeanManager beanManager, Object event, Annotation... qualifiers) { beanManager.fireEvent(event, qualifiers); } }