New layout: graphics, css, few improvements, ..

YarentY

www.yarenty.com - projects incubator

JPA – OSGi – Hibernate – JDK6 (part 1) - RCP PDF Print E-mail
Written by yarenty   
Thursday, 25 June 2009 11:40
Article Index
JPA – OSGi – Hibernate – JDK6 (part 1)
Running in OSGi container
Hacking Hibernate bundles
All Pages

When I started eclipse RCP application which trying to use the same code...

 

.. I surprisingly (or not surprisingly) see:

 

javax.persistence.PersistenceException: No Persistence provider for EntityManager named eireshop

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:129)

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:99)

      at com.yarenty.eireshop.dao.helper.JPAHelper.getEntityManager(JPAHelper.java:59)

 

UPS…

Lets investigate:

Go to Osgi container and display all bundles:

osgi>ss

[..]

162   RESOLVED    com.springsource.org.jboss.util_2.0.4.GA

163   RESOLVED    com.springsource.com.mysql.jdbc_5.1.6

171   RESOLVED    com.springsource.org.jgroups_2.5.1

179   RESOLVED    com.springsource.javax.transaction_1.1.0

205   RESOLVED    com.springsource.org.hibernate.annotations_3.4.0.GA

                  Master=228

208   RESOLVED    com.springsource.org.hibernate.validator_3.1.0.GA

218   ACTIVE      com.yarenty.eireshop.ui_1.0.0

226   ACTIVE      com.springsource.antlr_2.7.7

227   RESOLVED    com.springsource.javax.persistence_1.99.0

228   ACTIVE      com.springsource.org.hibernate_3.3.1.GA

                  Fragments=205, 230

229   RESOLVED    com.springsource.org.hibernate.annotations.common_3.3.0.ga

230   RESOLVED    com.springsource.org.hibernate.ejb_3.4.0.GA

                  Master=228

231   RESOLVED    com.yarenty.erieshop.dao_1.0.0

 

osgi>ss

 

 

All looks OK. But when I check source code for javax.persistence I’ve found that searching persistence providers looking for current thread classloader. That will not work in OSGi as current thread has visibility only for bundles that are declared in manifest.

 

      public Collection<PersistenceProvider> findAllProviders() throws IOException {

     

              ClassLoader loader = Thread.currentThread()

                                    .getContextClassLoader();

              Enumeration<URL> resources = loader

                                .getResources(SERVICE_PROVIDER_FILE);

     

              Set<String> providerNames = new HashSet<String>();

              while (resources.hasMoreElements()) {

                  URL url = resources.nextElement();

                  InputStream is = url.openStream();

                  try {

                        providerNames.addAll(

                            providerNamesFromReader(

                                new BufferedReader(

                                   new InputStreamReader(is))));

                  } finally {

                      is.close();

                  }

              }

              Collection<PersistenceProvider> loadedProviders =

                            new HashSet<PersistenceProvider>(); 

              for (String s : providerNames) {

                  try{

                        loadedProviders

                   .add((PersistenceProvider)loader

                            .loadClass(s).newInstance());

                  } catch (ClassNotFoundException exc){

                  } catch (InstantiationException exc){

                  } catch (IllegalAccessException exc){

                  }

              }

              return loadedProviders;

      }

 

 

But I found that there is javax.persistence.osgi  package, with activator and !!! tracker !!!. And that’s what I’m really looking for:

osgi> start 227

Persistence bundle starting...

Persistence bundle started.

 

 

Lets start one more time but this time with started persistence bundle:

osgi> Persistence bundle starting...

Persistence bundle started.

javax.persistence.PersistenceException: No Persistence provider for EntityManager named eireshop

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:129)

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:99)

      at com.yarenty.eireshop.dao.helper.JPAHelper.getEntityManager(JPAHelper.java:59)

      at com.yarenty.eireshop.dao.impl.UserHome.<clinit>(UserHome.java:34)

      at com.yarenty.eireshop.ui.View.createPartControl(View.java:115)

      at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:371)

      at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:230)

      at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:594)

      at org.eclipse.ui.internal.PartPane.setVisible(PartPane.java:306)

OSGi - Find all providers.

 

It’s working but couldn’t find providers.

osgi> Persistence bundle starting...

Persistence bundle started.

OSGi - Find all providers.

 

javax.persistence.PersistenceException: No Persistence provider for EntityManager named eireshop

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:129)

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:99)

      at com.yarenty.eireshop.dao.helper.JPAHelper.getEntityManager(JPAHelper.java:59)

 

 

 

Ok, if it can’t find them itself – lets help them.In my Dao bundle I create Activator class.

package com.yarenty.eireshop.dao.activator;

 

import java.util.Date;

import java.util.Dictionary;

import java.util.Properties;

 

import org.hibernate.ejb.HibernatePersistence;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

 

import javax.persistence.spi.PersistenceProvider;

 

/**

 * @author ejarnow

 */

public class Activator implements BundleActivator {

     

      private BundleContext context;

 

      @Override

      public void start(BundleContext context) throws Exception {

     

             System.out.println(

                   "Registering persistence provider ;-)");

 

      Properties properties = new Properties();

      properties.put("javax.persistence.spi.PersistenceProvider",

                     "eireshop");

 

            context.registerService(

                    PersistenceProvider.class.getName(),

                    new HibernatePersistence(),

                    properties);

           

           

      }

 

      @Override

      public void stop(BundleContext context) throws Exception {

      }

 

}

 

 

 

 

And output:

osgi> Persistence bundle starting...

Persistence bundle started.

Registering persistence provider ;-)

ProviderTracker: New service detected...

ProviderTracker: Added service eireshop

24-Jun-2009 12:07:31 org.slf4j.impl.JCLLoggerAdapter info

INFO: Hibernate Annotations 3.4.0.GA

24-Jun-2009 12:07:31 org.slf4j.impl.JCLLoggerAdapter info

INFO: Hibernate 3.3.1.GA

24-Jun-2009 12:07:31 org.slf4j.impl.JCLLoggerAdapter info

INFO: hibernate.properties not found

24-Jun-2009 12:07:31 org.slf4j.impl.JCLLoggerAdapter info

INFO: Bytecode provider name : javassist

24-Jun-2009 12:07:31 org.slf4j.impl.JCLLoggerAdapter info

INFO: using JDK 1.4 java.sql.Timestamp handling

24-Jun-2009 12:07:32 org.slf4j.impl.JCLLoggerAdapter info

INFO: Hibernate EntityManager 3.4.0.GA

24-Jun-2009 12:07:32 org.slf4j.impl.JCLLoggerAdapter info

INFO: Could not find any META-INF/persistence.xml file in the classpath

javax.persistence.PersistenceException: No Persistence provider for EntityManager named eireshop

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:129)

      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:99)

      at com.yarenty.eireshop.dao.helper.JPAHelper.getEntityManager(JPAHelper.java:59)

 

 

[…]

 

 

ProviderTracker: Removing service...

ProviderTracker: Removed service eireshop service object: org.hibernate.ejb.HibernatePersistence@1435ec9

Persistence bundle stopping...

Persistence bundle stopped.

 

 

Persistence looks like working – classes inside hibernate are called. Still provider, are not available, but now we see: “Could not find any META-INF/persistence.xml”

As it really is – tested before - it means some bundle doesn’t see my bundle classpath.

 

Now there are two possibilities:

-          hack them

-          write proper solution

 

First I will hack – it’s much easier and will prove where problem is (without reading gazylinos lines of code ;-) ).

 



Last Updated ( Thursday, 25 June 2009 13:31 )