webtier@glassfish.java.net

Re: NullPointerException when using _at_ManagedBean or @PersistenceUnit in a Phase

From: <webtier_at_javadesktop.org>
Date: Wed, 02 Dec 2009 11:26:30 PST

System: Ubuntu 9.10 -64
Java Version: 1.6.0_16
Container: Tomcat 6.0.20
Hibernate Version: 3.4.0.GA

Here is a REALLY stripped down version of my code (only included relevant parts):


public class AccessControlPhaseListener implements PhaseListener {
    @ManagedProperty(value = "#{auth}")
    private AuthenticationBean authService;

    public void afterPhase(PhaseEvent event) {
        if (authService == null) {
          authService = FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(context.getELContext(),null, "auth");
        }

        if (!authService.isLoggedIn()) {
            // .... redirect to login page....
        }
    }

    public PhaseId getPhaseId() {
        //ALL access go through RESTORE_VIEW and RENDER_VIEW (even direct url)
        return PhaseId.RESTORE_VIEW;
    }
}
============================================================
@ManagedBean(name="auth")
@SessionScoped
public class AuthenticationBean {

    @PersistenceUnit(unitName = "MyPU")
    private EntityManagerFactory emf;

    public boolean isLoggedIn() {
        EntityManager em = emf.createEntityManager();
        // ... do login check
    }
}
 =======================================================
Persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <!-- Derby -->
      <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
      <property name="hibernate.connection.url" value="jdbc:derby:/home/jeffdcamp/src/jsf2-helloworld3/database/mydb;create=true"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format.sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>
[Message sent by forum member 'jeffcampbell' ]

http://forums.java.net/jive/thread.jspa?messageID=374351