tags:

views:

667

answers:

1

I am having problems getting JBoss to inject an environment variable value into a bean. Here is my bean class:

package com.topcoder.test;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.annotation.ejb.RemoteBinding;

import com.topcoder.test.Test;

public @Stateless(name = "TestBean")
class TestBean implements Test {

    @Resource(name="managerFile")
    private String managerFile;

    public String getManagerFile() {
        return managerFile;
    }

    public String testMethod() {
        String ret = "the value of managerFile in testMethod() is " + getManagerFile();
        return ret;
    }

    @PersistenceContext(unitName = "persistenceUnit", type = PersistenceContextType.TRANSACTION)
    private EntityManager manager;

    @PersistenceContext(unitName = "persistenceUnit", type = PersistenceContextType.TRANSACTION)
    public void setEntityManager(EntityManager manager) {
        this.manager = manager;
    }

    public EntityManager getEntityManager() {
        if (manager == null) {
            throw new IllegalStateException("EntityManager has not been set on DAO before usage");
        }

        return manager;
    }

    @SuppressWarnings("unchecked")
    public List retrieveAll() throws Exception {
        EntityManager em = getEntityManager();

        List result = em.createQuery("FROM TestEntity").getResultList();

        if (result == null) {
            result = new ArrayList();
        }

        return result;
    }

}

I want the managerFile value to be injected by JBoss. Here is my ejb-jar.xml:

<enterprise-beans>
 <session>
  <ejb-name>Test</ejb-name>
  <ejb-class>com.topcoder.test.TestBean</ejb-class>
  <env-entry>
   <env-entry-name>managerFile</env-entry-name>
   <env-entry-type>java.lang.String</env-entry-type>
   <env-entry-value>com/topcoder/test/TestBean.properties</env-entry-value>
  </env-entry>
 </session>
</enterprise-beans>

But I get this error message when I deploy my ear to JBoss:

09:07:40,495 WARN [ResourceHandler] Not injecting managerFile, no matching enc injector env/managerFile found

I am running JBoss 4.2. What am I doing wrong?

+1  A: 

Ensure that the ejb-jar.xml is in the META-INF/ of the JAR that your EJBs are in, not the EAR's META-INF.

Unrelated to your issue you can remove the:

@PersistenceContext

from your setEntityManager() method (you don't need to annotate both it and the entityManager field)

mtpettyp
Thanks for your answer. I changed the Test to TestBean on the @Stateless annotation, but I still get the same error:[ResourceHandler] Not injecting managerFile, no matching enc injector env/managerFile foundAny other ideas?
dcp
Are you sure the ejb-jar.xml file is in your JAR's META-INF/ directory? If I don't included the <env-entry/> I get the same error so your issue may be a deployment problem
mtpettyp
I have an ear directory that I deployed to JBoss, and underneath that ear folder I have a META-INF folder. The ejb-jar.xml is in that META-INF folder.
dcp
How is your stateless session bean packaged? The ejb-jar.xml needs to be in the META-INF/ directory of the jar that contains that code, not the ear's MET-INF.
mtpettyp
That was it! Thanks so much. Once I put the ejb-jar.xml in the META-INF for the jar for the ejb code, it worked. I assume the application.xml and jboss-app.xml need to stay in the META-INF folder of the ear, right? That's the way I currently have it. Thanks again!
dcp
Yup, those two files need to be in the META-INF/ of the EAR. I'll update this Answer with the correct fix
mtpettyp