openwebbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Felipe Pina <fop....@gmail.com>
Subject Re: Lookup bean using beanManager returning null
Date Thu, 12 Jun 2014 18:47:55 GMT
Thank you Mark for attention, unfortunately do not have my code online.

But i think that i put all necessary informations to your diagnostic,
including my dependencies .

i hope it is clear, and I am available to answer any questions you may have

Regards
*Felipe*

*----------------------------------------------------------------------------------------------------------------------------------
package*

*WAR*
*  |- WebProject*
*            |- com.company.bean.JSFBean.PedidosMBean*
*  |-  BusinessProject*
*             |- com.company.bean.Permissao*
*             |- com.company.factory.BeanFactory*
*             |- com.company.command.Command*
*----------------------------------------------------------------------------------------------------------------------------------
JSF bean*













*/* CDI Scope & bean
*/@javax.inject.Named("pedidoMBean")@javax.enterprise.context.SessionScoped
public class PedidoMBean  {
 ....}----------------------------------------------------------------------------------------------------------------------------------Binding
@Qualifier @Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface BusinessBean
{}----------------------------------------------------------------------------------------------------------------------------------
BeanFactory *
*public class BeanFactory { static BeanManager getBeanManager() {
 InitialContext context;    Object result;    try {        context = new
InitialContext();        result =
context.lookup("java:comp/env/BeanManager"); //lookup in Tomcat    } catch
(NamingException e) {        try {            context = new
InitialContext();            result =
context.lookup("java:comp/BeanManager"); //lookup in JBossAS        } catch
(NamingException ex) {            throw new RuntimeException("BeanManager
could not be found in JNDI", e);        }    }    return (BeanManager)
result; } @SuppressWarnings("unchecked") public static <T> T
getContextualInstance(final Class<T> type) { BeanManager manager =
getBeanManager();    T result = null;        @SuppressWarnings("serial")
Annotation[] annotations = { new AnnotationLiteral<BusinessBean>() {} };
       Set<Bean<?>> beans = manager.getBeans(type, annotations); beans =
manager.getBeans(type);   -> isEmty beans = manager.getBeans("teste"); ->
isEmty*
* Bean<T> bean = (Bean<T>) manager.resolve(beans); -> isEmty *

*    if (bean != null) {        CreationalContext<T> context =
manager.createCreationalContext(bean);        if (context != null) {
     result = (T) manager.getReference(bean, type, context);        }    }
   return result; }}
----------------------------------------------------------------------------------------------------------------------------------
Business Bean*

*@javax.enterprise.context.RequestScoped
@BusinessBean@Named(value="teste")public class Permissao extends
NegocioBase<PermissaoDTO>  { ....      public Permissao() {} *


*}----------------------------------------------------------------------------------------------------------------------------------
  Caller Class *
*public class Command {*

*  public void invoke() {*
       Permissao bo = BeanFactory.getContextualInstance(Permissao.class) ;
       bo -> is null
*  }*
*}*



*----------------------------------------------------------------------------------------------------------------------------------
*
*Master **pom.xml*



*<project xmlns="http://maven.apache.org/POM/4.0.0
<http://maven.apache.org/POM/4.0.0>"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance>"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
<http://maven.apache.org/POM/4.0.0>
http://maven.apache.org/xsd/maven-4.0.0.xsd
<http://maven.apache.org/xsd/maven-4.0.0.xsd>">
<modelVersion>4.0.0</modelVersion>  <groupId>com.company</groupId>
<artifactId>builder</artifactId>   <version>0.2.1-SNAPSHOT</version>
<name>Master</name>  <packaging>pom</packaging>    <modules>
<module>../BusinessProject</module>    <module>../WebProject</module>
</modules> *

*  <properties>    <openwebbeans.version>1.2.1</openwebbeans.version>
</properties>*

<!--  **************************************************** OpenWebBeans
Specifications -->
        <!-- JSR-330 -->
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-atinject_1.0_spec</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
         <!-- JSR-299  -->
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jcdi_1.0_spec</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-interceptor_1.1_spec</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-impl</artifactId>
            <version>${openwebbeans.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-web</artifactId>
            <version>${openwebbeans.version}</version>
            <scope>runtime</scope>
        </dependency>

         <!-- SPI (Service Provider Interface). -->
     <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-spi</artifactId>
            <version>${openwebbeans.version}</version>
            <scope>runtime</scope>
        </dependency>

*        <!-- XBean Finder helps to find annotations in classes *
              *if you uncomment this dependence, the following error has
fires:*


*Jun 12, 2014 3:40:46 PM org.apache.catalina.core.StandardContext
listenerStartSEVERE: Exception sending context initialized event to
listener instance of class com.sun.faces.config.ConfigureListener
java.lang.RuntimeException:
javax.enterprise.context.ContextNotActiveException: WebBeans context with
scope type annotation @FlowDefinition does not exist within current thread
at
com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:273)
at
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
*
<dependency>
<groupId>org.apache.xbean</groupId>
 <artifactId>xbean-finder</artifactId>
<version>3.13</version>
 </dependency>
*--> *
 <!--  -->
 <dependency>
<groupId>org.apache.geronimo.bundles</groupId>
 <artifactId>scannotation</artifactId>
<version>1.0.2_1</version>
 </dependency>

*----------------------------------------------------------------------------------------------------------------------------------
web Project*

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
<groupId>*com.company*</groupId>
  <artifactId>builder</artifactId>
  <version>0.2.1-SNAPSHOT</version>
  <relativePath>../Master/pom.xml</relativePath>
  </parent>
  <packaging>war</packaging>
  <artifactId>WebProject</artifactId>
  <name>*WebProject*</name>

<dependencies>

<dependency>
<groupId>*com.company*</groupId>
 <artifactId>BusinessProject</artifactId>
<version>${project.version}</version>
 </dependency>

*</dependencies>*
*----------------------------------------------------------------------------------------------------------------------------------
Business Project*

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <parent>
<groupId>*com.company*</groupId>
  <artifactId>builder</artifactId>
  <version>0.2.1-SNAPSHOT</version>
  <relativePath>../Master/pom.xml</relativePath>
  </parent>
  <packaging>jar</packaging>
  <artifactId>*BusinessProject*</artifactId>
  <name>BusinessProject</name>



2014-06-12 14:52 GMT-03:00 Mark Struberg <struberg@yahoo.de>:

> Hi Felipe!
>
> Let me first ask you what you intend to do. I like to learn about your use
> case and then we should be able to help you.
>
> Your sample shows a class (Permissao) which has no qualifier annotations
> at all.
>
> But your sample also shows a Qualifier @BusinessBean. Where do you use it?
> Do you get a BeanManager at all?
>
> Do you have your sample online somewhere? Maybe on github?
> That would really make it easier for us to help you.
>
> LIeGrue,
> strub
>
>
>
>   On Thursday, 12 June 2014, 19:11, Felipe Pina <fop.net@gmail.com> wrote:
>
>
>
> I haved changed my method according to your reference
> http://deltaspike.apache.org/core.html#beanprovider
>
> but still no success  :(
>
> @SuppressWarnings("unchecked")
> public static <T> T getContextualInstance(final Class<T> type) {
>  BeanManager manager = getBeanManager();
>     T result = null;
>
>     @SuppressWarnings("serial")
>  Annotation[] annotations = { new AnnotationLiteral<BusinessBean>() {} };
>
> Set<Bean<?>> beans = manager.getBeans(type, annotations); -> empty
>  beans = manager.getBeans(type);  -> empty
> beans = manager.getBeans("teste"); -> empty
>  Bean<T> bean = (Bean<T>) manager.resolve(beans);
>     if (bean != null) {
>         CreationalContext<T> context =
> manager.createCreationalContext(bean);
>         if (context != null) {
>             result = (T) manager.getReference(bean, type, context);
>         }
>     }
>     return result;
> }
>
> *My dependencies *
>
>  <properties>
>   <openwebbeans.version>1.2.1</openwebbeans.version>
>  </properties>
>
>     <dependency>
>             <groupId>org.apache.geronimo.specs</groupId>
>             <artifactId>geronimo-atinject_1.0_spec</artifactId>
>             <version>1.0</version>
>             <scope>compile</scope>
>         </dependency>
>          <!-- JSR-299  -->
>         <dependency>
>             <groupId>org.apache.geronimo.specs</groupId>
>             <artifactId>geronimo-jcdi_1.0_spec</artifactId>
>             <version>1.0</version>
>             <scope>compile</scope>
>         </dependency>
>
>         <dependency>
>             <groupId>org.apache.geronimo.specs</groupId>
>             <artifactId>geronimo-interceptor_1.1_spec</artifactId>
>              <version>1.0</version>
>             <scope>compile</scope>
>         </dependency>
>
>         <dependency>
>             <groupId>org.apache.openwebbeans</groupId>
>             <artifactId>openwebbeans-impl</artifactId>
>             <version>${openwebbeans.version}</version>
>             <scope>runtime</scope>
>         </dependency>
>
>         <dependency>
>             <groupId>org.apache.openwebbeans</groupId>
>             <artifactId>openwebbeans-web</artifactId>
>             <version>${openwebbeans.version}</version>
>             <scope>runtime</scope>
>         </dependency>
>
>          <!-- SPI (Service Provider Interface). -->
>      <dependency>
>             <groupId>org.apache.openwebbeans</groupId>
>             <artifactId>openwebbeans-spi</artifactId>
>             <version>${openwebbeans.version}</version>
>             <scope>runtime</scope>
>         </dependency>
>
>
>
>
> 2014-06-12 10:03 GMT-03:00 Thomas Andraschko <andraschko.thomas@gmail.com>
> :
>
> Maybe you miss the beans.xml in your jar but the way how you call
> #getBeans with an qualifier is wrong.
>
>
> 2014-06-12 14:55 GMT+02:00 Felipe Pina <fop.net@gmail.com>:
>
> Take a look to my tests. The focus is not how am i passing as parameter
> annotation, because the lookup by name works to bean inside WebProject but
> not works to bean inside BusinessProject
>
> I have tested using three ways of lookup the bean, and both of bean are
> annotated similarly, but the webproject bean works and the BussinessProject
> bean do not works (jar package).
>
>  Set<Bean<?>> beans = manager.getBeans(type,
> type.getAnnotation(BusinessBean.class));
> Set<Bean<?>> beans = manager.getBeans(type);
>  Set<Bean<?>> beans = manager.getBeans("teste");
>
>
>
>
> 2014-06-12 9:34 GMT-03:00 Thomas Andraschko <andraschko.thomas@gmail.com>:
>
>  You need to a pass a AnnotationLiteral instance instead of
> "type.getAnnotation(yourAnnotation.class)"
> Here it's explained how to create one and use one:
> http://deltaspike.apache.org/core.html#beanprovider
>
>
>
> 2014-06-12 14:26 GMT+02:00 Gerhard Petracek <gerhard.petracek@gmail.com>:
>
> @felipe:
> we won't respond something different (see [1]), if you keep the wrong code.
> the one case you mentioned just works, because #getAnnotation returns null.
>
> regards,
> gerhard
>
> [1] http://s.apache.org/oDb
>
>
>
> 2014-06-12 14:16 GMT+02:00 Thomas Andraschko <andraschko.thomas@gmail.com>
> :
>
> Have a look at the bean manager java doc ->
>
> *getBeans
> <http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html#getBeans%28java.lang.reflect.Type,%20java.lang.annotation.Annotation...%29>*(java.lang.reflect.Type
beanType,
> java.lang.annotation.Annotation... qualifiers)
>
> RequestScoped isn't a CDI qualifier! ;)
>
>
> 2014-06-12 14:03 GMT+02:00 Felipe Pina <fop.net@gmail.com>:
>
> Questions
>
>
>    1. Is there any schema that uses in the beans.xml file that configures
>    the engine to evaluate candidates beans through a package path?
>    2. the fact that the bean be packaged inside a jar which in turn,
>    within a war, is impediment to the proper functioning of the injection of
>    bean?
>
>
>
> *Tests*
>
>    1. lookup bean from string value
>
>
>    1. lookup "teste" -> works fine
>    2. lookup "perm" -> *fail returning null*
>
>
>    - lookup bean from type
>
>
>    1. lookup using Permissao.class -> * fail returning null*
>
>
>
>  *Project Structure*
>
> *WAR*
> *  |- WebProject*
> *            |- com.company.bean.JSFBean  -> **@Named(value="teste")*
> *  |-  BusinessProject*
> *             |- com.company.bean.Permissao -> *
> *@Named(value="perm") @BusinessBean*
>
> *My Qualifier*
>
>  @Qualifier
> @Retention(RUNTIME)
> @Target({TYPE, METHOD, FIELD, PARAMETER})
> public @interface BusinessBean {
>
> }
>
> *My bean.*
>
> @javax.enterprise.context.RequestScoped
> @javax.enterprise.inject.Default
> public class Permissao extends NegocioBase<PermissaoDTO> {
> ....
> }
>
>
> *context.xml*
>  <Resource name="BeanManager"
>         auth="Container"
>         type="javax.enterprise.inject.spi.BeanManager"
>         factory="org.apache.webbeans.container.ManagerObjectFactory"/>
>
>
> *web.xml*
>
> <!-- I ran with and without this resource-env-ref section -->
>   <resource-env-ref>
>     <description>Object factory for the CDI Bean Manager</description>
>     <resource-env-ref-name>BeanManager</resource-env-ref-name>
>
> <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
>   </resource-env-ref>
>
> *BeanFactory*
>
> public class BeanFactory {
>  static BeanManager getBeanManager() {
>      InitialContext context;
>     Object result;
>     try {
>          context = new InitialContext();
>         result = context.lookup("java:comp/env/BeanManager"); //lookup in
> Tomcat
>      } catch (NamingException e) {
>         try {
>             context = new InitialContext();
>              result = context.lookup("java:comp/BeanManager"); //lookup
> in JBossAS
>         } catch (NamingException ex) {
>              throw new RuntimeException("BeanManager could not be found
> in JNDI", e);
>         }
>     }
>      return (BeanManager) result;
> }
>  @SuppressWarnings("unchecked")
>  public static <T> T getContextualInstance(final Class<T> type) {
> BeanManager manager = getBeanManager();
>      T result = null;
> *Set<Bean<?>> beans = manager.getBeans(type,
> type.getAnnotation(RequestScoped.class));*
> *---- >  why variable beans isEmpty !!!*
> Bean<T> bean = (Bean<T>) manager.resolve(beans);
>     if (bean != null) {
>          CreationalContext<T> context =
> manager.createCreationalContext(bean);
>         if (context != null) {
>              result = (T) manager.getReference(bean, type, context);
>         }
>     }
>      return result;
> }
> }
>
> --
> Atenciosamente,
> Felipe Pina
>
>
>
>
>
>
>
> --
> Atenciosamente,
> Felipe Pina
>
>
>
>
>
> --
> Atenciosamente,
> Felipe Pina
>
>
>


-- 
Atenciosamente,
Felipe Pina

Mime
View raw message