Hello,

because I need clean OSGI bundles for Hibernate and especially Envers, I am working on some patches for the 4.1 branch of hibernate-orm to enable generation of OSGI compatible jar files.

During the work I have found the following problems, and need some help and/or feedback. The problems are:

- The package org.hibernate.engine.spi is exported by two jar files, hibernate-core and hibernate-entitymanager, which is not possible in OSGI. The Problem is the STYLES map in CascadeStyle in hibernate-core which prevent that the package in hibernate-entitymanager can be refactored from org.hibernate.engine.spi to org.hibernate.ejb.engine.spi, to solve the duplicate export.
I think to fix this problem, the following steps should be done:
- Add a setter method to CascadeStyle to allow subclasses to add CascadeStyles to the map STYLES - Refactor the package org.hibernate.engine.spi in hibernate-envers to org.hibernate.ejb.engine.spi
- Use the setter method to add the new CascadeStyle in EJB3CascadeStyle

- The package org.hibernate.tool.ant is exported by to jars, hibernate-tools (version: 3.2.0.ga) and hibernate-envers, which is also a duplicate export problem. The problem is that HibernateToolTask in hibernate-tools has no getters/setters for configurationTask and thus hibernate-envers is directly reading/modyifing the property configurationTask. I think to fix this problem, the following steps should be done: - hibernate-tools should add public getters/setters for configurationTask in class HibernateToolTask - the package org.hibernate.tool.ant in hibernate-envers should be refactored to org.hibernate.envers.tool.ant - the property configurationTask should be accessed sole thru the getters/setters .

It would be nice if someone could check and comment my planned and already done changes.

I have already done the CascadeStyle fix, the configurationTask fix is outstanding because I need to know against which version of hibernate-tools I should create the fix.

In generally the following tasks are done until now:
- added configuration to gradle scripts to generate the osgi manifests for all jar files in hibernate-orm (needs maybe some cleanup) - right now the build files for hibernate-core, hibernate-entitymanager and hibernate-envers are modified. - added blueprint.xml to hibernate-entitymanager to announce the availabillity of a javax.persistence.provider

Next steps:
- add osgi manifests to the other jars of hibernate-orm
- write integration tests with pax exam and karaf 2.2.x
- finish refactoring of hibernate-tools
- add patches for other needed dependencies (hibernate-commons-annotations and more)
- more testing, more integration tests

Help, reviews and comments are welcome, and if someone has already done the work or parts, please drop me a note.

Attached find a patch against the branch 4.1 of hibernate-orm.

regards
Martin Neimeier

diff --git hibernate-core/hibernate-core.gradle 
hibernate-core/hibernate-core.gradle
index 789ba09..9d328ba 100644
--- hibernate-core/hibernate-core.gradle
+++ hibernate-core/hibernate-core.gradle
@@ -1,6 +1,7 @@
 apply plugin: 'antlr'
 apply plugin: org.hibernate.build.gradle.inject.InjectionPlugin
 apply plugin: org.hibernate.build.gradle.testing.matrix.MatrixTestingPlugin
