Error injecting EntityManager in Seam session component on Glassfish
cdunphy May 26, 2009 11:35 PMHello,
First, some information about my environment:
- Seam 2.1.2.CR2
- Glassfish 2.1
- Windows XP SP3 32-bit
I am working on a Seam session component that I can use to authenticate users to our AD controllers... here is the component I am trying to define:
@Stateless @Name("authenticator") public class AuthenticatorBean implements Authenticator { @Logger private Log log; @In private EntityManager entityManager; @In private Identity identity; @In private Credentials credentials; @In(create = true) private ActiveDirectoryController domain1ADPrimary; @In(create = true) private ActiveDirectoryController domain2ADBackup; @In(create = true) private ActiveDirectoryController domain2ADPrimary; @In(create = true) private ActiveDirectoryController domain2ADBackup; @Out(scope = ScopeType.SESSION) private User currentUser; // Here we inject the domain we want to use @RequestParameter private String domain; @Override public boolean authenticate() { currentUser = (User) entityManager.createQuery("select u from User u where u.username = :username") .setParameter(0, credentials.getUsername()) .getSingleResult(); if (currentUser == null) { log.error("User " + credentials.getUsername() + " exists in AD but is not authorized to use this application."); return false; } Map<String, Object> userMap = null; if (domain.equals(domain1ADPrimary.getDomain())) { // Login using domain1 AD Controllers } else if (domain.equals(domain2ADPrimary.getDomain())) { // Login using domain2 AD Controllers } else { log.error("ERROR: You must specify an NT domain."); return false; } identity.addRole(currentUser.getTeam().getLabel()); log.error("Authentication succeeded for user " + credentials.getUsername()); return true; } }
The applications starts and I can navigate through the CRUD pages that were made by seam-gen. The problem is that I am getting an Exception when I try to call this authenticate method on the authenticator component:
Caused by: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:comp/env/mnt/pu
This is strange for a few reasons. First of all, the CRUD pages that I generated from my Entity classes using the Seam Application Framework are all working just fine.
Here is the relevant persistence section of my components.xml file:
<persistence:managed-persistence-context name="entityManager" persistence-unit-jndi-name="@puJndiName@" auto-create="true" />
And the security portion:
<security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true" />
Here is the persistence unit:
<persistence-unit name="mnt"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>mntDatasource</jta-data-source> <jar-file>../../mnt.jar</jar-file> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.default_catalog" value="mnt" /> </properties> </persistence-unit>
When I check the exploded EAR file, it looks like it is setting the property correctly in C:\Projects\mnt\exploded-archives\mnt.ear\mnt_war\WEB-INF\classes\components.properties):
# These properties are used to replace Ant-style tokens in the component descriptor (components.xml) at runtime. jndiPattern=java:comp/env/mnt/#{ejbName}/local debug=true seamBootstrapsPu=false seamEmfRef=#{null} puJndiName=java:comp/env/mnt/pu
Does the fact that the CRUD classes are working, despite the fact the Home classes are JavaBean components, mean anything? Or could my persistence configuration for glassfish be working for JavaBean components but not EJB components? Or could there be something else going on that could be breaking the injection of the entityManager for this component?
Thanks!
// Chris