org.apache.myfaces.extensions.cdi.core.impl.util
Class CodiUtils

java.lang.Object
  extended by org.apache.myfaces.extensions.cdi.core.impl.util.CodiUtils

@Typed
public abstract class CodiUtils
extends Object

This is a collection of a few useful static helper functions.


Method Summary
static
<T> T
createNewInstanceOfBean(javax.enterprise.context.spi.CreationalContext<T> creationalContext, javax.enterprise.inject.spi.Bean<T> bean)
          Creates an instance for the given Bean and CreationalContext
static
<T> T
getContextualReference(javax.enterprise.inject.spi.BeanManager beanManager, Type t, javax.enterprise.inject.spi.Bean<T> bean)
          Creates a scoped instance (a proxy for normal scoped beans) for the given bean-descriptor.
static
<T> T
getContextualReferenceByClass(javax.enterprise.inject.spi.BeanManager beanManager, Class<T> targetClass, Annotation... qualifier)
          Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers
static
<T> T
getContextualReferenceByClass(javax.enterprise.inject.spi.BeanManager beanManager, Class<T> targetClass, boolean optionalBeanAllowed, Annotation... qualifier)
          Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers.
static
<T> T
getContextualReferenceByClass(Class<T> targetClass, Annotation... qualifier)
          Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers
static
<T> T
getContextualReferenceByClass(Class<T> targetClass, boolean optionalBeanAllowed, Annotation... qualifier)
          Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers.
static
<T> T
getContextualReferenceByName(javax.enterprise.inject.spi.BeanManager beanManager, String beanName, Class<T> targetClass)
          Creates a scoped instance (a proxy for normal scoped beans) for the given bean-name and class
static
<T> T
injectFields(T instance)
          Allows to perform dependency injection for instances which aren't managed by CDI
static
<T> T
injectFields(T instance, boolean requiresAdvancedQualifier)
          Allows to perform dependency injection for instances which aren't managed by CDI
static boolean isCdiInitialized()
          Checks if CDI is up and running
static boolean isQualifierEqual(Annotation qualifier1, Annotation qualifier2)
          Checks if the given qualifiers are equal.
static
<T extends Serializable>
T
lookupFromEnvironment(Class<T> targetType, Aggregatable<T> aggregatable, T... defaultImplementation)
          Resolves resources outside of CDI for the given class.
static
<T extends Serializable>
T
lookupFromEnvironment(Class<T> targetType, T... defaultImplementation)
          Resolves resources outside of CDI for the given class.
static
<T extends Serializable>
T
lookupFromEnvironment(String key, Class<T> targetType, Aggregatable<T> aggregatable, T... defaultImplementation)
          Resolves resources outside of CDI for the given key and class.
static
<T extends Serializable>
T
lookupFromEnvironment(String key, Class<T> targetType, T... defaultImplementation)
          Resolves resources outside of CDI for the given key and class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createNewInstanceOfBean

public static <T> T createNewInstanceOfBean(javax.enterprise.context.spi.CreationalContext<T> creationalContext,
                                            javax.enterprise.inject.spi.Bean<T> bean)
Creates an instance for the given Bean and CreationalContext

Type Parameters:
T - current type
Parameters:
creationalContext - current context
bean - current bean
Returns:
created instance

getContextualReferenceByName

public static <T> T getContextualReferenceByName(javax.enterprise.inject.spi.BeanManager beanManager,
                                                 String beanName,
                                                 Class<T> targetClass)
Creates a scoped instance (a proxy for normal scoped beans) for the given bean-name and class

Type Parameters:
T - target type
Parameters:
beanManager - current bean-manager
beanName - name of the bean
targetClass - class of the bean
Returns:
created or resolved instance

getContextualReferenceByClass

public static <T> T getContextualReferenceByClass(Class<T> targetClass,
                                                  Annotation... qualifier)
Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers

Type Parameters:
T - target type
Parameters:
targetClass - class of the bean
qualifier - optional qualifiers
Returns:
created or resolved instance

getContextualReferenceByClass

public static <T> T getContextualReferenceByClass(javax.enterprise.inject.spi.BeanManager beanManager,
                                                  Class<T> targetClass,
                                                  Annotation... qualifier)
Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers

Type Parameters:
T - target type
Parameters:
beanManager - current bean-manager
targetClass - class of the bean
qualifier - optional qualifiers
Returns:
created or resolved instance

getContextualReferenceByClass

public static <T> T getContextualReferenceByClass(Class<T> targetClass,
                                                  boolean optionalBeanAllowed,
                                                  Annotation... qualifier)
Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers. Compared to the other util methods it allows optional beans.

