Class CdiObjectFactory

java.lang.Object
org.apache.struts2.ObjectFactory
org.apache.struts2.cdi.CdiObjectFactory
All Implemented Interfaces:
Serializable

public class CdiObjectFactory extends ObjectFactory
CdiObjectFactory allows Struts 2 managed objects, like Actions, Interceptors or Results, to be injected by a Contexts and Dependency Injection container (JSR299 / WebBeans). The BeanManager instance will be searched in the container's JNDI context, according to following algorithm:
See Also:
  • Field Details

    • CDI_JNDIKEY_BEANMANAGER_COMP

      public static final String CDI_JNDIKEY_BEANMANAGER_COMP
      The key under which the BeanManager can be found according to CDI API docs
      See Also:
    • CDI_JNDIKEY_BEANMANAGER_APP

      public static final String CDI_JNDIKEY_BEANMANAGER_APP
      The key under which the BeanManager can be found according to JBoss Weld docs
      See Also:
    • CDI_JNDIKEY_BEANMANAGER_COMP_ENV

      public static final String CDI_JNDIKEY_BEANMANAGER_COMP_ENV
      The key under which the BeanManager can be found in pure Servlet containers according to JBoss Weld docs.
      See Also:
    • STRUTS_OBJECT_FACTORY_CDI_JNDI_KEY

      public static final String STRUTS_OBJECT_FACTORY_CDI_JNDI_KEY
      See Also:
    • beanManager

      protected jakarta.enterprise.inject.spi.BeanManager beanManager
  • Constructor Details

    • CdiObjectFactory

      public CdiObjectFactory()
  • Method Details

    • setJndiKey

      public void setJndiKey(String jndiKey)
    • findBeanManager

      protected jakarta.enterprise.inject.spi.BeanManager findBeanManager()
      Try to find the CDI BeanManager from JNDI context. First, if provided, the key given by struts.objectFactory.cdi.jndiKey will be checked. Then, if nothing was found or no explicit configuration was given, the key CDI_JNDIKEY_BEANMANAGER_COMP will be tested. If nothing is found there, the key CDI_JNDIKEY_BEANMANAGER_APP will be checked. If still nothing is found there, the key CDI_JNDIKEY_BEANMANAGER_COMP_ENV will be checked.
      Returns:
      the BeanManager, if found. null otherwise.
    • lookup

      protected jakarta.enterprise.inject.spi.BeanManager lookup(Context context, String jndiKeyToCheck)
      Lookup the given JNDI key in the given context.
      Parameters:
      context - the context to use for lookup.
      jndiKeyToCheck - the key to lookup.
      Returns:
      the BeanManager, if found; null if not found or NamingException was thrown.
    • buildBean

      public Object buildBean(String className, Map<String,Object> extraContext, boolean injectInternal) throws Exception
      Overrides:
      buildBean in class ObjectFactory
      Throws:
      Exception
    • getInjectionTarget

      protected jakarta.enterprise.inject.spi.InjectionTarget<?> getInjectionTarget(Class<?> clazz)
      Get a InjectionTarget instance for a given class. If the appropriate target is not found in cache, a nw instance will be created.
      Parameters:
      clazz - The class to get a InjectionTarget instance for.
      Returns:
      if found in cache, an existing instance. A new instance otherwise.
    • buildNonContextualCreationalContext

      protected jakarta.enterprise.context.spi.CreationalContext buildNonContextualCreationalContext(jakarta.enterprise.inject.spi.BeanManager beanManager)
      Simple wrapper for CreationalContext creation.
      Parameters:
      beanManager - the BeanManager to use for creating the context.
      Returns:
      the context to use, if given BeanManager was not null. null otherwise.
    • isNoArgConstructorRequired

      public boolean isNoArgConstructorRequired()
      Allow constructor injection
      Overrides:
      isNoArgConstructorRequired in class ObjectFactory