org.omnifaces.util.Beans Maven / Gradle / Ivy
/*
* Copyright 2013 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
*
* 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.omnifaces.util;
import java.lang.annotation.Annotation;
import java.util.Map;
import javax.enterprise.context.spi.AlterableContext;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Stereotype;
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;
/**
*
* Collection of utility methods for the CDI API that are mainly shortcuts for obtaining stuff from the
* {@link BeanManager}.
*
*
Usage
*
* Some examples:
*
* // Get the CDI managed bean reference (proxy) of the given bean class.
* SomeBean someBean = Beans.getReference(SomeBean.class);
*
* // Get the CDI managed bean instance (actual) of the given bean class.
* SomeBean someBean = Beans.getInstance(SomeBean.class);
*
* // Get all currently active CDI managed bean instances in the session scope.
* Map<Object, String> activeSessionScopedBeans = Beans.getActiveInstances(SessionScope.class);
*
* // Destroy any currently active CDI managed bean instance of given bean class.
* Beans.destroy(SomeBean.class);
*
*
* The "native" CDI way would otherwise look like this:
*
* // Get the CDI managed bean reference (proxy) of the given bean class.
* Set<Bean<?>> beans = beanManager.getBeans(SomeBean.class);
* Bean<SomeBean> bean = (Bean<SomeBean>) beanManager.resolve(beans);
* CreationalContext<SomeBean> context = beanManager.createCreationalContext(bean);
* SomeBean someBean = (SomeBean) beanManager.getReference(bean, SomeBean.class, context);
*
*
* If you need a dependency-free way of obtaining the CDI managed bean reference (e.g. when you want to write code which
* should also run on Tomcat), use {@link org.omnifaces.config.BeanManager} enum instead.
*
* @author Bauke Scholtz
* @since 1.6.1
* @see BeansLocal
*/
@Typed
public final class Beans {
// Constructors ---------------------------------------------------------------------------------------------------
private Beans() {
// Hide constructor.
}
// Utility --------------------------------------------------------------------------------------------------------
/**
* Returns the CDI bean manager.
* @return The CDI bean manager.
* @since 2.0
* @see org.omnifaces.config.BeanManager#get()
*/
public static BeanManager getManager() {
return org.omnifaces.config.BeanManager.INSTANCE.get();
}
/**
* Returns the CDI managed bean representation of the given bean class.
* @param The generic CDI managed bean type.
* @param beanClass The CDI managed bean class.
* @return The CDI managed bean representation of the given bean class, or null
if there is none.
* @see BeanManager#getBeans(String)
* @see BeanManager#resolve(java.util.Set)
*/
public static Bean resolve(Class beanClass) {
return BeansLocal.resolve(getManager(), beanClass);
}
/**
* Returns the CDI managed bean reference (proxy) of the given bean class.
* Note that this actually returns a client proxy and the underlying actual instance is thus always auto-created.
* @param The expected return type.
* @param beanClass The CDI managed bean class.
* @return The CDI managed bean reference (proxy) of the given class, or null
if there is none.
* @see #resolve(Class)
* @see #getReference(Bean)
*/
public static T getReference(Class beanClass) {
return BeansLocal.getReference(getManager(), beanClass);
}
/**
* Returns the CDI managed bean reference (proxy) of the given bean representation.
* Note that this actually returns a client proxy and the underlying actual instance is thus always auto-created.
* @param The expected return type.
* @param bean The CDI managed bean representation.
* @return The CDI managed bean reference (proxy) of the given bean, or null
if there is none.
* @see BeanManager#createCreationalContext(javax.enterprise.context.spi.Contextual)
* @see BeanManager#getReference(Bean, java.lang.reflect.Type, javax.enterprise.context.spi.CreationalContext)
*/
public static T getReference(Bean bean) {
return BeansLocal.getReference(getManager(), bean);
}
/**
* Returns the CDI managed bean instance (actual) of the given bean class and creates one if one doesn't exist.
* @param The expected return type.
* @param beanClass The CDI managed bean class.
* @return The CDI managed bean instance (actual) of the given bean class, or null
if there is none.
* @since 1.8
* @see #getInstance(Class, boolean)
*/
public static T getInstance(Class beanClass) {
return BeansLocal.getInstance(getManager(), beanClass);
}
/**
* Returns the CDI managed bean instance (actual) of the given bean class and creates one if one doesn't exist and
* create
argument is true
, otherwise don't create one and return null
if
* there's no current instance.
* @param The expected return type.
* @param beanClass The CDI managed bean class.
* @param create Whether to create create CDI managed bean instance if one doesn't exist.
* @return The CDI managed bean instance (actual) of the given bean class, or null
if there is none
* and/or the create
argument is false
.
* @since 1.7
* @see #resolve(Class)
* @see #getInstance(Bean, boolean)
*/
public static T getInstance(Class beanClass, boolean create) {
return BeansLocal.getInstance(getManager(), beanClass, create);
}
/**
* Returns the CDI managed bean instance (actual) of the given bean representation and creates one if one doesn't
* exist and create
argument is true
, otherwise don't create one and return
* null
if there's no current instance.
* @param The expected return type.
* @param bean The CDI managed bean representation.
* @param create Whether to create create CDI managed bean instance if one doesn't exist.
* @return The CDI managed bean instance (actual) of the given bean, or null
if there is none and/or
* the create
argument is false
.
* @since 1.7
* @see BeanManager#getContext(Class)
* @see BeanManager#createCreationalContext(javax.enterprise.context.spi.Contextual)
* @see Context#get(javax.enterprise.context.spi.Contextual, javax.enterprise.context.spi.CreationalContext)
*/
public static T getInstance(Bean bean, boolean create) {
return BeansLocal.getInstance(getManager(), bean, create);
}
/**
* Returns all active CDI managed bean instances in the given CDI managed bean scope. The map key represents
* the active CDI managed bean instance and the map value represents the CDI managed bean name, if any.
* @param The generic CDI managed bean scope type.
* @param scope The CDI managed bean scope, e.g. RequestScoped.class
.
* @return All active CDI managed bean instances in the given CDI managed bean scope.
* @since 1.7
* @see BeanManager#getBeans(String)
* @see BeanManager#getContext(Class)
* @see Context#get(javax.enterprise.context.spi.Contextual)
*/
public static Map