11 Replies Latest reply: Apr 21, 2010 2:24 AM by nklashok namakkal RSS

Seam injection works for POJO, fails when made Stateless

Ryan Nideffer Newbie

In my Seam project, I have an auth component named authenticator




@Name("authenticator")
public class DbAuthenticator implements Authenticator {
    
    @Logger
    private org.jboss.seam.log.Log log;
    @In
    private EntityManager entityManager;
    @In
    private Credentials credentials;
    @In
    private Identity identity;
    @SuppressWarnings("unchecked")
    public boolean authenticate() { ... }
}



This works just fine as a POJO seam component. However, following the booking example and also JEE best practices, I make this a Stateless Session Bean by adding the @Stateless annotation:


@Stateless
@Name("authenticator")
public class DbAuthenticator implements Authenticator {
    ...
}



JBoss shows this bean being registered in JNDI, and Seam shows the component being a STATELESSSESSIONBEAN with the proper JNDI name. As soon as the authenticate() method runs though, none of the private member variables are injected!!! I get NullPointerExceptions when accessing them, and attaching a debugger shows that they are all null.


Why would adding the @Stateless annotation cause this to happen?


Thank you.


  • 1. Re: Seam injection works for POJO, fails when made Stateless
    Arbi Sookazian Master

    You can try doing @In(create=true, required=false) but I'm not sure why this works for JavaBean but not for SLSB...


    I'm assuming you're using the Seam component as a backing bean (hence the @Name), so Seam should auto-instantiate the component instance when it is referenced in the JSF EL...

  • 2. Re: Seam injection works for POJO, fails when made Stateless
    Ryan Nideffer Newbie

    Arbi Sookazian wrote on Mar 09, 2010 00:06:


    You can try doing @In(create=true, required=false) but I'm not sure why this works for JavaBean but not for SLSB...

    I'm assuming you're using the Seam component as a backing bean (hence the @Name), so Seam should auto-instantiate the component instance when it is referenced in the JSF EL...


    Thanks for your reply. I tried changing these to @In(create=true) and still have the same problem, they are not being injected. However, changing @In EntityManager entityManager to @PersistenceContext EntityManager entityManager then caused it to be injected, where it wasnt before.


    Still confused....

  • 3. Re: Seam injection works for POJO, fails when made Stateless
    Arbi Sookazian Master

    show your components.xml.


    I am looking for the SMPC config in particular:


    <persistence:managed-persistence-context auto-create="true" name="entityManager">
            <persistence:persistence-unit-jndi-name>java:/blogEntityManagerFactory</persistence:persistence-unit-jndi-name>
        </persistence:managed-persistence-context>



    Did you seam-gen this app?

  • 4. Re: Seam injection works for POJO, fails when made Stateless
    Ryan Nideffer Newbie
    <?xml version="1.0" encoding="UTF-8"?>
    <components xmlns="http://jboss.com/products/seam/components"
                     ...
                     http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
    
        <core:init debug="false" jndi-pattern="kiosk-votingengine/#{ejbName}/local"/>
    
        <core:manager concurrent-request-timeout="500"
                     conversation-timeout="120000"
                     conversation-id-parameter="cid"
                     parent-conversation-id-parameter="pid"/>
    
        <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
        <web:hot-deploy-filter url-pattern="*.ec"/>
    
        <persistence:managed-persistence-context name="entityManager" auto-create="true"
                           persistence-unit-jndi-name="java:/kveEntityManagerFactory"/>
    
        <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
    
        <security:jpa-permission-store user-permission-class="com.example.db.voting.model.UserPermission"
                            role-permission-class="com.example.db.voting.model.UserPermission"/>
    
        <event type="org.jboss.seam.security.notLoggedIn">
           <action execute="#{redirect.captureCurrentView}"/>
        </event>
        <event type="org.jboss.seam.security.loginSuccessful">
           <action execute="#{redirect.returnToCapturedView}"/>
        </event>
    
        <mail:mail-session host="localhost" port="25"/>
    
    </components>
    



    Yes, I used seam-gen to create this project initially, and have not altered it all that much.


    I'm just really perplexed that not even the @Logger annotation is working. Everything is injected properly without the @stateless annotation on the class, then as soon as I add that nothing is injected. From what I understand, that places my class into the Stateless context, but the Log object should be injected into any context correct?


    Thank you

  • 5. Re: Seam injection works for POJO, fails when made Stateless
    Arbi Sookazian Master

    Are you using JBoss AS 4.x or 5.x?


    from ref doc:


    For JBoss AS, the following pattern is correct:
    <core:init jndi-name="earName/#{ejbName}/local" />
    In this case, earName is the name of the EAR in which the bean is deployed, Seam replaces
    #{ejbName} with the name of the EJB, and the final segment represents the type of interface
    (local or remote).



    Is the name of your EAR the following?


    kiosk-votingengine


    Are there any exceptions in the server.log after the authenticate() method executes?

  • 6. Re: Seam injection works for POJO, fails when made Stateless
    Ryan Nideffer Newbie

    I'm using JBoss 6.0.0 M1 and Seam 2.2.1-SNAPSHOT from last month (we are making the decision to deploy on Jboss 6, and the latest Seam GA release has a bug preventing startup with our deployment profile, hence the snapshot).


    Yes, my ear is named kiosk-votingengine, and the EJBs contained in my EAR modules are installed into JNDI with the proper names. I have verified using the JNDIView in JMX, but here are log snippets too:


    [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (Thread-2) Binding the following Entries in Global JNDI:
    
         kiosk-votingengine/DbAuthenticator/local - EJB3.x Default Local Business Interface
         kiosk-votingengine/DbAuthenticator/local-com.example.voting.auth.Authenticator - EJB3.x Local Business Interface
    
    ...
    
    [org.jboss.seam.Component] (Thread-2) Component: authenticator, scope: STATELESS, type: STATELESS_SESSION_BEAN, 
    class: com.example.voting.auth.DbAuthenticator, JNDI: kiosk-votingengine/DbAuthenticator/local
    



    And my Authenticator interface has the @Local annotation as well:


    @javax.ejb.Local
    public interface Authenticator {
    
        boolean authenticate();
    
    }
    



  • 7. Re: Seam injection works for POJO, fails when made Stateless
    Arbi Sookazian Master

    If I were you, I would try to reproduce using stable JBoss (4.2.x or 5.x) and Seam versions...  maybe it's a bug in Seam/JBoss.


    I don't recommend you be that aggressive about upgrading but it's your project...

  • 8. Re: Seam injection works for POJO, fails when made Stateless
    Ryan Nideffer Newbie

    Yeah, thats a good suggestion, and probably a little less time consuming that digging into Seam source and understanding why a static Logger gets injected but not a member variable.


    Thanks for you time and help!

  • 9. Re: Seam injection works for POJO, fails when made Stateless
    Stuart Douglas Master

    Doyou have the following ejb-jar.xml in your deployment?


    
    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
             version="3.0">
    
       <interceptors>
          <interceptor>
             <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
          </interceptor>
       </interceptors>
    
       <assembly-descriptor>
          <interceptor-binding>
             <ejb-name>*</ejb-name>
             <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
          </interceptor-binding>
       </assembly-descriptor>
    
    </ejb-jar>
    
    

  • 10. Re: Seam injection works for POJO, fails when made Stateless
    Ryan Nideffer Newbie

    D'oh! The jar containing my EJBs does NOT have that file, which would explain why nothing is being injected. I'll try it out shortly, thank you!

  • 11. Re: Seam injection works for POJO, fails when made Stateless
    nklashok namakkal Newbie

    Ryan, My POJO injection is failing . I am not able to get the EntityManager in side my DataBean. I need to get the session from the EntityManager and pass it to DAO. Can you throw some light on this?