Using Gemini DBAccess/JPA in services [message #989227] |
Wed, 05 December 2012 10:02  |
Eclipse User |
|
|
|
Hello,
I want to use Gemini DBAccess/JPA in a service.
For that I have created a plugin which will act as a service.
Dependencies as I have
org.eclipse.gemini.jpa;bundle-version="1.1.0",
org.eclipse.gemini.dbaccess.derby;bundle-version="1.1.0",
org.apache.derby;bundle-version="10.5.1",
com.hds.persistence.bundle;bundle-version="1.0.0",
org.eclipse.gemini.ext.di;bundle-version="1.0.0" .
In the META-INF/MANIFEST.MF file
Meta-Persistence: META-INF/persistence.xml
JPA-PersistenceUnits: hds
are registered.
The file META-INF/persistence.xml exists. The EMF also be created accordingly.
In my service implementation, I use the following to get a reference to the EentityManager:
@Inject
@GeminiPersistenceContext (unitName = "hds")
protected EntityManager em;
In the file OSGI-INF/component.xml I've also added a reference to the EMF.
<reference name="EMF"
interface="javax.persistence.EntityManagerFactory"
cardinality="1..1"
target="(osgi.unit.name=hdsPersistence)"
policy="dynamic"/>
See http://www.eclipse.org/forums/index.php/m/837592/?srch=gemini+jpa+service#msg_837592.
When I start the service, however, is no EM was injected.
If I
@Inject
@GeminiPersistenceContext (unitName = "hds")
protected EntityManager em;
for example, in a handler use, coupled with a CMD, the EM is injected correctly.
Can anyone tell me what needs to be done in order to use Gemini in a service?
The service is to be used in UI parts to encapsulate the access to the data.
Sorry for my English.
Greeting for Germany.
Ronald
|
|
|
|
|
|
|
Re: Using Gemini DBAccess/JPA in services [message #989476 is a reply to message #989360] |
Thu, 06 December 2012 11:53   |
Eclipse User |
|
|
|
Hello Mike,
it's exasperating.
I have looked at the thread and think that it does not fit. It is the basis of Gemini.Ext.DI implementation. The way I see it, I can here all the possible values that I can use in the persistence.xml.
I do not think it helps me more.
I want to try again to outline my problem.
First: I have a plugin that provides @Entity. It also contains the META-INF/persistence.xml and the matching entry in the MANIFEST.MF.
Second: I want to implement a plugin that provides services to the application (read, write, search for Entity, etc.). The interface of the application are provided so that this plugin interface methods.
Thirdly, when the activation of the service Plugings I would get the reference to the javax.persistence.EntityManager(Factory).
The first plugin (the one with the @Entity) is found when the application and it seems that the appropriate services are registered.
The second plugin I implemented a bundle Activator, which has a service tracker for javax.persistence.EntityManagerFactory. The method addingService (ServiceReference reference) returns the reference.
In the service implementation I have a method activate (Component context) implemented, the service searches for a reference, which I have set in the OSGI-INF/compoments.xml:
<reference name="EMF"
interface="javax.persistence.EntityManagerFactory"
cardinality="1..1"
target="(osgi.unit.name=hdsPersistence)"
policy="dynamic"/>
Here is a fragment from the Bundle-Activator:
@Override
public void start (BundleContext context) throws Exception {
log.info ("HDS Service DBAccess starting");
ctx = context;
dsfTracker = new ServiceTracker (
ctx,
EntityManagerFactory.class.getName (),
this);
dsfTracker.open ();
log.info (
"HDS Service DBAccess started ctx:=" + ctx + ", dsfTracker:=" + dsfTracker);
log.info ("HDS Service DBAccess started");
}
@Override
public Object addingService (ServiceReference reference) {
log.info ("addingService (" + reference + ") ...");
Bundle b = reference.getBundle();
Object service = b.getBundleContext().getService(reference);
String unitName = (String) reference.getProperty(EntityManagerFactoryBuilder.JPA_UNIT_NAME);
log.info ("b:=" + b);
log.info ("service:=" + service);
log.info ("unitName:=" + unitName);
return service;
}
Here is a fragment from the Service-Activator:
protected void activate (ComponentContext context) {
System.out.println (getClass ().getName () + ".activate (" + context + ")...");
Object obj = context.locateService ("EMF");
System.out.println (" locateService (\"EMF\"):=" + obj);
if (obj instanceof EMFServiceProxyHandler) { // <== ClassNotFoundException here
em = (EntityManager) ((EMFServiceProxyHandler) obj).createEMF (null);
}
if (obj instanceof EntityManagerFactory) {
em = ((EntityManagerFactory) obj).createEntityManager ();
}
System.out.println (" em:=" + em);
System.out.println (getClass ().getName () + ".activate (" + context + ") FINISHED");
}
When I start the application I receive in this method a ClassNotFoundException (see mark).
I have somewhere a mistake? Or a trifle forgotten?
Is there a way, the EntityManager (Factory) in the activation of the service implementation to resolve somehow the OSGI framework?
If so how?
Greetings form Germany.
Ronald
|
|
|
Re: Using Gemini DBAccess/JPA in services [message #989560 is a reply to message #989227] |
Thu, 06 December 2012 17:55   |
Eclipse User |
|
|
|
Hi,
There's is a missunderstanding of the gemini.ext.di plugin. It is design to inject EntityManager or EntityManagerFactory instances into e4 components.
If you want to use di in OSGi services, than use declarative services. This service can be injected via the normal e4 di mechanism, as e4 searches in the OSGi service registry for matching services.
cheers,
Muki
|
|
|
Re: Using Gemini DBAccess/JPA in services [message #989749 is a reply to message #989560] |
Fri, 07 December 2012 14:14   |
Eclipse User |
|
|
|
Hi Ronald,
I guessed that since you had @GeminiPersistenceContext in your code that you were already using e4 or had seen the e4 example from Filippe somewhere. It sounds like it was rather just by chance that you picked the same name, though. In any case, my understanding is that you can use DS with e4 if you want to.
For the CNF problem, the EMFServiceProxyHandler class is private to the impl, and the impl packages are not exported for applications to use. Why is it that you think you need to use it? You should only be accessing the EMF[Builder] and other public JPA and OSGi types.
-Mike
|
|
|
Re: Using Gemini DBAccess/JPA in services [message #989767 is a reply to message #989560] |
Fri, 07 December 2012 16:19  |
Eclipse User |
|
|
|
Hello Muki,
Hello Mike,
@Muki
Quote:If you want to use di in OSGi services, than use declarative services. This service can be injected via the normal e4 di mechanism, as e4 searches in the OSGi service registry for matching services.
That was the missing link. 
@Mike
Thanks for your help.
Have a nice weekend...
- Ronald
|
|
|
Powered by
FUDForum. Page generated in 0.03739 seconds