org.omnifaces.util.BeansLocal Maven / Gradle / Ivy
/*
* 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