webtier@glassfish.java.net

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

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Wed, 02 Dec 2009 11:35:37 -0800

Mojarra delegates to Tomcat for EE-related resource injection. If
Tomcat doesn't support injection of PersistenceUnits (you'll need to
check their docs to verify), then there's nothing that can be done from
the JSF side.

Perhaps you could try your code on GlassFish V3?

On 12/2/09 11:26 AM, webtier_at_javadesktop.org wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>
>