Type Parameters:
T - target type
Parameters:
targetClass - class of the bean
optionalBeanAllowed - flag which indicates if it's an optional bean
qualifier - optional qualifiers
Returns:
created or resolved instance if such a bean exists, null otherwise

getContextualReferenceByClass

public static <T> T getContextualReferenceByClass(javax.enterprise.inject.spi.BeanManager beanManager,
                                                  Class<T> targetClass,
                                                  boolean optionalBeanAllowed,
                                                  Annotation... qualifier)
Creates a scoped instance (a proxy for normal scoped beans) for the given bean-class and qualifiers. Compared to the other util methods it allows optional beans.

Type Parameters:
T - target type
Parameters:
beanManager - current bean-manager
targetClass - class of the bean
optionalBeanAllowed - flag which indicates if it's an optional bean
qualifier - optional qualifiers
Returns:
created or resolved instance if such a bean exists, null otherwise

getContextualReference

public static <T> T getContextualReference(javax.enterprise.inject.spi.BeanManager beanManager,
                                           Type t,
                                           javax.enterprise.inject.spi.Bean<T> bean)
Creates a scoped instance (a proxy for normal scoped beans) for the given bean-descriptor.

Type Parameters:
T - target type
Parameters:
beanManager - current bean-manager
t - type of the bean
bean - bean-descriptor
Returns:
created or resolved instance if such a bean exists, null otherwise

injectFields

public static <T> T injectFields(T instance)
Allows to perform dependency injection for instances which aren't managed by CDI

Type Parameters:
T - current type
Parameters:
instance - current instance
Returns:
instance with injected fields (if possible)

injectFields

public static <T> T injectFields(T instance,
                                 boolean requiresAdvancedQualifier)
Allows to perform dependency injection for instances which aren't managed by CDI

Type Parameters:
T - current type
Parameters:
instance - current instance
requiresAdvancedQualifier - flag which indicates if an instance has to be annotated with Advanced to be eligible for dependency injection.
Returns:
instance with injected fields (if possible)

isQualifierEqual

public static boolean isQualifierEqual(Annotation qualifier1,
                                       Annotation qualifier2)
Checks if the given qualifiers are equal. Qualifiers are equal if they have the same annotationType and all their methods, except those annotated with @Nonbinding, return the same value.

Parameters:
qualifier1 - first qualifier
qualifier2 - second qualifier
Returns:
true if both qualifiers are equal, false otherwise

lookupFromEnvironment

public static <T extends Serializable> T lookupFromEnvironment(Class<T> targetType,
                                                               T... defaultImplementation)
Resolves resources outside of CDI for the given class.

Type Parameters:
T - current type
Parameters:
targetType - target type
defaultImplementation - default implementation
Returns:
configured artifact or null if there is no result

lookupFromEnvironment

public static <T extends Serializable> T lookupFromEnvironment(Class<T> targetType,
                                                               Aggregatable<T> aggregatable,
                                                               T... defaultImplementation)
Resolves resources outside of CDI for the given class.

Type Parameters:
T - current type
Parameters:
targetType - target type which is also used as key (the simple name of it)
aggregatable - allows to aggregate multiple results
defaultImplementation - default implementation
Returns:
configured artifact or an aggregated instance if there are multiple results or null if there is no result

lookupFromEnvironment

public static <T extends Serializable> T lookupFromEnvironment(String key,
                                                               Class<T> targetType,
                                                               T... defaultImplementation)
Resolves resources outside of CDI for the given key and class.

Type Parameters:
T - current type
Parameters:
key - key for identifying the resource which has to be resolved
targetType - target type
defaultImplementation - default implementation
Returns:
configured artifact or null if there is no result

lookupFromEnvironment

public static <T extends Serializable> T lookupFromEnvironment(String key,
                                                               Class<T> targetType,
                                                               Aggregatable<T> aggregatable,
                                                               T... defaultImplementation)
Resolves resources outside of CDI for the given key and class.

Type Parameters:
T - current type
Parameters:
key - key for identifying the resource which has to be resolved
targetType - target type
aggregatable - allows to aggregate multiple results
defaultImplementation - default implementation
Returns:
configured artifact or an aggregated instance if there are multiple results or null if there is no result

isCdiInitialized

public static boolean isCdiInitialized()
Checks if CDI is up and running

Returns:
true if CDI was bootstrapped, false otherwise


Copyright © 2010-2011 The Apache Software Foundation. All Rights Reserved.