Search code examples
jsfejbresourcebundle

Is there a way to inject an EJB into a custom Resource Bundle


I'm trying to build a resource bundle that get its messages from a database, rather than a properties file. I did some tests extending ResourceBundle, ResourceBundle.Control and ListResourceBundle, and I guess I'm on the right path.

I just reach a point when I need to query the actual database to fetch the messages from the my ResourceBundle implementation. I would like to leverage all the JPA infrastructure I have on the EJB layer in order to run the queries and instantiate the objects that will store the keys, messages and locales. So it would be just perfect if I could @EJB-inject my session bean into the ResourceBundle, but I cannot find a way to do it.

Here is my ResourceBundle code:

@ManagedBean(eager=true)
@ApplicationScoped
public class ResourceDicWeb extends ListResourceBundle {

@EJB
private MyDAO myDAO;

@Override
protected Object[][] getContents() {
    List<DicWeb> dicWeb = updateDictionary();

    Object[][] content = new Object[dicWeb.size()][2];

    for(int i = 0; i <= dicWeb.size(); i++) {
        content[i][0] = dicWeb.get(i).getAtrKey();
        content[i][1] = dicWeb.get(i).getAtrMessage();
    }

    return content;
}

    private List<DicWeb> updateDictionary() {
        return myDAO.fetchDictionary(); // at this point myDAO is always null
    }

}

My next step would be to use old-fashioned jdbc to create a database connection directly from the resourcebundle, but this is definitely the last option.

Cheers!


Solution

  • If the injection doesn't work then you should still be able to do this via JNDI.

    Context ctx = new InitialContext();
    MyDao myDao = (MyDao) ctx.lookup("myapp/myejbmodule/MyDao!org.myapp.ejb.MyDao");