users@glassfish.java.net

OSGi Java EE hybrid example split into modules best practices

From: Kirk Rasmussen <kirk.rasmussen_at_texturacorp.com>
Date: Tue, 21 Feb 2012 12:18:36 -0600

Hello,

 

I am very interested in the hybrid Java EE approach to OSGi that
Glassfish supports to leverage declarative security and transaction
propagation available in Java EE. I was able to get the EclipseCon demo
app working fine.

 

I was wondering if it is possible to split up the EJB and JPA portions.
In other words I would like to have a persistence bundle that contains
the entities with all the goodies (sharable, lazy loaded, 2nd level
cache) and a separate EJB service bundle.

 

A more realistic service API would leverage Entities and not just Java
primitives like Strings. For example, let's say the 'UserAuthService'
added a new method:

 

public interface UserAuthService {

  ...

   // EJB impl would have @RolesAllowed("admin") applied

   public User lookup(String name)

}

 

So now the common bundle would have a dependency on the Entity User.
Seems logical to move this to another package / bundle, e.g.
eclipsecon2011.osgieehol.persistence. From my understanding
http://glassfish.java.net/public/GF-OSGi-Features.pdf the JPA bundle
must include both the entities and the META-INF/persistence.xml file
which I did.

 

Now the EJB bundle only contains 'UserAuthServiceEJB' which I modified
to lookup the EntityManager as an OSGi service (@PersistenceContext
didn't work):

 

@Stateless

public class UserAuthServiceEJB implements UserAuthService

{

    //_at_PersistenceContext

    @Inject @OSGiService(dynamic=true)

    private EntityManager em;

....

}

 

But it's not able to locate the JPA EntityManager

 

 

[#|2012-02-21T12:06:21.679-0600|WARNING|glassfish3.1.2|javax.enterprise.
system.container.ejb.com.sun.ejb.containers|_ThreadID=26;_ThreadName=Thr
ead-4;|EJB5184:A system exception occurred during an invocation on EJB
UserAuthServiceEJB, method: public boolean
eclipsecon2011.osgieehol.ejb_service.impl.UserAuthServiceEJB.login(java.
lang.String,java.lang.String)|#]

 

Caused by: java.lang.NullPointerException

        at
eclipsecon2011.osgieehol.ejb_service.impl.UserAuthServiceEJB.login(UserA
uthServiceEJB.java:27)

 

 

What is the proper way to accomplish this?

 

Thanks!

 

 

"Do the right thing. It will gratify some people and astonish the rest."
- Mark Twain