+apply plugin: 'osgi'
 
 dependencies {
     compile( libraries.jta )
@@ -33,9 +34,24 @@ dependencies {
 
 }
 
-manifest.mainAttributes(
-        'Main-Class': 'org.hibernate.Version'
-)
+jar {
+   manifest {
+     attributes 'Main-Class': 'org.hibernate.Version'
+     instruction 'Import-Package', 
+                     'antlr.*;version=' + antlr_VersionRange,
+                  'com.fasterxml.classmate.*;version=' + 
com_fasterxml_classmate_VersionRange,
+                  'javassist.*;version=' + javassist_VersionRange,
+                  'org.apache.tools.ant.*;version=' + 
org_apache_tools_ant_VersionRange,
+                  'javax.persistence.*;version=' + 
javax_persistence_VersionRange,
+                  'javax.transaction.*;version=' + 
javax_transaction_VersionRange,
+                  'javax.validation.*;version=' + 
javax_validation_VersionRange,
+                  'org.dom4j.*;version=' + org_dom4j_VersionRange,
+                  'org.hibernate.annotations.common.*;version='+ 
org_hibernate_annotations_common_VersionRange,
+                  'org.jboss.jandex;version=' + org_jboss_jandex_VersionRange ,
+                  'org.jboss.logging;version=' + 
org_jboss_logging_VersionRange,
+                  '*;version=0'
+   }
+}
 
 sourceSets.main {
     ext.jaxbTargetDir = file( "${buildDir}/generated-src/jaxb/main" )
diff --git 
hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java 
hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java
index c39f0f6..4466b4c 100755
--- hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java
+++ hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java
@@ -302,6 +302,15 @@ public abstract class CascadeStyle implements Serializable 
{
                STYLES.put( "delete-orphan", DELETE_ORPHAN );
                STYLES.put( "none", NONE );
        }
+       
+       /**
+        * Add a new cascade style to the map 
+        * @param key The cascade style name
+        * @param value CascadeStyle instance
+        */
+       public static void putSTYLES(String key, CascadeStyle value) {
+               STYLES.put(key, value);
+       }
 
        /**
         * Factory method for obtaining named cascade styles
diff --git hibernate-entitymanager/hibernate-entitymanager.gradle 
hibernate-entitymanager/hibernate-entitymanager.gradle
index 393f8c9..fb27a48 100644
--- hibernate-entitymanager/hibernate-entitymanager.gradle
+++ hibernate-entitymanager/hibernate-entitymanager.gradle
@@ -1,6 +1,7 @@
 import org.apache.tools.ant.filters.ReplaceTokens
 
 apply plugin: org.hibernate.build.gradle.testing.matrix.MatrixTestingPlugin
+apply plugin: 'osgi'
 
 dependencies {
     compile( project(':hibernate-core') )
@@ -16,6 +17,22 @@ dependencies {
     testRuntime( libraries.validator )
 }
 
+jar {
+       manifest {
+               instruction 'Export-Service', 
+               
'javax.persistence.spi.PersistenceProvider;javax.persistence.provider="org.hibernate.ejb.HibernatePersistence"'
+               instruction 'Import-Package', 
+               'javassist.*;version=' + javassist_VersionRange,
+               'javax.persistence.*;version=' + javax_persistence_VersionRange,
+               'javax.transaction.*;version=' + javax_transaction_VersionRange,
+               'javax.validation.*;version=' + javax_validation_VersionRange,
+               'org.hibernate.annotations.common.*;version=' + 
org_hibernate_annotations_common_VersionRange,
+               'org.hibernate.*;version=' + org_hibernate_VersionRange,
+               '*;version=0'
+               
+       }
+}
+
 
////////////////////////////////////////////////////////////////////////////////////////////////////////
 // JPA model-gen set up
 
////////////////////////////////////////////////////////////////////////////////////////////////////////
diff --git 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadeStyle.java
 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadeStyle.java
index b5903be..eed2471 100644
--- 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadeStyle.java
+++ 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadeStyle.java
@@ -19,7 +19,10 @@
  * 51 Franklin Street, Fifth Floor
  * Boston, MA  02110-1301  USA
  */
-package org.hibernate.engine.spi;
+package org.hibernate.ejb.engine.spi;
+
+import org.hibernate.engine.spi.CascadeStyle;
+import org.hibernate.engine.spi.CascadingAction;
 
 /**
  * Becasue CascadeStyle is not opened and package protected,
@@ -47,6 +50,6 @@ public abstract class EJB3CascadeStyle extends CascadeStyle {
        };
 
        static {
-               STYLES.put( "persist", PERSIST_EJB3 );
+               putSTYLES( "persist", PERSIST_EJB3 );
        }
 }
diff --git 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadingAction.java
 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadingAction.java
index d86641a..091d397 100644
--- 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadingAction.java
+++ 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/engine/spi/EJB3CascadingAction.java
@@ -19,7 +19,7 @@
  * 51 Franklin Street, Fifth Floor
  * Boston, MA  02110-1301  USA
  */
-package org.hibernate.engine.spi;
+package org.hibernate.ejb.engine.spi;
 
 import java.util.Iterator;
 import java.util.Map;
@@ -28,6 +28,7 @@ import org.jboss.logging.Logger;
 
 import org.hibernate.HibernateException;
 import org.hibernate.ejb.internal.EntityManagerMessageLogger;
+import org.hibernate.engine.spi.CascadingAction;
 import org.hibernate.event.spi.EventSource;
 import org.hibernate.type.CollectionType;
 
diff --git 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/event/EJB3PersistEventListener.java
 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/event/EJB3PersistEventListener.java
index 9025130..e3b93a4 100644
--- 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/event/EJB3PersistEventListener.java
+++ 
hibernate-entitymanager/src/main/java/org/hibernate/ejb/event/EJB3PersistEventListener.java
@@ -26,8 +26,8 @@ package org.hibernate.ejb.event;
 import java.io.Serializable;
 
 import org.hibernate.engine.spi.CascadingAction;
-import org.hibernate.engine.spi.EJB3CascadeStyle;
-import org.hibernate.engine.spi.EJB3CascadingAction;
+import org.hibernate.ejb.engine.spi.EJB3CascadeStyle;
+import org.hibernate.ejb.engine.spi.EJB3CascadingAction;
 import org.hibernate.event.internal.DefaultPersistEventListener;
 import org.hibernate.event.spi.EventSource;
 
diff --git 
hibernate-entitymanager/src/main/resources/OSGI-INF/blueprint/blueprint.xml 
hibernate-entitymanager/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index e69de29..659d79a 100644
--- hibernate-entitymanager/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ hibernate-entitymanager/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -0,0 +1,15 @@
+<?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.ejb.HibernatePersistence" />
+
+       <service ref="hibernateJpaProvider" 
interface="javax.persistence.spi.PersistenceProvider">
+               <service-properties>
+                       <entry key="javax.persistence.provider" 
value="org.hibernate.ejb.HibernatePersistence" />
+               </service-properties>
+       </service>
+</blueprint>
\ No newline at end of file
diff --git hibernate-envers/hibernate-envers.gradle 
hibernate-envers/hibernate-envers.gradle
index 84e22c0..2d5549c 100644
--- hibernate-envers/hibernate-envers.gradle
+++ hibernate-envers/hibernate-envers.gradle
@@ -1,4 +1,5 @@
 apply plugin: org.hibernate.build.gradle.testing.matrix.MatrixTestingPlugin
+apply plugin: 'osgi'
 
 dependencies {
     compile( project( ':hibernate-core' ) )
@@ -9,6 +10,21 @@ dependencies {
     testRuntime( libraries.javassist )
 }
 
+jar {
+   manifest {
+     instruction 'Import-Package',
+        'javassist.*;version=' + javassist_VersionRange,
+        'org.apache.tools.ant.*;version=' + org_apache_tools_ant_VersionRange,
+        'org.dom4j.*;version=' + org_dom4j_VersionRange,
+               'javax.persistence.*;version=' + javax_persistence_VersionRange,
+               'javax.transaction.*;version=' + javax_transaction_VersionRange,
+               'org.hibernate.annotations.common.*;version=' + 
org_hibernate_annotations_common_VersionRange,
+               'org.hibernate.*;version=' + org_hibernate_VersionRange,   
+        'org.jboss.logging;version=' + org_jboss_logging_VersionRange,
+               '*;version=0'
+       }
+}
+     
 sourceSets {
     main {
         ext.generatedJpaMetamodelSrcDir = file( 
"${buildDir}/generated-src/jpamodelgen/${name}" )
diff --git libraries.gradle libraries.gradle
index a77639d..aaaa395 100644
--- libraries.gradle
+++ libraries.gradle
@@ -9,6 +9,20 @@ bytemanVersion = '1.5.2'
 infinispanVersion = '5.1.4.FINAL'
 jnpVersion = '5.0.6.CR1'
 
+// versions ranges for osgi manifests
+org_hibernate_VersionRange = '"[4.1.7.SNAPSHOT,5)"'
+org_apache_tools_ant_VersionRange = '"[1.8.2,2)"'
+antlr_VersionRange = '"[2.7.7,3)"'
+com_fasterxml_classmate_VersionRange = '"[0.5.4,1)"'
+javassist_VersionRange = '"[3.15.0.GA,4)"'
+javax_persistence_VersionRange = '"[2,3)"'
+javax_transaction_VersionRange = '"[1.1,2)"'
+javax_validation_VersionRange = '"[1,2)"'
+org_dom4j_VersionRange ='"[1.6.1,2)"'
+org_hibernate_annotations_common_VersionRange='"[4.0.1.Final,5)"'
+org_jboss_jandex_VersionRange='"[1.0.3.Final,2)"'
+org_jboss_logging_VersionRange='"[3.1.0.GA,4)"'
+
 libraries = [
         // Ant
         ant:            'org.apache.ant:ant:1.8.2',
_______________________________________________
hibernate-dev mailing list
hibernate-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/hibernate-dev

Reply via email to