users@glassfish.java.net

Re: OSGi Java EE hybrid example split into modules best practices

From: Sahoo <sanjeeb.sahoo_at_oracle.com>
Date: Fri, 24 Feb 2012 22:24:06 +0530

I thought I answered this earlier, but apparently I have not. You need
to inject EntityManagerFactory instead of EntityManager. Checkout
https://svn.java.net/svn/glassfish~svn/trunk/fighterfish/sample/uas
and look at
./ejbservice2/src/main/java/org/glassfish/fighterfish/sample/uas/ejbservice2/UserAuthServiceEJB2.java

You can build them yourself and try out.

I am traveling now, so expect some delays in replies.

Thanks,
Sahoo
On Tuesday 21 February 2012 11:48 PM, Kirk Rasmussen wrote:
>
> 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, 2^nd 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=Thread-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(UserAuthServiceEJB.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
>