Steve Ebersole commented on New Feature HHH-7527

Copying part of one of Martin's responses on mailing list...

Ok, to give some background information, I will cite a section of the
"OSGI Service Platform Release 4, Enterprise Specification Version 4.2":

127.5.1 Managed Model

A JPA Provider that supports running in managed mode should register a
specific service for the Java EE Containers: the Persistence Provider
service. The interface is the standard JPA PersistenceProvider interface.
See Dependencies on page 402 for the issues around the multiple versions
that this specification supports.
The service must be registered with the following service property:

javax.persistence.provider – The JPA Provider implementation class name, a
documented name for all JPA Providers.

The Persistence Provider service enables a Java EE Container to find a
particular JPA Provider. This service is intended for containers only, not
for Client Bundles because there are implicit assumptions
in the JPA Providers about the Java EE environment. A Java EE Container
must obey the life cycle of the Persistence Provider service. If this
service is unregistered then it must close all connections and
clean up the corresponding resources.

This means we need to generate a service which announces the existence of
a new persistence provider. There are in general two ways:

The programmatic way:

  • Create a BundleActivator.class which generates the service.
  • Add the Bundle-Activator (needed) and Export-Service (only informative
    nature) manifest headers

A non complete example could be:
--------------------------

HibernatePersistenceProviderActivator.java
package org.hibernate.jpa.osgi;

import java.util.Properties;
import javax.persistence.spi.PersistenceProvider;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class HibernatePersistenceProviderActivator implements BundleActivator {
     public void start(final BundleContext context) throws Exception {
         Properties properties = new Properties();
         properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
         context.registerService(
                 PersistenceProvider.class.getName(),
                 HibernatePersistenceProvider.class.newInstance(), 
                 properties
         );
    }

    public void stop(final BundleContext context) throws Exception {
    }
}
META-INF/MANIFEST.MF
...
Bundle-Activator: org.hibernate.jpa.osgi.PersistenceProviderActivator

--------------------------

The descriptive way:

  • Create a blueprint.xml file to describe and announce the service (See
    OSGI Enterprise Specification Version 4.2 - Section 121 for more
    information about blueprint)
  • The file must be located under OSGI-INF/blueprint
  • Add the Export-Service manifest headers (only informative nature)

Some background about blueprint bundles:

121.3 Blueprint Life-Cycle

A bundle is a Blueprint bundle if it contains one or more blueprint XML
definition resources in the OSGI-INF/blueprint directory or it contains
the Bundle-Blueprint manifest header referring to existing resources.

A non complete Example could be:
--------------------------

OSGI-INF/blueprint
<?xml version="1.0" encoding="UTF-8"?>

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.osgi.org/xmlns/blueprint/v1.0.0
        http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <bean id="hibernateJpaProvider" class="org.hibernate.jpa.HibernatePersistenceProvider" />
        <service ref="hibernateJpaProvider" interface="javax.persistence.spi.PersistenceProvider">
            <service-properties>
                 <entry key="javax.persistence.provider" value="org.hibernate.jpa.HibernatePersistenceProvider" />
            </service-properties>
       </service>
</blueprint>

--------------------------

I prefer the descriptive way - but I am open for discussions.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira